-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedmonds karp flow.cpp
80 lines (65 loc) · 1.44 KB
/
edmonds karp flow.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
80
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define SZ 802
int flow[SZ][SZ],cap[SZ][SZ],prv[SZ],f;
//initialize cap with 0 for each test case!
vector< vector<int> > L;
// L is the adj List of the graph
bool augmenting(int &N, int s, int t){
fill(prv,prv+N,-1);
queue<int> Q;
Q.push(s);
prv[s] = -2;
int aux;
while(!Q.empty()){
aux = Q.front();
Q.pop();
for(int i = 0;i<L[aux].size();++i)
if(flow[aux][L[aux][i]]<cap[aux][L[aux][i]] && prv[L[aux][i]]==-1){
prv[L[aux][i]] = aux;
if(L[aux][i]==t) goto found;
Q.push(L[aux][i]);
}
}
return false;
found:
int x = INT_MAX,cur = t,next;
while(cur!=s){
next = prv[cur];
x = min(x,cap[next][cur]-flow[next][cur]);
cur = next;
}
f += x;
cur = t;
while(cur!=s){
next = prv[cur];
flow[next][cur] += x;
flow[cur][next] -= x;
cur = next;
}
return true;
}
int main() {
int m,c,n,x,y;
cin>>n>>m;
L.assign(n+1,vi());
for(int i=0; i<m; ++i){
cin>>x>>y>>c;
L[x].pb(y);
L[y].pb(x);
cap[x][y] += c;
cap[y][x] += c;
}
int SRC = 1, SINK=N;
while(augmenting(n,SRC,SINK)){
// cerr<<"f = "<<f<<"\n";
}
cout<<f<<"\n";
return 0;
}