-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdinic.cpp
More file actions
62 lines (55 loc) · 1.01 KB
/
dinic.cpp
File metadata and controls
62 lines (55 loc) · 1.01 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
const int N = 500;
const int M = 10000;
int n, m;
struct Edge {
int v, u, c, f = 0;
Edge() {}
Edge(int v, int u, int c) : v(v), u(u), c(c) {}
int cf() { return c - f; }
} e[2 * M];
vector<int> g[N];
int s, t;
int df;
int dist[N];
bool bfs() {
queue<int> q;
q.push(s);
fill(dist, dist + n, -1);
dist[s] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
for (int i : g[v]) {
if (e[i].cf() >= df && dist[e[i].u] == -1) {
dist[e[i].u] = dist[v] + 1;
q.push(e[i].u);
}
}
}
return dist[t] != -1;
}
int ptr[N];
bool dfs(int v) {
if (v == t) {
return true;
}
for (; ptr[v] < g[v].size(); ++ptr[v]) {
int i = g[v][ptr[v]];
if (dist[v] + 1 == dist[e[i].u] && e[i].cf() >= df && dfs(e[i].u)) {
e[i].f += df;
e[i ^ 1].f -= df;
return true;
}
}
return false;
}
void dinic() {
const int K = 30;
for (df = 1 << K; df >= 1; df >>= 1) {
while (bfs()) {
fill(ptr, ptr + N, 0);
while (dfs(s)) {
}
}
}
}