https://www.luogu.com.cn/problem/P3376
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=209,M=5009;
const ll inf=2e18;
int ver[M<<1],head[N],ne[M<<1],tot=1,n,m,st,en,d[N];
ll edge[M<<1],ans=0;
void add(int x,int y,ll z){
ver[++tot]=y,edge[tot]=z,ne[tot]=head[x],head[x]=tot;
}
int bfs(){
for(int i=1;i<=n;i++)
d[i]=0;
queue<int>q;
d[st]=1;
q.push(st);
while(q.size()){
int x=q.front();
q.pop();
for(int i=head[x];i;i=ne[i]){
int y=ver[i];
if(d[y]||!edge[i])
continue;
d[y]=d[x]+1;
q.push(y);
if(y==en)
return 1;
}
}
return 0;
}
ll dinic(int x,ll flow){
if(x==en)
return flow;
ll res=flow;
for(int i=head[x];i&&res;i=ne[i]){
int y=ver[i];
if(!edge[i]||d[y]!=d[x]+1)
continue;
ll tmp=dinic(y,min(edge[i],res));
if(!tmp)
d[y]=0;
res-=tmp;
edge[i]-=tmp;
edge[i^1]+=tmp;
}
return flow-res;
}
int main(){
scanf("%d%d%d%d",&n,&m,&st,&en);
for(int i=1;i<=m;i++){
int x,y;
ll z;
scanf("%d%d%lld",&x,&y,&z);
add(x,y,z),add(y,x,0);
}
while(bfs())
while(ll flow=dinic(st,inf))
ans+=flow;
printf("%lld",ans);
return 0;
}