forked from t3nsor/codebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edmonds-karp.cpp
56 lines (48 loc) · 1.25 KB
/
edmonds-karp.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
/* Implementation of the Ford-Fulkerson method that uses BFS
* for finding augmenting paths. O(VE^2)
*/
int n;
// at any moment capacity stores the residual capacity
vector<vector<int>> capacity; // n x n matrix
// graph is directed but we need to store edges
// in both directions
vector<vector<int>> adj;
// TODO: flow through each edge is not stored,
// add another matrix to store it
int bfs(int s, int t, vector<int>& parent) {
fill(parent.begin(), parent.end(), -1);
parent[s] = -2;
queue<pair<int, int>> q;
q.push({s, INF});
while (!q.empty()) {
int cur = q.front().first;
int flow = q.front().second;
q.pop();
for (int next : adj[cur]) {
if (parent[next] == -1 && capacity[cur][next]) {
parent[next] = cur;
int new_flow = min(flow, capacity[cur][next]);
if (next == t)
return new_flow;
q.push({next, new_flow});
}
}
}
return 0;
}
int maxflow(int s, int t) {
int flow = 0;
vector<int> parent(n);
int new_flow;
while (new_flow = bfs(s, t, parent)) {
flow += new_flow;
int cur = t;
while (cur != s) {
int prev = parent[cur];
capacity[prev][cur] -= new_flow;
capacity[cur][prev] += new_flow;
cur = prev;
}
}
return flow;
}