题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532
感觉题意不清楚,不知道是不是个人英语水平问题。本来还以为需要维护入度和出度来找源点和汇点呢,看讨论才知道1就是起点,m就是汇点。。好想把代码写的工程化一点。
1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 7 const int INF = 0x3f3f3f3f; 8 int n, m; 9 int cap[210][210], flow[210][210], res[210], pre[210]; 10 queue<int>que; 11 12 int Edmonds_Karp() 13 { 14 while(!que.empty()) 15 { 16 que.pop(); 17 } 18 int flow_sum = 0; 19 while(true) 20 { 21 memset(res, 0, sizeof(res)); 22 res[1] = INF; 23 que.push(1); 24 while(!que.empty()) 25 { 26 int u = que.front(); 27 que.pop(); 28 for(int v = 1; v <= m; v++) 29 { 30 if(!res[v] && cap[u][v] > flow[u][v]) 31 { 32 pre[v] = u; 33 que.push(v); 34 res[v] = min(res[u], cap[u][v] - flow[u][v]); 35 } 36 } 37 } 38 if(res[m] == 0)break; 39 for(int u = m; u != 1; u = pre[u]) 40 { 41 flow[pre[u]][u] += res[m]; 42 flow[u][pre[u]] -= res[m]; 43 } 44 flow_sum += res[m]; 45 } 46 return flow_sum; 47 } 48 49 int main() 50 { 51 int u, v, w; 52 while(scanf("%d %d", &n, &m) != EOF) 53 { 54 memset(cap, 0, sizeof(cap)); 55 memset(flow, 0, sizeof(flow)); 56 for(int i = 0; i < n; i++) 57 { 58 scanf("%d %d %d", &u, &v, &w); 59 cap[u][v] += w; 60 } 61 printf("%d ", Edmonds_Karp()); 62 } 63 return 0; 64 }