题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532
感觉题意不清楚,不知道是不是个人英语水平问题。本来还以为需要维护入度和出度来找源点和汇点呢,看讨论才知道1就是起点,m就是汇点。。好想把代码写的工程化一点。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std; const int INF = 0x3f3f3f3f;
int n, m;
int cap[][], flow[][], res[], pre[];
queue<int>que; int Edmonds_Karp()
{
while(!que.empty())
{
que.pop();
}
int flow_sum = ;
while(true)
{
memset(res, , sizeof(res));
res[] = INF;
que.push();
while(!que.empty())
{
int u = que.front();
que.pop();
for(int v = ; v <= m; v++)
{
if(!res[v] && cap[u][v] > flow[u][v])
{
pre[v] = u;
que.push(v);
res[v] = min(res[u], cap[u][v] - flow[u][v]);
}
}
}
if(res[m] == )break;
for(int u = m; u != ; u = pre[u])
{
flow[pre[u]][u] += res[m];
flow[u][pre[u]] -= res[m];
}
flow_sum += res[m];
}
return flow_sum;
} int main()
{
int u, v, w;
while(scanf("%d %d", &n, &m) != EOF)
{
memset(cap, , sizeof(cap));
memset(flow, , sizeof(flow));
for(int i = ; i < n; i++)
{
scanf("%d %d %d", &u, &v, &w);
cap[u][v] += w;
}
printf("%d\n", Edmonds_Karp());
}
return ;
}