北京好热啊,宿舍还没空调,都不能安安静静地敲代码了~>_<~今天老师讲网络流完全没听啊,晚上想了好久的网络流,感觉还是没有完全理解,过了一道模板题。
Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 61994 | Accepted: 23794 |
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.
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 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 int map[300][300]; 8 int pre[300]; 9 bool vis[300]; 10 int n,m,s,t; 11 bool bfs() 12 { 13 int i,temp; 14 queue<int>q; 15 memset(pre,0,sizeof(pre)); 16 memset(vis,0,sizeof(vis)); 17 vis[s]=true; 18 q.push(s); 19 while(!q.empty()) 20 { 21 temp=q.front(); 22 q.pop(); 23 if(temp==t) return true; 24 for(i=1;i<=n;i++) 25 { 26 if(!vis[i]&&map[temp][i]) 27 { 28 q.push(i); 29 pre[i]=temp; 30 vis[i]=true; 31 } 32 } 33 } 34 return false; 35 } 36 int EK() 37 { 38 int i,ans=0; 39 while(1) 40 { 41 if(!bfs()) return ans; 42 int MIN=9999999; 43 for(i=t;i!=s;i=pre[i]) 44 MIN=min(MIN,map[pre[i]][i]); 45 for(i=t;i!=s;i=pre[i]) 46 { 47 map[pre[i]][i]-=MIN; 48 map[i][pre[i]]+=MIN; 49 } 50 ans+=MIN; 51 } 52 } 53 int main() 54 { 55 while(~scanf("%d%d",&m,&n)) 56 { 57 s=1,t=n; 58 memset(map,0,sizeof(map)); 59 for(int i=0;i<m;i++) 60 { 61 int a,b,c; 62 scanf("%d%d%d",&a,&b,&c); 63 map[a][b]+=c; 64 } 65 printf("%d\n",EK()); 66 } 67 return 0; 68 }