题目链接:HDU 1532 Drainage Ditches 最大排水量
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
50
分析:最大流,EK算法。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 220
#define INF 0x3f3f3f3f
int ans, s, t, n;
int a[maxn], pre[maxn];
int flow[maxn][maxn];
int cap[maxn][maxn];
void Edmonds_Karp()
{
queue<int> q;
memset(flow, 0, sizeof(flow));
ans = 0;
while(1)
{
memset(a, 0, sizeof(a));
a[s] = INF;
q.push(s);
while(!q.empty()) //bfs找增广路径
{
int u = q.front();
q.pop();
for(int v = 1; v <= n; v++)
if(!a[v] && cap[u][v] > flow[u][v])
{
pre[v] = u;
q.push(v);
a[v] = min(a[u], cap[u][v]-flow[u][v]);
}
}
if(a[t] == 0) break;
for(int u = t; u != s; u = pre[u]) //改进网络流
{
flow[pre[u]][u] += a[t];
flow[u][pre[u]] -= a[t];
}
ans += a[t];
}
}
int main()
{
//freopen("hdu_1532.txt", "r", stdin);
int m, u, v, c;
while(~scanf("%d%d", &m, &n))
{
memset(cap, 0, sizeof(cap));
while(m--)
{
scanf("%d%d%d", &u, &v, &c);
cap[u][v] += c;
}
s = 1, t = n;
Edmonds_Karp();
printf("%d\n", ans);
}
return 0;
}
HDU 1532 Drainage Ditches 最大排水量 网络最大流 Edmonds_Karp算法
原文:http://blog.csdn.net/u011439796/article/details/39485911