Edmond_karp算法:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int maxn = 200 + 5;
const int inf = 0x7f7f7f7f;
int pre[maxn], c[maxn][maxn], flow[maxn];
int ans;
int bfs(int s, int t)
{
queue<int> q;
memset(pre, -1, sizeof(pre));
memset(flow, 0, sizeof(flow));
q.push(s);
flow[s] = inf;
while(!q.empty())
{
int u = q.front();
q.pop();
if (u == t) break;
for (int i = 1; i <= t; i++)
{
if (!flow[i] && c[u][i] > 0)
{
pre[i] = u;
q.push(i);
flow[i] = min(flow[u], c[u][i]);//s-v路径上的最小残量
}
}
}
if (pre[t] == -1) return -1;
return 1;
}
int Edmond_karp(int s, int t)
{
int max_flow = 0;
while (bfs(s, t) != -1)//寻找增广路径
{
max_flow += flow[t];
int k = t;
while (pre[k] != -1)
{
c[pre[k]][k] -= flow[t];//更新正向流量
c[k][pre[k]] += flow[t];//更新反向流量
k = pre[k];
}
}
return max_flow;
}
int main()
{
//freopen("input.txt", "r", stdin);
int n, m;
while (cin >> n >> m)
{
memset(c, 0, sizeof(c));//初始化
for (int i = 0; i < n; i++)
{
int u, v, x;
scanf("%d%d%d", &u, &v, &x);
c[u][v] += x;
}
printf("%d\n", Edmond_karp(1, m));
}
return 0;
}