标签:
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<stdio.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<string> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 using namespace std; 10 int c[251]; // 分层图 11 int a[251][251]; //邻接矩阵 12 int n,m,ans; 13 inline int min(int a,int b) {return a<b?a:b;} 14 int bfs() { 15 memset(c,0xff,sizeof(c)); 16 c[1]=0; 17 queue<int> q; 18 q.push(1); 19 while(!q.empty()) { 20 int top=q.front(); 21 q.pop(); 22 for (int i=1;i<=n;++i) { 23 if(c[i]<0&&a[top][i]>0) { 24 c[i]=c[top]+1; 25 q.push(i); 26 } 27 } 28 } 29 if(c[n]>0) return 1; 30 else return 0; 31 } 32 int dfs(int x,int low) { 33 if(x==n) return low; 34 for (int i=1,k;i<=n;++i) 35 if(a[x][i]>0&&c[i]==c[x]+1&&(k=dfs(i,min(low,a[x][i])))) { 36 a[x][i]-=k; 37 a[i][x]+=k; 38 return k; 39 } 40 return 0; 41 } 42 int main() { 43 while(~scanf("%d%d",&m,&n)) { 44 memset(a,0,sizeof(a)); 45 for (int i=1;i<=m;++i) { 46 int aa,bb,cc; 47 scanf("%d%d%d",&aa,&bb,&cc); 48 a[aa][bb]+=cc; 49 } 50 ans=0; 51 int t; 52 while(bfs()) { 53 while(t=dfs(1,0x7fffff)) ans+=t; 54 } 55 printf("%d\n",ans); 56 } 57 }
[POJ 1273] Drainage Ditches & 最大流Dinic模板
标签:
原文地址:http://www.cnblogs.com/TonyNeal/p/poj1273.html