RT,最后一个点 WA,EK。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define int long long
const int N = 305, INF = 1e18;
int n, m, sp, tp;
int gra[N][N], flow[N], pre[N];
int bfs(int s, int t)
{
memset(flow, 0, sizeof flow);
memset(pre, 0x3f, sizeof pre);
flow[s] = INF;
pre[s] = 0;
queue<int> q;
q.push(s);
while (q.size())
{
int u = q.front();
q.pop();
for (int i = 1ll; i <= n; i++)
{
if (i != u && gra[u][i] > 0 && pre[i] == pre[0])
{
pre[i] = u;
q.push(i);
flow[i] = min(flow[u], gra[u][i]);
}
}
}
if (pre[t] == pre[0]) return -1ll;
return flow[t];
}
int maxflow(int s, int t)
{
int res = 0;
while (true)
{
int k = bfs(s, t);
if (k == -1ll) break;
int l = t;
while (l != s)
{
int fa = pre[l];
gra[fa][l] -= k;
gra[l][fa] += k;
l = fa;
}
res += k;
}
return res;
}
signed main()
{
scanf("%lld%lld", &n, &tp);
for (int i = 1ll; i <= n; i++)
{
int u, v, w;
scanf("%lld%lld%lld", &u, &v, &w);
gra[u][v] += w;
}
sp = 1ll;
printf("%lld\n", maxflow(sp, tp));
return 0;
}