标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4240
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #include<vector> 8 #include<queue> 9 #define inf 0x7fffffff 10 using namespace std; 11 const int maxn=1000+10; 12 const int M = 9999999; 13 14 int n,m,A,B; 15 int from,to; 16 int d[maxn]; 17 int one; 18 struct node 19 { 20 int v,flow; 21 int next; 22 }edge[M]; 23 int head[maxn],edgenum; 24 25 void add(int u,int v,int flow) 26 { 27 edge[edgenum].v=v ;edge[edgenum].flow=flow; 28 edge[edgenum].next=head[u]; 29 head[u]=edgenum++; 30 31 edge[edgenum].v=u ;edge[edgenum].flow=0; 32 edge[edgenum].next=head[v]; 33 head[v]=edgenum++; 34 } 35 36 int bfs() 37 { 38 memset(d,0,sizeof(d)); 39 d[from]=1; 40 queue<int> Q; 41 Q.push(from); 42 while (!Q.empty()) 43 { 44 int u=Q.front() ;Q.pop() ; 45 for (int i=head[u] ;i!=-1 ;i=edge[i].next) 46 { 47 int v=edge[i].v; 48 if (!d[v] && edge[i].flow>0) 49 { 50 d[v]=d[u]+1; 51 Q.push(v); 52 if (v==to) return 1; 53 } 54 } 55 } 56 return 0; 57 } 58 59 int dfs(int u,int flow) 60 { 61 if (u==to || flow==0) 62 { 63 if (u==to) one=max(one,flow); 64 return flow; 65 } 66 int cap=flow; 67 for (int i=head[u] ;i!=-1 ;i=edge[i].next) 68 { 69 int v=edge[i].v; 70 if (d[v]==d[u]+1 && edge[i].flow>0) 71 { 72 int x=dfs(v,min(cap,edge[i].flow)); 73 edge[i].flow -= x; 74 edge[i^1].flow += x; 75 cap -= x; 76 if (cap==0) return flow; 77 } 78 } 79 return flow-cap; 80 } 81 82 double dinic() 83 { 84 double sum=0; 85 one=0; 86 while (bfs()) sum += (double)dfs(from,inf); 87 cout<<sum<<" "<<one<<endl; 88 return sum/(double)one; 89 } 90 91 int main() 92 { 93 int t,D; 94 scanf("%d",&t); 95 while (t--) 96 { 97 scanf("%d%d%d%d%d",&D,&n,&m,&A,&B); 98 memset(head,-1,sizeof(head)); 99 edgenum=0; 100 from=A; 101 to=B; 102 int a,b,c; 103 for (int i=0 ;i<m ;i++) 104 { 105 scanf("%d%d%d",&a,&b,&c); 106 add(a,b,c); 107 } 108 double ans=dinic(); 109 printf("%d %.3lf\n",D,ans); 110 } 111 return 0; 112 }
标签:
原文地址:http://www.cnblogs.com/huangxf/p/4324867.html