标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 67823 | Accepted: 26209 |
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
Source
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<cassert> 7 #include<climits> 8 #define maxn 210 9 using namespace std; 10 void find(); 11 void flow(); 12 void update(); 13 struct Edge 14 { 15 int c; 16 int f; 17 }edge[maxn][maxn]; 18 int n,m; 19 int s,t; 20 int residual[maxn][maxn]; 21 int que[maxn*maxn],head,tail; 22 int pre[maxn]; 23 bool vis[maxn]; 24 int max_flow,min_flow; 25 void find() 26 { 27 int i,cu; 28 memset(vis,false,sizeof(vis)); 29 memset(residual,0,sizeof(residual)); 30 memset(pre,0,sizeof(pre)); 31 head=0;que[head]=s;pre[s]=s;vis[s]=true;tail=1; 32 while(head<tail&&pre[t]==0) 33 { 34 cu=que[head]; 35 for(i=1;i<=n;i++) 36 { 37 if(vis[i]==false) 38 { 39 if(edge[cu][i].c-edge[cu][i].f>0) 40 { 41 residual[cu][i]=edge[cu][i].c-edge[cu][i].f; 42 pre[i]=cu;que[tail++]=i;vis[i]=true; 43 } 44 else if(edge[i][cu].f>0) 45 { 46 residual[cu][i]=edge[i][cu].f; 47 pre[i]=cu;que[tail++]=i;vis[i]=true; 48 } 49 } 50 } 51 head++; 52 } 53 } 54 void flow() 55 { 56 int i=t,j; 57 if(pre[i]==0) 58 { 59 min_flow=0;return; 60 } 61 j=0x7fffffff; 62 while(i!=s) 63 { 64 if(residual[pre[i]][i]<j) j=residual[pre[i]][i]; 65 i=pre[i]; 66 } 67 min_flow=j; 68 } 69 void update() 70 { 71 int i=t; 72 if(pre[i]==0) return; 73 while(i!=s) 74 { 75 if(edge[pre[i]][i].c-edge[pre[i]][i].f>0) 76 edge[pre[i]][i].f+=min_flow; 77 else if(edge[i][pre[i]].f>0) 78 edge[pre[i]][i].f+=min_flow; 79 i=pre[i]; 80 } 81 } 82 void solve() 83 { 84 s=1;t=n; 85 max_flow=0; 86 while(true) 87 { 88 find();flow(); 89 max_flow+=min_flow; 90 if(min_flow>0) update(); 91 else return; 92 } 93 } 94 int main() 95 { 96 std::ios::sync_with_stdio(false); 97 int i,u,v,c; 98 while(scanf("%d %d",&m,&n)!=EOF) 99 { 100 memset(edge,0,sizeof(edge)); 101 for(i=0;i<m;i++) 102 { 103 scanf("%d %d %d",&u,&v,&c); 104 edge[u][v].c+=c; 105 } 106 solve(); 107 printf("%d\n",max_flow); 108 } 109 return 0; 110 }
POJ 1273 Drainage Ditches题解——S.B.S.
标签:
原文地址:http://www.cnblogs.com/AwesomeOrion/p/5536745.html