#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;
const int M=1e6+5;
int h[N],ne[M],e[M],w[M],tot=1;
void add(int from,int to,int wi) {
e[++tot]=to;
w[tot]=wi;
ne[tot]=h[from];
h[from]=tot;
}
int n,m,S,T;
int dep[N],gap[N];
void bfs() {
dep[T]=1;
queue<int>q;
q.push(T);
while(!q.empty()) {
int now=q.front();
gap[dep[now]]++;//统计深度的个数
q.pop();
for(int i=h[now];i;i=ne[i]) {
int to=e[i];
if(dep[to])continue;
q.push(to);
dep[to]=dep[now]+1;
}
}
}
int cur[N];
int dfs(int now,int sum) {
if(now==T)return sum;
int ans=0;
for(int i=cur[now];i&∑i=ne[i]) {
cur[now]=i;
int to=e[i];
if(w[i]>0&&dep[now]==dep[to]+1) {
int k=dfs(to,min(sum,w[i]));
ans+=k;
sum-=k;
w[i]-=k;
w[i^1]+=k;
}
}
if(sum==0)return ans;
//流不完
--gap[dep[now]];
if(gap[dep[now]]==0)dep[S]=n+1;
dep[now]++;
gap[dep[now]]++;
return ans;
}
int ISAP() {
int ans=0;
bfs();
while(dep[S]<=n) {
memcpy(cur,h,sizeof(h));
ans+=dfs(S,1e9);
}
return ans;
}
int main() {
cin>>n>>m>>S>>T;
while(m--) {
int x,y,wi;
cin>>x>>y>>wi;
add(x,y,wi);
add(y,x,0);
}
cout<<ISAP()<<endl;
return 0;
}
标签:int,sum,tot,dep,ans,now,ISAP
From: https://www.cnblogs.com/basicecho/p/16978134.html