Link
P3376 【模板】网络最大流
Solve
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=205,maxe=10005,INF=1<<30;
int N,M,S,T,lnk[maxn],nxt[maxe],son[maxe],w[maxe],cnt=1,Min_x[maxn],pre[maxn],vis[maxn],Q[maxn];
LL Ans;
inline void add_e(int x,int y,int z){
son[++cnt]=y;w[cnt]=z;nxt[cnt]=lnk[x];lnk[x]=cnt;
}
inline int read(){
int ret=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
while(ch<='9'&&ch>='0')ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
bool bfs(){
memset(vis,0,sizeof vis);
int hed=0,til=1;Q[til]=S;vis[S]=1;
Min_x[S]=INF;
while(hed!=til){
hed++;
for(int j=lnk[Q[hed]];j;j=nxt[j]){
if(w[j]){
if(vis[son[j]])continue;
Min_x[son[j]]=min(Min_x[Q[hed]],w[j]);
pre[son[j]]=j;
Q[++til]=son[j];vis[son[j]]=1;
if(son[j]==T)return 1;
}
}
}
return 0;
}
void update(){
int x=T;
while(x!=S){
int i=pre[x];
w[i]-=Min_x[T];
w[i^1]+=Min_x[T];
x=son[i^1];
}
Ans+=(LL)Min_x[T];
}
int main(){
freopen("P3376.in","r",stdin);
freopen("P3376.out","w",stdout);
N=read();M=read();S=read(),T=read();
for(int i=1;i<=M;i++){
int x=read(),y=read(),z=read();
add_e(x,y,z);
add_e(y,x,0);
}
while(bfs())
update();
printf("%lld
",Ans);
return 0;
}