标签:hose line eof poj tran names input 分层图 which
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
网络流Dinic模板
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 #include <queue> 6 #include <string> 7 #include <cstring> 8 using namespace std; 9 const int inf = 0x3f3f3f3f; 10 const int maxn = 220; 11 int c[maxn][maxn]; 12 int dep[maxn]; 13 int n,m; 14 void pt() 15 { 16 for (int i=1;i<=n;++i){ 17 for (int j=1;j<=n;++j) 18 printf("%d ",c[i][j]); 19 printf("\n"); 20 } 21 printf("==================\n"); 22 } 23 int bfs (int s,int t) 24 { 25 memset(dep,-1,sizeof dep); 26 queue<int> q; 27 while (!q.empty()) q.pop(); 28 dep[s] = 0; 29 q.push(s); 30 while (!q.empty()){ 31 int u = q.front(); 32 q.pop(); 33 for (int v=1;v<=n;++v){ 34 if (c[u][v]>0&&dep[v]==-1){//能到达该节点的条件是这条边有流量,而且这个点没有被访问 35 dep[v] = dep[u]+1; 36 q.push(v); 37 } 38 } 39 } 40 return dep[t]!=-1; 41 } 42 int dfs (int u,int mi,int t) 43 { 44 if (u==t) 45 return mi; 46 int tmp; 47 for (int v=1;v<=n;++v){ 48 if (c[u][v]>0&&dep[v]==dep[u]+1&&(tmp=dfs(v,min(mi,c[u][v]),t))){//下一节点的深度是当前节点+1 49 c[u][v]-=tmp; 50 c[v][u]+=tmp; 51 return tmp; 52 } 53 } 54 return 0;//别忘写返回0!!! 55 } 56 int dinic () 57 { 58 int ans = 0; 59 int tmp; 60 while (bfs(1,n)){//每次按照深度建立分层图,这样每次dfs的时候下一节点的深度是当前节点+1 61 while (1){ 62 tmp = dfs(1,inf,n); 63 //printf("%d\n",tmp); 64 if (tmp==0) 65 break; 66 //pt(); 67 ans+=tmp; 68 } 69 } 70 return ans; 71 } 72 int main() 73 { 74 //freopen("de.txt","r",stdin); 75 while (~scanf("%d%d",&m,&n)){ 76 memset(c,0,sizeof c); 77 for (int i=0;i<m;++i){ 78 int u,v,cap; 79 scanf("%d%d%d",&u,&v,&cap); 80 c[u][v]+=cap; 81 } 82 printf("%d\n",dinic()); 83 } 84 return 0; 85 }
POJ 1273 Drainage Ditches (网络流Dinic模板)
标签:hose line eof poj tran names input 分层图 which
原文地址:http://www.cnblogs.com/agenthtb/p/7450555.html