-
Notifications
You must be signed in to change notification settings - Fork 13
/
edmonds_karp_maxflow.cpp
71 lines (61 loc) · 1.13 KB
/
edmonds_karp_maxflow.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
#define INF 1000000
vector< int >par;
vector< vector<int> >adj,cap;
int bfs(int src, int dest)
{
fill(par.begin(),par.end(),-1);
par[src]=-2;
queue< pair<int,int> >q;
q.push({src,INF});
int cur,val,nval;
while(!q.empty())
{
cur = q.front().first;
val = q.front().second;
q.pop();
// cout<<"curr: "<<cur<<"\n";
for(auto x:adj[cur])
{
if(par[x]==-1 && cap[cur][x]>0)
{
par[x]=cur;
nval = min(val, cap[cur][x]);
if(x==dest) return nval;
q.push({x,nval});
}
}
}
return 0;
}
int maxflow(int src, int dest)
{
int flow = 0;
int newflow;
while(newflow = bfs(src,dest))
{
// cout<<newflow<<"\t newflow \n";
flow += newflow;
for(int i=dest; i!=src; i=par[i])
{
// cout<<"par:"<<par[i]<<" idx:"<<i<<"\n";
cap[par[i]][i] -= newflow;
cap[i][par[i]] += newflow;
}
}
return flow;
}
void input(int ver, int edges)
{
par.resize(ver+1);
adj.resize(ver+1);
cap.resize(ver+1);
for(int i=1;i<=ver;i++) cap[i].resize(ver+1);
int from,to,weight;
for(int i=1; i<=edges; i++)
{
cin>>from>>to>>weight;
cap[from][to] = weight;
adj[from].push_back(to);
adj[to].push_back(from);
}
}