HDU ACM 1532 Drainage Ditches(网络流---增广路)
http://acm.hdu.edu.cn/showproblem.php?pid=1532
题意:找到从起点到终点的最大流.
BFS()---- 找到一条增广路
GetFlow()---- 找到该增广路的流量,即流量最小的边,减去流量,再在增广路的个边上补反边
1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 const int MAX = 200 + 10; 5 const int INF = 0x3fffffff; 6 7 bool used[MAX]; 8 int front[MAX]; 9 int map[MAX][MAX]; 10 int n; 11 int m; 12 int sumflow; 13 bool BFS(int s){//用广搜找增广路 14 queue <int> q; 15 memset(used,0,sizeof(used)); 16 q.push(s); 17 front[s] = 1; 18 used[s] = 1; 19 while(!q.empty()){ 20 int mid = q.front(); 21 q.pop(); 22 int i; 23 for(i=1;i<=n;i++){ 24 if(!used[i] && map[mid][i] > 0){ 25 q.push(i); 26 used[i] = 1; 27 front[i] = mid; 28 if(i == n){ 29 return 1; 30 } 31 } 32 } 33 } 34 return 0; 35 } 36 int GetFlow(){ 37 int min = INF; 38 int i = n; 39 while(front[i] != i){ 40 if(map[front[i]][i] < min){ 41 min = map[front[i]][i]; 42 } 43 i = front[i]; 44 } 45 46 i = n; 47 while(front[i] != i){ 48 map[front[i]][i] -= min; 49 map[i][front[i]] += min; 50 i = front[i]; 51 } 52 return min; 53 } 54 int CalSum(){ 55 while(BFS(1)){ 56 sumflow += GetFlow(); 57 } 58 return sumflow; 59 } 60 61 62 63 int main(){ 64 while(cin>>m>>n){ 65 sumflow = 0; 66 memset(map,0,sizeof(map)); 67 int i; 68 for(i=0;i<m;i++){ 69 int a,b,c; 70 cin>>a>>b>>c; 71 map[a][b] += c;//注意这里 map[a][b] = c 会错 72 } 73 cout<<CalSum()<<endl; 74 } 75 return 0; 76 }