while (hh <= tt) { int ver = q[hh ++]; for (int i = h[ver]; ~i; i = ne[i]) { int to = e[i]; if (vis[to] || !w[i]) continue; dist[to] = min(dist[ver], w[i]); pre[to] = i; q[++ tt] = to, vis[to] = true; if (to == ed) returntrue; } } returnfalse; }
voidupdate() { int cur = ed; while (cur != st) { int nxt = pre[cur]; w[nxt] -= dist[ed], w[nxt ^ 1] += dist[ed]; cur = e[nxt ^ 1]; } ans += dist[ed]; }
signedmain() { // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); memset(h, -1, sizeof h); cin >> n >> m >> st >> ed; while (m --) { int a, b, c; cin >> a >> b >> c; if (edge[a][b]) w[edge[a][b]] += c; elseadd(a, b, c), add(b, a, 0), edge[a][b] = idx - 1; }
while (hh <= tt) { int ver = q[hh ++]; for (int i = h[ver]; ~i; i = ne[i]) { int to = e[i]; if (vis[to] || !w[i]) continue; dist[to] = min(dist[ver], w[i]); pre[to] = i; q[++ tt] = to, vis[to] = true; if (to == ed) returntrue; } } returnfalse; }
while (hh <= tt) { int ver = que[hh ++]; for (int i = h[ver]; ~i; i = ne[i]) { int to = e[i]; if (!dist[to] && w[i] > 0) dist[to] = dist[ver] + 1, que[++ tt] = to; } } return dist[ed]; }
intdfs(int ver, int flow) { if (ver == ed || !flow) return flow; for (int &i = cpy[ver]; ~i; i = ne[i]) { int to = e[i]; if (dist[to] == dist[ver] + 1 && w[i] > 0) { int temp = dfs(to, min(flow, w[i])); if (temp > 0) { w[i] -= temp, w[i ^ 1] += temp; return temp; } } } return0; }
intdinic() { int ans = 0, temp = 0; while (bfs()) { memcpy(cpy, h, sizeof cpy); while (temp = dfs(st, INF)) ans += temp; } return ans; }
signedmain() { // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); memset(h, -1, sizeof h), idx = -1; cin >> n >> m >> st >> ed; while (m --) { int a, b, c; cin >> a >> b >> c; add(a, b, c), add(b, a, 0); }