P3376 模板网络流
emmm
题目也已经很清楚了
题目链接:https://www.luogu.com.cn/problem/P3376
#include <bits/stdc++.h> using namespace std; const int maxn=200000+5; int last[maxn],nxt[maxn*2],to[maxn*2],wi[maxn*2],cnt=-1; int deep[maxn]; int s,t,n,m; void add(int u,int v,int w) { to[++cnt]=v; wi[cnt]=w; nxt[cnt]=last[u]; last[u]=cnt; return ; } bool bfs() { queue < int > Q; memset(deep,0,sizeof(deep)); while(!Q.empty()) Q.pop(); Q.push(s); deep[s]=1; do{ int u=Q.front(); Q.pop(); for(int i=last[u];i!=-1;i=nxt[i]) if(wi[i]>0 && deep[to[i]]==0) { deep[to[i]]=deep[u]+1; Q.push(to[i]); } }while(!Q.empty()); if(deep[t]==0) return 0; return 1; } int dfs(int u,int dist) { if(u==t) return dist; for(int i=last[u];i!=-1;i=nxt[i]) if(deep[to[i]]==deep[u]+1 && wi[i]!=0) { int di=dfs(to[i],min(dist,wi[i])); if(di>0) { wi[i]-=di; wi[i^1]+=di; return di; } } return 0; } int dinic() { int ans=0; while(bfs()) ans+=dfs(s,100000007); return ans; } int main() { memset(last,-1,sizeof(last)); memset(nxt,-1,sizeof(nxt)); cin>>n>>m>>s>>t; for(int i=1;i<=m;i++) { int u,v,w; cin>>u>>v>>w; add(u,v,w); add(v,u,0); } cout<<dinic(); return 0; }