Created
April 15, 2015 09:55
-
-
Save lan496/94008d32bf37eadac1d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Ford-Fulkerson's algorithm | |
struct edge{int to,cap,rev;}; | |
const int INF=1e9; | |
//g[e.to][e.rev] で逆辺を操作できる | |
void addEdge(vector<vector<edge> > &g,int from,int to,int cap){ | |
g[from].push_back((edge){to,cap,(int)g[to].size()}); | |
g[to].push_back((edge){from,0,(int)g[from].size()-1}); | |
} | |
int dfs(vector<vector<edge> > &g,vector<bool> &used,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(g,used,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 FordFulkerson(vector<vector<edge> > &g,int s,int t){ | |
int flow=0; | |
for(;;){ | |
vector<bool> used(g.size(),false); | |
int f=dfs(g,used,s,t,INF); | |
if(f==0) return flow; | |
flow+=f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment