luogu2740[USACO4.2]Drainage Ditches 可以随便求最大流
https://www.luogu.org/problemnew/show/P3376
然后这有个模板题用dinic~
#include<cstdio> #include<algorithm> #define rep(i,n) for(register int i=1;i<=n;i++) using namespace std; typedef long long lint; inline lint read() { lint s=0,f=1;char c=getchar(); while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=0;c=getchar();} while(c>=‘0‘&&c<=‘9‘){s=s*10+c-‘0‘;c=getchar();} return f?s:-s;; } const int N=10005; const int M=100005; const lint INF=(~0ull>>1); struct edge { int to,nxt;lint w; edge(int to=0,int nxt=0,lint w=0):to(to),nxt(nxt),w(w){} }edges[M<<1]; int n,m,cnt,st,ed,s,t; int head[M<<1],q[N],d[N]; lint ans; inline void addEdge(int u,int v,lint w) { edges[++cnt]=edge(v,head[u],w); head[u]=cnt; } #define cur edges[i].to inline bool bfs() { rep(i,n)d[i]=0;d[s]=1; st=ed=0;q[st++]=s; while(ed<st) { int k=q[ed++]; for(register int i=head[k];i;i=edges[i].nxt)if(!d[cur]&&edges[i].w) { d[cur]=d[k]+1;q[st++]=cur; if(cur==t)return 1; } } return 0; } inline lint dinic(int x,lint f) { if(x==t)return f; lint res=f; for(register int i=head[x];i&&res;i=edges[i].nxt)if(d[cur]==d[x]+1&&edges[i].w) { int k=dinic(cur,min(edges[i].w,res)); if(!k)d[cur]=0; edges[i].w-=k;edges[i^1].w+=k;res-=k; } return f-res; } #undef cur int main() { n=read();m=read();s=read();t=read();cnt=1; rep(i,m) { int u,v;lint w;u=read();v=read();w=read(); addEdge(u,v,w);addEdge(v,u,0); } lint flow=0; while(bfs())while((flow=dinic(s,INF)))ans+=flow; printf("%lld",ans); return 0; }
老是忘记cnt=1;orz