henkgaz @ 2019-06-03 20:35:43
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int inf=0x7fffffff;
struct edge{
int to,cap,flow,nxt;
edge()
{
nxt=-1;
}
}e[200001];
int n,m,s,t,fir[10001],depth[10001],cnt=-1,w,k,dis;
queue<int>q;
void ae(int x,int y,int z)
{
e[++cnt].to=y;
e[cnt].cap=z;
e[cnt].nxt=fir[x];
fir[x]=cnt;
}
bool bfs()
{
while(!q.empty())
q.pop();
memset(depth,0,sizeof(depth));
depth[s]=1;
q.push(s);
while(!q.empty())
{
k=q.front();
q.pop();
for(int i=fir[k];i!=-1;i=e[i].nxt)
{
w=e[i].cap-e[i].flow;
if(w>0&&!depth[e[i].to])
{
depth[e[i].to]=depth[k]+1;
q.push(e[i].to);
}
}
}
if(!depth[t])
return 0;
return 1;
}
int dfs(int u,int y)
{
if(u==t)
return y;
for(int& i=fir[u];i!=-1;i=e[i].nxt)
{
w=e[i].cap-e[i].flow;
if((w!=0)&&(depth[e[i].to]=depth[u]+1))
{
dis=dfs(e[i].to,min(y,e[i].cap-e[i].flow));
if(dis==0)
break;
e[i].flow+=dis;
e[i^1].flow-=dis;
return dis;
}
}
return 0;
}
int dinic()
{
int ans=0;
while(bfs())
{
while(dis=dfs(s,inf))
ans+=dis;
}
return ans;
}
int main()
{
int u,v,w;
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=n;i++)
fir[i]=-1;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
ae(u,v,w);
ae(v,u,0);
}
printf("%d",dinic());
return 0;
}