-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum_flow.cpp
More file actions
53 lines (51 loc) · 1.07 KB
/
Copy pathmaximum_flow.cpp
File metadata and controls
53 lines (51 loc) · 1.07 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
class Flow {
public:
const static int N = 500+ 2;
vector< int > adj[N];
vector<int> parent;
vector< vector<int> > cap;
int source, sink;
Flow(int s, int t) {
source = s; sink = t;
parent.assign(N, 0);
cap.assign(N, vector<int> (N, 0));
}
void add_edge(int a, int b, int w) {
cap[a][b] += w;
adj[a].push_back(b);
adj[b].push_back(a);
}
int find_augmenting_path() {
fill(parent.begin(), parent.end(), -1);
queue< pair<int,int > > q;
q.push({source, INT_MAX});
while(q.size()) {
int cur = q.front().first;
int flow = q.front().second;
q.pop();
for(auto nxt: adj[cur]) {
if(!cap[cur][nxt] || parent[nxt] != -1) continue;
int new_flow = min(flow, cap[cur][nxt]);
q.push({nxt, new_flow});
parent[nxt] = cur;
if(nxt == sink) return new_flow;
}
}
return 0;
}
int get_max_flow() {
int flow;
int ans = 0;
while(flow = find_augmenting_path()) {
int cur = sink;
ans += flow;
while(cur != source) {
int p = parent[cur];
cap[p][cur] -= flow;
cap[cur][p] += flow;
cur = p;
}
}
return ans;
}
};