Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfaae53
commit 533effd
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++) | ||
#define snuke(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++) | ||
|
||
typedef int F; | ||
#define F_INF (1<<29) | ||
#define MAXV 1000 | ||
#define MAXE 1000 // E*2! | ||
|
||
F cap[MAXE],flow[MAXE]; | ||
int to[MAXE],_prev[MAXE],last[MAXV],used[MAXV],level[MAXV]; | ||
|
||
struct MaxFlow{ | ||
int V,E; | ||
|
||
MaxFlow(int n){ | ||
int i; | ||
V = n; E = 0; | ||
REP(i,V) last[i] = -1; | ||
} | ||
|
||
void add_edge(int x, int y, F f){ | ||
cap[E] = f; flow[E] = 0; to[E] = y; _prev[E] = last[x]; last[x] = E; E++; | ||
cap[E] = 0; flow[E] = 0; to[E] = x; _prev[E] = last[y]; last[y] = E; E++; | ||
} | ||
|
||
bool bfs(int s, int t){ | ||
int i; | ||
REP(i,V) level[i] = -1; | ||
queue <int> q; | ||
q.push(s); level[s] = 0; | ||
while(!q.empty()){ | ||
int x = q.front(); q.pop(); | ||
for(i=last[x];i>=0;i=_prev[i]) if(level[to[i]] == -1 && cap[i] > flow[i]) {q.push(to[i]); level[to[i]] = level[x] + 1;} | ||
} | ||
return (level[t] != -1); | ||
} | ||
|
||
F dfs(int v, int t, F f){ | ||
int i; | ||
if(v == t) return f; | ||
for(i=used[v];i>=0;used[v]=i=_prev[i]) if(level[to[i]] > level[v] && cap[i] > flow[i]){ | ||
F tmp = dfs(to[i],t,min(f,cap[i]-flow[i])); | ||
if(tmp > 0) {flow[i] += tmp; flow[i^1] -= tmp; return tmp;} | ||
} | ||
return 0; | ||
} | ||
|
||
F maxflow(int s, int t){ | ||
int i; | ||
while(bfs(s,t)){ | ||
REP(i,V) used[i] = last[i]; | ||
while(dfs(s,t,F_INF) != 0); | ||
} | ||
F ans = 0; | ||
for(i=last[s];i>=0;i=_prev[i]) ans += flow[i]; | ||
return ans; | ||
} | ||
|
||
}; |