@@ -19,7 +19,7 @@ void add_edge(ll u, ll v, ll c) {
19
19
}
20
20
}
21
21
22
- bool bfs (ll lim ) {
22
+ bool bfs () {
23
23
fill (dist.begin (), dist.end (),INF);
24
24
dist[src] = 0 ;
25
25
q.push (src);
@@ -28,7 +28,7 @@ bool bfs(ll lim) {
28
28
ll u = q.front (); q.pop ();
29
29
30
30
for (ll to : adj[u]) {
31
- if (dist[to] == INF and capacity [u][to] - flow [u][to] >= lim ) {
31
+ if (dist[to] == INF and flow [u][to] < capacity [u][to]) {
32
32
dist[to] = dist[u] + 1 ;
33
33
q.push (to);
34
34
}
@@ -39,38 +39,34 @@ bool bfs(ll lim) {
39
39
return dist[dest] != INF;
40
40
}
41
41
42
- bool dfs (ll u, ll curr_flow) {
42
+ ll dfs (ll u, ll curr_flow) {
43
43
if (curr_flow == 0 or u == dest) {
44
44
return curr_flow;
45
45
}
46
46
47
47
for (; pt[u] < adj[u].size (); pt[u]++) {
48
48
ll to = adj[u][pt[u]];
49
49
50
- if (dist[to] == dist[u] + 1 and capacity[u][to] - flow[u][to] >= curr_flow ) {
51
- bool pushed = dfs (to, curr_flow);
52
- if (pushed) {
53
- flow[u][to] += curr_flow ;
54
- flow[to][u] -= curr_flow ;
55
- return true ;
50
+ if (dist[to] == dist[u] + 1 ) {
51
+ ll pushed = dfs (to, min ( curr_flow, capacity[u][to] - flow[u][to]) );
52
+ if (pushed > 0 ) {
53
+ flow[u][to] += pushed ;
54
+ flow[to][u] -= pushed ;
55
+ return pushed ;
56
56
}
57
57
}
58
58
}
59
59
60
- return false ;
60
+ return 0 ;
61
61
}
62
62
63
63
ll dinic () {
64
- ll flow = 0 , lim = (1 << 30 );
65
- while (lim >= 1 ) {
66
- if (!bfs (lim)) {
67
- lim >>= 1 ;
68
- continue ;
69
- }
64
+ ll flow = 0 ;
65
+ while (bfs ()) {
70
66
fill (pt.begin (), pt.end (),0 );
71
67
72
- while (dfs (src, lim )) {
73
- flow += lim ;
68
+ while (ll pushed = dfs (src, INF )) {
69
+ flow += pushed ;
74
70
}
75
71
}
76
72
return flow;
0 commit comments