存个网络流模板
洛谷P3376
#include<stdio.h>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
const int maxn = 10000+5;
const int INF = 0x3fffffff;
struct node{
int to,w,rev;
};
vector<node>G[maxn];
int n,m,S,T,
dis[maxn];
bool bfs(){
memset(dis,-1,sizeof(dis));
dis[S] = 0;
queue<int>que;
que.push(S);
while(!que.empty()){
int e = que.front();
que.pop();
for(int i=0,sz=G[e].size();i<sz;i++){
node t = G[e][i];
if(dis[t.to] == -1 && t.w){
dis[t.to] = dis[e]+1;
que.push(t.to);
}
}
}
return dis[T] != -1;
}
int dfs(int a,int flow){
//printf("dfs %d %d\n",a,flow);
if(a == T)return flow;
int res = 0;
for(int i=0,sz=G[a].size();i<sz;i++){
node &t = G[a][i];
if(dis[t.to] == dis[a] +1 && t.w){
int tmp = dfs(t.to,min(t.w,flow));
if(!tmp)continue;
flow -= tmp;
res += tmp;
t.w -= tmp;
G[t.to][t.rev].w += tmp;
if(flow<=0)break;
}
}
return res;
}
int main(){
scanf("%d%d%d%d",&n,&m,&S,&T);
int x,y,w;
while(m--){
scanf("%d%d%d",&x,&y,&w);
G[x].push_back(node{y,w,G[y].size()});
G[y].push_back(node{x,0,G[x].size()-1});
}
int ans = 0;
while(bfs())ans += dfs(S,INF);
cout<<ans;
return 0;
}