HDU 1532 && POJ 1273 Drainage Ditches
首先要吐槽一下杭电,错误的代码都能过,而且这种情况不止一次了。
第一次最大流,纪念一下。
就是用的Edmonds-Karp,自己的模板,详细资料参考《算法导论》第26章最大流。
题意就是给N条有向路径,M个节点,1为源点,M为汇点,求最大流裸题。
直接上代码,代码中,最开始,我初始化和搜索中的范围都是[1,N],在HDU交,居然AC。后来在POJ WA地不明所以,才发现这个错误。
注意判重边。
1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 #include<stdlib.h> 5 #include<math.h> 6 #include<algorithm> 7 #include<string.h> 8 #include<string> 9 #include<vector> 10 #include<map> 11 #include<set> 12 #include<queue> 13 14 #define repA(p,q,i) for( int (i)=(p); (i)!=(q); ++(i) ) 15 #define repAE(p,q,i) for( int (i)=(p); (i)<=(q); ++(i) ) 16 #define repD(p,q,i) for( int (i)=(p); (i)!=(q); --(i) ) 17 #define repDE(p,q,i) for( int (i)=(p); (i)>=(q); --(i) ) 18 #define range 210 19 20 int n,m; 21 long long maxflow; 22 int Gf[range][range]; 23 int f[range]; 24 int confirm[range]; 25 26 void initial(); 27 bool BFS(); 28 29 int main() 30 { 31 while( scanf("%d%d",&n,&m) != EOF ) 32 { 33 initial(); 34 int si,ei,ci; 35 repA(0,n,i) 36 { 37 scanf("%d%d%d",&si,&ei,&ci); 38 if(si == ei) continue; 39 Gf[si][ei] += ci; 40 } 41 42 maxflow=0; 43 while( BFS() ) 44 { 45 int minx = 0x3f3f3f3f; 46 int st,et=m; 47 while( f[et] != et ) 48 { 49 //printf("Done!\n"); 50 st = f[et]; 51 minx = min(minx, Gf[st][et]); 52 et = st; 53 } 54 //while(1); 55 et=m; 56 57 while( f[et] != et ) 58 { 59 st = f[et]; 60 Gf[st][et] -= minx; 61 Gf[et][st] += minx; 62 et=st; 63 } 64 maxflow += minx; 65 } 66 cout<<maxflow<<endl; 67 } 68 return 0; 69 } 70 71 void initial() 72 { 73 repAE(1,m,i) 74 repAE(1,m,j) 75 Gf[i][j]=0; 76 repAE(1,m,i) 77 f[i]=i; 78 return ; 79 } 80 81 bool BFS() 82 { 83 repAE(1,m,i) 84 confirm[i]=true; 85 queue<int> gray; 86 gray.push(1); 87 confirm[1]=false; 88 89 int u; 90 while( !gray.empty() ) 91 { 92 u=gray.front(); 93 gray.pop(); 94 repAE(1,m,i) 95 { 96 if( confirm[i] && Gf[u][i] > 0 ) 97 { 98 gray.push(i); 99 confirm[i]=false; 100 f[i]=u; 101 if(i == m) return true; 102 } 103 } 104 105 } 106 return false; 107 }
To Be The Best Of Yourself