最大流模板
#include<bits/stdc++.h>
using namespace std;
const int N=20005,M=200005,INF=100000;
int n,m,S,T;
struct oppo{
int to,nex,s;
}rod[M];
int head[N],tot;
void add(int from,int to,int s)
{
rod[tot].to=to;
rod[tot].nex=head[from];
rod[tot].s=s;
head[from]=tot++;
}
void link(int from,int to,int s)
{
add(from,to,s);
add(to,from,0);
}
int cur[N],d[N];
bool bfs()
{
queue< int > v;
memset(d,-1,sizeof(d));
d[S]=0,cur[S]=head[S];
v.push(S);
while(v.size())
{
int lxl=v.front();
v.pop();
for(int i=head[lxl];~i;i=rod[i].nex)
{
int to=rod[i].to;
if(rod[i].s&&d[to]==-1){
d[to]=d[lxl]+1;
cur[to]=head[to];
if(to==T) return 1;
v.push(to);
}
}
}
return 0;
}
int find(int x,int low)
{
if(x==T) return low;
int all=0;
for(int i=cur[x];~i;i=rod[i].nex)
{
cur[x]=i;
int to=rod[i].to;
if(d[to]==d[x]+1&&rod[i].s)
{
int t=find(to,min(rod[i].s,low-all));
if(!t) d[to]=-1;
rod[i].s-=t;
rod[i^1].s+=t;
all+=t;
}
if(all==low) break;
}
return all;
}
int dinic()
{
int ans=0,r;
while(bfs()) while(r=find(S,INF)) ans+=r;
return ans;
}
int main()
{
memset(head,-1,sizeof(head));
cin>>n>>m>>S>>T;
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
link(a,b,c);
}
cout<<dinic()<<"\n";
return 0;
}