Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22789 Accepted Submission(s): 10905
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.
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int INF=0x3f3f3f3f; 4 const int N=200+10; 5 int flow[N][N]; 6 int mark[N],pre[N]; 7 int n,m,f; 8 void max_flow(){ 9 while(1){ 10 memset(mark,0,sizeof(mark)); 11 memset(pre,0,sizeof(pre)); 12 queue<int>Q; 13 mark[1]=1; 14 Q.push(1); 15 while(!Q.empty()){ 16 int cnt=Q.front(); 17 Q.pop(); 18 if(cnt==n)break; 19 for(int i=1;i<=n;i++){ 20 if(!mark[i]&&flow[cnt][i]>0){ 21 mark[i]=1; 22 Q.push(i); 23 pre[i]=cnt; 24 } 25 } 26 }if(!mark[n])break; 27 int minx=INF; 28 for(int i=n;i!=1;i=pre[i]){ 29 minx=min(flow[pre[i]][i],minx); 30 } 31 for(int i=n;i!=1;i=pre[i]){ 32 flow[pre[i]][i]-=minx; 33 flow[i][pre[i]]+=minx; 34 } 35 f+=minx; 36 } 37 } 38 int main(){ 39 while(~scanf("%d%d",&m,&n)){ 40 memset(flow,0,sizeof(flow)); 41 for(int i=0;i<m;i++){ 42 int u,v,len; 43 scanf("%d%d%d",&u,&v,&len); 44 flow[u][v]+=len; 45 } 46 f=0; 47 max_flow(); 48 printf("%d\n",f); 49 } 50 return 0; 51 }