题意:
给n条边,从 fr 到 to,有一个容量 num。
然后问从1 流到 m,最多能流多少。
解析:
用EK求最大流,数据量比较小。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1
using namespace std;
const int maxn = 200 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);
int cap[maxn][maxn]; //容量
int a[maxn]; //残量
int flow[maxn][maxn]; //流量
int p[maxn]; //记录父节点
int n, m;
int EK(int s, int t)
{
queue<int> q;
memset(flow, 0, sizeof(flow));
int res = 0;
while (1)
{
memset(a, 0, sizeof(a));
a[s] = inf;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = s; v <= t; v++)
{
if (!a[v] && flow[u][v] < cap[u][v])
{
p[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 = p[u])
{
flow[p[u]][u] += a[t];
flow[u][p[u]] -= a[t];
}
res += a[t];
}
return res;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
while (scanf("%d%d", &n, &m) == 2)
{
memset(cap, 0, sizeof(cap));
for (int i = 1; i <= n; i++)
{
int fr, to, num;
scanf("%d%d%d", &fr, &to, &num);
cap[fr][to] += num;
}
int ans = EK(1, m);
printf("%d\n", ans);
}
return 0;
}