Skip to content

Commit b552ece

Browse files
committed
Scaling Dinic
1 parent bf5a76f commit b552ece

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

content/graph/Dinic.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* Date: 2019-04-26
44
* License: CC0
55
* Source: https://cp-algorithms.com/graph/dinic.html
6-
* Description: Flow algorithm with guaranteed complexity $O(V^2E)$.
7-
* $O(\sqrt{V}E)$ for bipartite graphs, $O(\min(V^{1/2}, E^{2/3})E))$ for unit graphs.
6+
* Description: Flow algorithm with complexity $O(VE\log U)$ where $U = \max |\text{cap}|$.
7+
* $O(\min(E^{1/2}, V^{2/3})E)$ if $U = 1$; $O(\sqrt{V}E)$ for bipartite matching.
88
* To obtain the actual flow, look at positive values only.
99
* Status: Tested on SPOJ FASTFLOW and SPOJ MATCHING
1010
*/
@@ -34,13 +34,13 @@ struct Dinic {
3434
}
3535
ll calc(int s, int t) {
3636
ll flow = 0; q[0] = s;
37-
do {
37+
rep(L,0,31) do { // 'int L=30' maybe faster for random data
3838
lvl = ptr = vi(sz(q));
3939
int qi = 0, qe = lvl[s] = 1;
4040
while (qi < qe && !lvl[t]) {
4141
int v = q[qi++];
4242
trav(e, adj[v])
43-
if (!lvl[e.to] && e.f < e.c)
43+
if (!lvl[e.to] && (e.c - e.f) >> (30 - L))
4444
q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
4545
}
4646
while (ll p = dfs(s, t, LLONG_MAX)) flow += p;

0 commit comments

Comments
 (0)