#include <cstdlib> #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <queue> #include <cmath> using namespace std; #define ll long long #define re register const int N=1e7+10; const int inf=1e9; inline void read(int &a) { a=0; int d=1; char ch; while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1; a=ch^48; while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48); a*=d; } struct note { int v,next,dis; }edge[N]; int dep[N],head[N],n,m,cnt=-1,maxflow; inline void add(int u,int v,int dis) { edge[++cnt].next=head[u]; edge[cnt].v=v; edge[cnt].dis=dis; head[u]=cnt; edge[++cnt].next=head[v]; edge[cnt].v=u; edge[cnt].dis=0; head[v]=cnt; } queue <int> q; inline bool bfs(int s,int t) { while(!q.empty()) q.pop(); memset(dep,0x7f,sizeof(dep)); dep[s]=0; q.push(s); while(!q.empty()) { int now=q.front(); q.pop(); for(re int i=head[now];i!=-1;i=edge[i].next) { if(dep[edge[i].v]>inf&&edge[i].dis) { dep[edge[i].v]=dep[now]+1; q.push(edge[i].v); } } } if(dep[t]<inf) return 1; return 0; } inline int dfs(int now,int t,int limit) { if(now==t) return limit; for(re int i=head[now];i!=-1;i=edge[i].next) { if(dep[now]+1==dep[edge[i].v]&&edge[i].dis) { int f=dfs(edge[i].v,t,min(edge[i].dis,limit)); if(f>0) { edge[i].dis-=f; edge[i^1].dis+=f; return f; } } } return 0; } inline void dinic(int s,int t) { while(bfs(s,t)) { while(int f=dfs(s,t,inf)) maxflow+=f; } } int main() { memset(head,0xff,sizeof(head)); int n,m,s,t; read(n),read(m),read(s),read(t); for(re int i=1;i<=m;i++) { int u,v,w; read(u),read(v),read(w); add(u,v,w); } dinic(s,t); printf("%d",maxflow); return 0; }
比较快的模板
#include <bits/stdc++.h> using namespace std; #define ll long long #define re register const int N=1e5+10; const int inf=1e9; inline void read(int &a) { a=0; int d=1; char ch; while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1; a=ch^48; while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48); a*=d; } int cur[N],head[N],cnt=-1,maxflow,n,m,dep[N]; struct note{int next,v,dis;}edge[2*N]; queue <int> q; inline void add(int u,int v,int dis) { edge[++cnt].next=head[u]; edge[cnt].v=v; edge[cnt].dis=dis; head[u]=cnt; } inline bool bfs(int s,int t) { while(!q.empty()) q.pop(); for(re int i=1;i<=n;i++) cur[i]=head[i]; memset(dep,0x7f,sizeof(dep)); q.push(s); dep[s]=0; while(!q.empty()) { int now=q.front(); q.pop(); for(re int i=head[now];i!=-1;i=edge[i].next) { if(dep[edge[i].v]>inf&&edge[i].dis) { dep[edge[i].v]=dep[now]+1; q.push(edge[i].v); } } } if(dep[t]<inf) return true; return false; } inline int dfs(int now,int t,int limit) { if(!limit||now==t) return limit; int flow=0,f; for(re int i=cur[now];i!=-1;i=edge[i].next) { cur[now]=i; if(dep[now]+1==dep[edge[i].v]&&(f=dfs(edge[i].v,t,min(limit,edge[i].dis)))) { flow+=f; edge[i].dis-=f; edge[i^1].dis+=f; limit-=f; if(!limit) break; } } return flow; } inline void dinic(int s,int t) {while(bfs(s,t)) maxflow+=dfs(s,t,inf);} int main() { int s,t; memset(head,0xff,sizeof(head)); read(n),read(m),read(s),read(t); for(re int i=1;i<=m;i++) { int u,v,w; read(u); read(v); read(w); add(u,v,w); add(v,u,0); } dinic(s,t); printf("%d",maxflow); return 0; }