裸题,直接上模版。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
const int inf = 500000000;
const int maxn = 2200;
struct node
{
int u, v;//左右端点
int f; //flow 流量
};
node e[maxn];
int first[maxn], cc; //cc是结点编号
int next[maxn], p[maxn], a[maxn];
//加边
inline void add_edge(int u, int v, int f) //
{
//加入正向边
e[cc].u = u;
e[cc].v = v;
e[cc].f = f;
next[cc] = first[u];
first[u] = cc;
cc++;
//加入反向边
e[cc].u = v;
e[cc].v = u;
e[cc].f = 0; //是0
next[cc] = first[v];
first[v] = cc;
cc++;
}
//最大流的EK算法
int EK(int s, int t)
{
queue<int> q;
int f = 0; //结果
while(1)
{
memset(a, 0, sizeof(a));
memset(p, -1, sizeof(p));
q.push(s);
a[s] = inf;
while( !q.empty() )
{
int u = q.front();
q.pop();
int i;
for(i = first[u]; i != -1; i = next[i] )
{
if( !a[e[i].v] && e[i].f )
{
p[ e[i].v ] = i;
a[ e[i].v ] = min(a[u], e[i].f );
q.push(e[i].v);
}
}
}
if(a[t] == 0) break;
int u;
for(u = t; u != s; u = e[p[u]].u)
{
e[p[u]].f -= a[t];
e[p[u] ^ 1].f += a[t];
}
f += a[t];
}
return f;
}
int main()
{
int N, M;
while( scanf("%d%d", &N, &M) != EOF)
{
cc = 0;
memset(first, -1, sizeof first);
memset(next, -1, sizeof next);
int u, v, f;
while(N--)
{
scanf("%d%d%d",&u,&v,&f);
add_edge(u, v, f);
}
int ans = EK(1, M);
printf("%d\n", ans);
}
return 0;
}