标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #define inf 0x7fffffff 9 using namespace std; 10 const int maxn=16; 11 12 int n,m; 13 int graph[maxn][maxn],d[maxn]; 14 15 int bfs() 16 { 17 memset(d,0,sizeof(d)); 18 d[1]=1; 19 queue<int> Q; 20 Q.push(1); 21 while (!Q.empty()) 22 { 23 int u=Q.front() ;Q.pop() ; 24 for (int v=1 ;v<=n ;v++) 25 { 26 if (!d[v] && graph[u][v]>0) 27 { 28 d[v]=d[u]+1; 29 Q.push(v); 30 if (v==n) return 1; 31 } 32 } 33 } 34 return 0; 35 } 36 37 int dfs(int u,int flow) 38 { 39 if (u==n || flow==0) return flow; 40 int cap=flow; 41 for (int v=1 ;v<=n ;v++) 42 { 43 if (d[v]==d[u]+1 && graph[u][v]>0) 44 { 45 int x=dfs(v,min(cap,graph[u][v])); 46 cap -= x; 47 graph[u][v] -= x; 48 graph[v][u] += x; 49 if (cap==0) return flow; 50 } 51 } 52 return flow-cap; 53 } 54 55 int Dinic() 56 { 57 int sum=0; 58 while (bfs()) sum += dfs(1,inf); 59 return sum; 60 } 61 62 int main() 63 { 64 int t,ncase=1; 65 scanf("%d",&t); 66 while (t--) 67 { 68 scanf("%d%d",&n,&m); 69 int a,b,c; 70 memset(graph,0,sizeof(graph)); 71 for (int i=0 ;i<m ;i++) 72 { 73 scanf("%d%d%d",&a,&b,&c); 74 graph[a][b] += c; 75 } 76 printf("Case %d: %d\n",ncase++,Dinic()); 77 } 78 return 0; 79 }
标签:
原文地址:http://www.cnblogs.com/huangxf/p/4265800.html