-
Notifications
You must be signed in to change notification settings - Fork 21
/
DinicsFlow.cpp
79 lines (70 loc) · 2.1 KB
/
DinicsFlow.cpp
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
// Dinics Max Flow
// To put lower bound on edge capacities form a new graph G' with source s' and t'
// for each edge u->v in G with cap (low, high), replace it with
// s'->v with low
// v->t' with low
// u->v with high - low
const int inf = 0x3f3f3f3f;
struct edge {
int x, y, cap, flow;
};
struct DinicFlow {
vector <edge> e;
vector <int> cur, d;
vector < vector <int> > adj;
int n, source, sink;
DinicFlow() {}
DinicFlow(int v) {
n = v;
cur = vector <int> (n + 1);
d = vector <int> (n + 1);
adj = vector < vector <int> > (n + 1);
}
void addEdge(int from, int to, int cap) {
edge e1 = {from, to, cap, 0};
edge e2 = {to, from, 0, 0};
adj[from].push_back(e.size()); e.push_back(e1);
adj[to].push_back(e.size()); e.push_back(e2);
}
int bfs() {
queue <int> q;
for(int i = 0; i <= n; ++i) d[i] = -1;
q.push(source); d[source] = 0;
while(!q.empty() and d[sink] < 0) {
int x = q.front(); q.pop();
for(int i = 0; i < (int)adj[x].size(); ++i) {
int id = adj[x][i], y = e[id].y;
if(d[y] < 0 and e[id].flow < e[id].cap) {
q.push(y); d[y] = d[x] + 1;
}
}
}
return d[sink] >= 0;
}
int dfs(int x, int flow) {
if(!flow) return 0;
if(x == sink) return flow;
for(;cur[x] < (int)adj[x].size(); ++cur[x]) {
int id = adj[x][cur[x]], y = e[id].y;
if(d[y] != d[x] + 1) continue;
int pushed = dfs(y, min(flow, e[id].cap - e[id].flow));
if(pushed) {
e[id].flow += pushed;
e[id ^ 1].flow -= pushed;
return pushed;
}
}
return 0;
}
int maxFlow(int src, int snk) {
this->source = src; this->sink = snk;
int flow = 0;
while(bfs()) {
for(int i = 0; i <= n; ++i) cur[i] = 0;
while(int pushed = dfs(source, inf)) {
flow += pushed;
}
}
return flow;
}
};