【E - ECJTU_ACM 11级队员2012年暑假训练赛(2)】
Description
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
1 // Project name : E 2 // File name : main.cpp 3 // Author : Izumu 4 // Date & Time : Tue Jul 10 15:13:33 2012 5 6 #include <iostream> 7 #include <vector> 8 #include <algorithm> 9 #include <string> 10 #include <queue> 11 #include <stack> 12 #include <cstring> 13 #include <cstdio> 14 #include <iomanip> 15 using namespace std; 16 const int inf=0x3f3f3f3f; 17 const int maxn=222; 18 int c[maxn][maxn]; 19 int f[maxn][maxn]; 20 bool you[maxn][maxn]; 21 int cen[maxn]; 22 int n,m; 23 int tx,ty,tc; 24 vector<int>g[maxn]; 25 queue<int>q; 26 bool bfs() 27 { 28 memset(cen,-1,sizeof(cen)); 29 while (!q.empty()) q.pop(); 30 q.push(1); 31 cen[1]=0; 32 int now; 33 while(!q.empty()) 34 { 35 now=q.front(); 36 q.pop(); 37 for(int i=0;i<g[now].size();i++) 38 { 39 if(you[now][g[now][i]] && cen[g[now][i]] == -1) 40 { 41 cen[g[now][i]]= cen[now] + 1; 42 q.push(g[now][i]); 43 } 44 } 45 } 46 return cen[n]!=-1; 47 } 48 int dfs(int flow=inf,int now=1) 49 { 50 if(cen[now] == cen[n] ) 51 { 52 if(now == n) 53 { 54 return flow; 55 } 56 else 57 { 58 return 0; 59 } 60 } 61 int temp,sum; 62 sum = 0; 63 for(int i=0;i<g[now].size();i++) 64 { 65 if(cen[g[now][i]] - cen[now] !=1 || !you[now][g[now][i]] || cen[g[now][i]] > cen[n] ) 66 { 67 continue; 68 } 69 temp = dfs (min(c[now][g[now][i]] - f[now][g[now][i]] , flow) , g[now][i]); 70 f[now][g[now][i]] += temp; 71 f[g[now][i]][now] -= temp; 72 if(f[now][g[now][i]] == c[now][g[now][i]]) 73 { 74 you[now][g[now][i]] = false; 75 } 76 you[g[now][i]][now] = true; 77 sum += temp; 78 flow -= temp; 79 } 80 return sum; 81 } 82 83 int dinic() 84 { 85 int ans=0; 86 while( bfs() ) 87 { 88 ans+=dfs(); 89 } 90 return ans; 91 } 92 93 int main() 94 { 95 while(cin>>m>>n) 96 { 97 memset(c,0,sizeof(c)); 98 memset(f,0,sizeof(f)); 99 memset(you,false,sizeof(you)); 100 for(int i=1;i<=n;i++) 101 { 102 g[i].clear(); 103 } 104 for(int i=1;i<=m;i++) 105 { 106 cin>>tx>>ty>>tc; 107 c[tx][ty]+=tc; 108 you[tx][ty]=true; 109 g[tx].push_back(ty); 110 g[ty].push_back(tx); 111 } 112 cout<<dinic()<<endl; 113 } 114 return 0; 115 } 116 117 // end 118 // ism