ZOJ 3792 Romantic Value 最小割+求割边的数量
来源:互联网 发布:新浪直播软件 编辑:程序博客网 时间:2024/06/05 02:22
Farmer John is a diligent man. He spent a lot of time building roads between his farms. From his point of view, every road is romantic because the scenery along it is very harmonious and beautiful. Recently, John is immersed in poetry, he wants stay alone and enjoy the wonderful words. But his little brother always disturbs him. This night, fortunately, his little brother does not stay the same farm with him. So, he wants to destroy some roads to keep himself quiet for a few days(then no route exist between John and his brother). Of course, John love his romantic roads, so he want to separate him and his brother with least romantic cost.
There are N farms(numbered from 1 to N) and M undirected roads each with a romantic value c(indicate how much Farmer John loves it). Now John stays in farm p and his little brother stay in farm q. John wants to first minimize the romantic value lost, then to destroy as few roads as possible. Help him to calculate the ratio of [sum of the remainder roads' value]/[the amount of removed roads](not necessary to maximisation this ratio) when he achieves his goal.
Input
The first line is a single integer T, indicate the number of testcases. Then follows T testcase. For each testcase, the first line contains four integers N M p q(you can assume p and q are unequal), then following M lines each contains three integer a b c which means there is an undirected road between farm a and farm b with romantic value c. (2<=N<=50, 0<=M<=1000, 1<=c<1000, 1<=p,q<=N)
Output
For each test case, print the ratio in a single line(keep two decimal places). If p and q exist no route at the start, then output "Inf".
Sample Input
14 5 1 41 2 11 3 12 4 23 4 22 3 1
Sample Output
2.50
Author: ZHAO, Liqiang
Source: ZOJ Monthly, June 2014
#include<stdio.h>#include<string.h>#include<algorithm>#define MAXN 57 //点#define MAXM 4007//边#define inf 0x3f3f3f3fusing namespace std;struct E{ int from,v,next; int cap;} edge[MAXM];int num;int head[MAXN],dis[MAXN],gap[MAXN];int nn,n,m;//代表点的个数void init(){ num=0; memset(head,-1,sizeof(head));}void addedge(int u,int v,int w){ edge[num].from=u; edge[num].v=v; edge[num].cap=w; edge[num].next=head[u]; head[u]=num++; edge[num].from=v; edge[num].v=u; edge[num].cap=0; edge[num].next=head[v]; head[v]=num++;}void BFS(int start,int end){ memset(dis,-1,sizeof(dis)); memset(gap,0,sizeof(gap)); gap[0]=1; int que[MAXN],front,rear; front=rear=0; dis[end]=0; que[rear++]=end; while(front!=rear) { int u=que[front++]; if(front==MAXN)front=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(dis[v]!=-1)continue; que[rear++]=v; if(rear==MAXN)rear=0; dis[v]=dis[u]+1; ++gap[dis[v]]; } }}int SAP(int start,int end){ int res=0; nn=n; BFS(start,end); int cur[MAXN],S[MAXN]; memcpy(cur,head,sizeof(head)); int u=start,i,vp=0; while(dis[start]<nn) { if(u==end) { int temp=inf; int inser; for(i=0; i<vp; i++) if(temp>edge[S[i]].cap) { temp=edge[S[i]].cap; inser=i; } for(i=0; i<vp; i++) { edge[S[i]].cap-=temp; edge[S[i]^1].cap+=temp; } res+=temp; vp=inser; u=edge[S[vp]].from; } if(u!=end&&gap[dis[u]-1]==0) break; for(i=cur[u]; i!=-1; i=edge[i].next) if(edge[i].cap!=0&&dis[u]==dis[edge[i].v]+1) break; if(i!=-1) { cur[u]=i; S[vp++]=i; u=edge[i].v; } else { int min=nn; for(i=head[u]; i!=-1; i=edge[i].next) { if(edge[i].cap==0)continue; if(min>dis[edge[i].v]) { min=dis[edge[i].v]; cur[u]=i; } } --gap[dis[u]]; dis[u]=min+1; ++gap[dis[u]]; if(u!=start)u=edge[S[--vp]].from; } } return res;}int main(){ int s,t,T; scanf("%d",&T); while(T--) { scanf("%d%d%d%d",&n,&m,&s,&t); init(); int u,v,c,sum=0; for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&c); addedge(u,v,c*1000+1); addedge(v,u,c*1000+1); sum+=c; } int ans=SAP(s,t); if(!ans){printf("Inf\n");continue;} double a,b; if(ans%1000==0){b=1000;ans-=1000;} else b=ans%1000; a=sum-ans/1000; printf("%.2lf\n",1.0*a/b); } return 0;}
- ZOJ 3792 Romantic Value 最小割+求割边的数量
- zoj 3792 Romantic Value(最小割)
- ZOJ 3792 Romantic Value 最小割
- ZOJ 3792 Romantic Value 最小割
- zoj 3792 Romantic Value(最小割下边数最小)
- zoj-3792-Romantic Value-最小割+数值转化
- ZOJ 3792 Romantic Value(最小割问题)
- ZOJ 3792 Romantic Value(ISAP && 最小割)
- zoj 3792 最小割等于最大流 Romantic Value
- ZOJ 3792 - Romantic Value (网络流‘最小割)
- ZOJ 3792 Romantic Value 最小割(最小费用下最小边数)
- ZOJ 3792 Romantic Value(网络流之最小割)(找割边)
- zoj3792 Romantic Value 最大流<->最小割
- zoj3792--Romantic Value(最大流+最小割,求解割边)
- ZOJ 3792 最小割+求割边
- zoj 3792 最小割
- ZOJ 2587 最小割的唯一性
- ZOJ 2587 最小割的唯一性
- 如何进行ORACLE数据挖掘 ?
- [LeetCode 66]Plus One
- sea人去娃儿去玩儿去玩儿
- 生产者与消费者的案例
- MOT
- ZOJ 3792 Romantic Value 最小割+求割边的数量
- 支持向量机通俗导论(理解SVM的三层境界)
- [Unity编辑器]自定义编辑器界面
- 历届试题 带分数 (暴力+全排列)
- 信号量与PV操作
- 如何学习sencha-touch2 mvc(2013-6-22)
- http://huangwen200301930.blog.163.com/blog/static/484849812011111143243761/
- 抽象类
- 文明之剑-我读王晋康科幻小说