-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEdmondsKarp.cpp
55 lines (44 loc) · 1.02 KB
/
EdmondsKarp.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
// EdmondsKarp Algorithm
// MAXFLOW
// O(V * E^2)
#define ll long long int
const ll MAXN = 205;
const ll INF = 1e8;
ll n;
ll capacity[MAXN][MAXN];
vector<ll> graph[MAXN];
ll parent[MAXN];
ll bfs(ll s,ll t){
FOR(i,0,n) parent[i] = -1;
queue<pair<ll,ll>> q;
q.push({s,INF});
while(!q.empty()){
ll u = q.front().first;
ll flow = q.front().second;
q.pop();
for(ll v:graph[u]){
if(parent[v] == -1 && capacity[u][v]){
parent[v] = u;
ll new_flow = min(flow,capacity[u][v]);
if(v == t) return new_flow;
q.push({v,new_flow});
}
}
}
return 0;
}
ll maxflow(ll s,ll t){
ll flow = 0;
ll new_flow;
while(new_flow = bfs(s,t)){
flow += new_flow;
ll curr = t;
while(curr != s){
ll prev = parent[curr];
capacity[prev][curr] -= new_flow;
capacity[curr][prev] += new_flow;
curr = prev;
}
}
return flow;
}