-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDinic.java
More file actions
94 lines (87 loc) · 1.73 KB
/
Dinic.java
File metadata and controls
94 lines (87 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
public class Dinic{
class edge{
int v1, v2, cap, flow;
edge rev;
public edge(int a, int b, int c, int d) {
v1= a;
v2= b;
cap= c;
flow= d;
}
}
ArrayDeque<Integer> q;
ArrayList<edge>[] adj;
int n, s, t, oo= 123456789;
boolean [] blocked;
int [] dist;
public Dinic(int a) {
n= a;
s= n++;
t= n++;
blocked= new boolean[n];
dist= new int[n];
q= new ArrayDeque<>();
adj= new ArrayList[n];
for (int i = 0; i < adj.length; i++) {
adj[i]= new ArrayList<>();
}
}
public void add(int a, int b, int cap, int flow) {
edge e= new edge(a, b, cap, flow);
edge rev= new edge(b,a,0,0);
adj[a].add(rev.rev=e);
adj[b].add(e.rev= rev);
}
public boolean bfs() {
q.clear();
Arrays.fill(dist, -1);
dist[t] = 0;
q.add(t);
while (!q.isEmpty()) {
int cur = q.poll();
if (cur == s) return true;
for (edge e : adj[cur]) {
if (e.rev.cap > e.rev.flow && dist[e.v2] == -1) {
dist[e.v2] = dist[cur] + 1;
q.add(e.v2);
}
}
}
return dist[s] != -1;
}
public int dfs(int on, int min) {
if (on == t) return min;
int flow = 0;
for (edge e : adj[on]) {
int cur = 0;
if (!blocked[e.v2] && dist[e.v2] == dist[on] - 1 && e.cap - e.flow > 0) {
cur = dfs(e.v2, Math.min(min - flow, e.cap - e.flow));
e.flow += cur;
e.rev.flow = -e.flow;
flow += cur;
}
if (flow == min) return flow;
}
blocked[on] = flow != min;
return flow;
}
public int flow() {
clear();
int ret = 0;
while (bfs()) {
Arrays.fill(blocked, false);
ret += dfs(s, oo);
}
return ret;
}
public void clear() {
for (ArrayList<edge> edges : adj) {
for (edge e : edges) {
e.flow = 0;
}
}
}
}