题目链接:https://www.luogu.org/problem/P3376
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
queue<int> q;
int n,m,s,t,tot;
int head[10010],pre[10010],maxflow,flow[10010];
struct node
{
int to,w,nextt;
}E[200010];
void add(int from,int to,int w)
{
E[tot].to=to;
E[tot].w=w;
E[tot].nextt=head[from];
head[from]=tot++;
}
int bfs(int be,int en)
{
while(!q.empty()) q.pop();
memset(pre,-1,sizeof(pre));
pre[be]=0;
q.push(be);
flow[be]=inf;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];~i;i=E[i].nextt)
{
int v=E[i].to,w=E[i].w;
if(w>0&&pre[v]==-1)
{
pre[v]=i;
flow[v]=min(w,flow[u]);
q.push(v);
}
}
}
return pre[en]==-1?-1:flow[en];
}
void EK(int be,int en)
{
int increase=0;
while((increase=bfs(be,en))!=-1)
{
int k=en;
while(k!=be)
{
int l=pre[k];
E[l].w-=increase;
E[l^1].w+=increase;
k=E[l^1].to;
}
maxflow+=increase;
}
}
int main()
{
int u,v,w;
memset(head,-1,sizeof(head));
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,0);//反向建边
}
EK(s,t);
printf("%d\n",maxflow);
}