forked from beet-aizu/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfordfulkerson.cpp
79 lines (70 loc) · 1.45 KB
/
fordfulkerson.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
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
struct Fordfulkerson{
const int INF = 1 << 28;
struct edge{
int to,cap,rev;
edge(){}
edge(int to,int cap,int rev):to(to),cap(cap),rev(rev){}
};
int n;
vector<vector<edge> > G;
vector<int> used;
Fordfulkerson(){}
Fordfulkerson(int sz):n(sz),G(n),used(n){}
void add_edge(int from,int to,int cap){
G[from].push_back(edge(to,cap,G[to].size()));
// undirected
//G[to].push_back(edge(from,cap,G[from].size()-1));
// directed
G[to].push_back(edge(from,0,G[from].size()-1));
}
int dfs(int v,int t,int f){
if(v==t) return f;
used[v]=true;
for(int i=0;i<(int)G[v].size();i++){
edge &e = G[v][i];
if(!used[e.to] && e.cap > 0 ){
int d=dfs(e.to,t,min(f,e.cap));
if(d>0){
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int flow(int s,int t,int lim){
int fl=0;
for(;;){
fill(used.begin(),used.end(),0);
int f=dfs(s,t,lim);
if(f==0) return fl;
fl+=f;
lim-=f;
}
}
int flow(int s,int t){
return flow(s,t,INF);
}
};
//END CUT HERE
signed main(){
int V,E;
cin>>V>>E;
Fordfulkerson flow(V);
for(int i=0;i<E;i++){
int u,v,c;
cin>>u>>v>>c;
flow.add_edge(u,v,c);
}
cout<<flow.flow(0,V-1)<<endl;
return 0;
}
/*
verified on 2018/01/19
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A&lang=jp
*/