/* Name: poj 1273 Drainage Ditches Author: UnimenSun Date: 08/06/11 21:41 Description: 祼的最大流 */ /* 解题报告: 祼的最大流用Edmonds_Karp算法即可,处理图时注意有重边 注:该题的解法,即Edmonds_Karp函数是最大流问题的模板 */ #include <iostream> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int inf = 1000000000; const int maxn = 210; int cap[maxn][maxn]; int n, m; int s, t; int flow[maxn][maxn], d[maxn], parent[maxn], maxflow; void Edmonds_Karp() { maxflow = 0; memset(flow, 0, sizeof(flow)); memset(parent, 0, sizeof(parent)); while(true) { memset(d, 0, sizeof(d)); queue<int> que; d[s] = inf; que.push(s); while(!que.empty()) //BFS寻找增广路 { int u = que.front(); que.pop(); for(int v=1; v<=m; ++v) { if(!d[v] && cap[u][v]>flow[u][v]) //找到新结点v { parent[v] = u; //记录v的父亲,并加入FIFO队列 que.push(v); d[v] = min(d[u], cap[u][v]-flow[u][v]); //s-v路径上的最小残量 } } } if(0 == d[t]) //找不到,则当前流已经是最大流 break; for(int u=t; u!=s; u=parent[u]) //从汇点往回走 { flow[parent[u]][u] += d[t]; //更新正向流 flow[u][parent[u]] -= d[t]; //更新反向流 } maxflow += d[t]; //更新从s流出的总流量 } } int main() { while(cin>>n>>m) { memset(cap, 0, sizeof(cap)); while(n--) { int temps, tempe, c; cin>>temps>>tempe>>c; cap[temps][tempe] += c; //处理重边 } s = 1; t = m; Edmonds_Karp(); cout<<maxflow<<endl; } return 0; }