summaryrefslogtreecommitdiff
path: root/content/graph/dinicScaling.cpp
diff options
context:
space:
mode:
authormzuenni <michi.zuendorf@gmail.com>2024-09-07 23:41:49 +0200
committermzuenni <michi.zuendorf@gmail.com>2024-09-07 23:41:49 +0200
commit9e9c033aa41f786f494cadea43563f83f3e951a3 (patch)
tree46413539a25f553d8b1b0d5cab7d38dcf0305759 /content/graph/dinicScaling.cpp
parentacdc3eaf1035fd840ee5b522f98bcae6d28464e2 (diff)
insert divsum
Diffstat (limited to 'content/graph/dinicScaling.cpp')
-rw-r--r--content/graph/dinicScaling.cpp21
1 files changed, 13 insertions, 8 deletions
diff --git a/content/graph/dinicScaling.cpp b/content/graph/dinicScaling.cpp
index f4e833a..0974b78 100644
--- a/content/graph/dinicScaling.cpp
+++ b/content/graph/dinicScaling.cpp
@@ -26,17 +26,18 @@ bool bfs(ll lim) {
return dist[t] >= 0;
}
-bool dfs(int v, ll flow) {
- if (v == t) return true;
+ll dfs(int v, ll flow) {
+ if (v == t || flow == 0) return flow;
for (; pt[v] < sz(adj[v]); pt[v]++) {
Edge& e = adj[v][pt[v]];
if (dist[e.to] != dist[v] + 1) continue;
- if (e.c - e.f >= flow && dfs(e.to, flow)) {
- e.f += flow;
- adj[e.to][e.rev].f -= flow;
- return true;
+ ll cur = dfs(e.to, min(e.c - e.f, flow));
+ if (cur > 0) {
+ e.f += cur;
+ adj[e.to][e.rev].f -= cur;
+ return cur;
}}
- return false;
+ return 0;
}
ll maxFlow(int source, int target) {
@@ -45,7 +46,11 @@ ll maxFlow(int source, int target) {
for (ll lim = (1LL << 62); lim >= 1; lim /= 2) {
while (bfs(lim)) {
pt.assign(sz(adj), 0);
- while (dfs(s, lim)) flow += lim;
+ ll cur;
+ do {
+ cur = dfs(s, lim);
+ flow += cur;
+ } while (cur > 0);
}}
return flow;
}