2025-10-28 2025-10-28 WARNING 题目链接 $ code $ 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394#include<bits/stdc++.h>#define ll long long#define ma 114514using namespace std;const ll inf = 1e18;//------------------------------------------ll read() { ll x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f;}//------------------------------------------ll n, m;ll st, en;//------------------------------------------ll head[ma], ver[ma], nxt[ma], edge[ma], tot = 1;void add(ll x, ll y, ll z) { ver[++tot] = y, edge[tot] = z; nxt[tot] = head[x], head[x] = tot;}void Add(ll x, ll y, ll z) { add(x, y, z), add(y, x, 0);}//------------------------------------------ll d[ma];ll now[ma];ll top = 0;bool bfs() { top++; memset(d, 0, sizeof(d)); d[st] = 1; queue<ll> q; q.push(st); now[st] = head[st]; while (q.size()) { ll x = q.front(); q.pop(); for (ll i = head[x];i;i = nxt[i]) { ll y = ver[i], z = edge[i]; if (z && !d[y]) { d[y] = d[x] + 1; now[y] = head[y]; q.push(y); if (y == en) return 1; } } } return 0;}ll dfs(ll x, ll sum) { if (x == en) return sum; ll res = sum; for (ll i = now[x];i;i = nxt[i]) { now[x] = i; ll y = ver[i], z = edge[i]; if (z && d[y] == d[x] + 1) { ll k = dfs(y, min(res, z)); if (!k) d[y] = 0; edge[i] -= k; edge[i ^ 1] += k; res -= k; if (!res) break; } } return sum - res;}ll dinic() { ll maxflow = 0, flow = 0; while (bfs()) { flow = dfs(st, inf); while (flow) { maxflow += flow; flow = dfs(st, inf); } } return maxflow;}//------------------------------------------int main() { n = read(), m = read(), st = read(), en = read(); for (ll i = 1;i <= m;i++) { ll x = read(), y = read(), z = read(); Add(x, y, z); } cout << dinic() << endl;} Next LCT模板