#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxx=1001;
int n,m,s,t,flow=0;
int c[maxx][maxx],f[maxx][maxx],a[maxx],pre[maxx];
int bfs(){
queue<int> q;
while(1){
memset(a,0,sizeof(a));
q.push(s);
a[s]=1e9;
while(!q.empty()){
int u=q.front();
q.pop();
for(int v=1;v<=n;++v)
if(!a[v] && f[u][v]<c[u][v]){
pre[v]=u;
q.push(v);
a[v]=min(a[u],c[u][v]-f[u][v]);
}
}
if(!a[t])break;
for(int i=t;i!=s;i=pre[i]){
f[pre[i]][i]+=a[t];
f[i][pre[i]]-=a[t];
}
flow+=a[t];
}
return flow;
}
int read(){
char x;
while((x=getchar())<'0' || x>'9');
int u=x-'0';
while((x=getchar())>='0' && x<='9')u=u*10+x-'0';
return u;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
int i,j,k;
n=read();
m=read();
s=read();
t=read();
while(m--){
i=read();
j=read();
k=read();
c[i][j]=k;
}
printf("%d",bfs());
return 0;
}
网络流EK算法(模板)
最新推荐文章于 2024-04-24 22:52:11 发布