<Zoj3229 Shoot the Bullet|东方文花帖|【模板】有源汇上下界最大流 - 洛谷>
1. 先bfs分层
2. dfs增广,当前弧优化
3. 重复以上步骤
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
//#define M
//#define mo
#define N 500010
struct node {
int x, y, z, n;
}d[N];
int n, m, i, j, k;
int h[N], H[N], dep[N];
int u, v, w, S, T, ans;
queue<int>q;
void cun(int x, int y, int z) {
// printf("%lld %lld %lld\n", x, y, z);
d[++k].x=x; d[k].y=y; d[k].z=z;
d[k].n=h[x]; h[x]=k;
}
int bfs() {
while(!q.empty()) q.pop();
memset(dep, -1, sizeof(dep));
memcpy(h, H, sizeof(H));
dep[S]=1; q.push(S);
while(!q.empty()) {
u=q.front(); q.pop();
// printf("%d %d\n", u, h[u]);
for(int g=h[u]; g; g=d[g].n) {
v=d[g].y; w=d[g].z;
// printf("%lld %lld\n", v, w);
if(w<=0 || dep[v]!=-1) continue;
dep[v]=dep[u]+1; q.push(v);
}
}
// printf("--------------%lld\n", dep[T]);
return dep[T]!=-1;
}
int dfs(int x, int w) {
if(x==T) return w;
if(!w) return 0;
int ans=0, s;
for(int &i=h[x]; i; i=d[i].n) {
int y=d[i].y, z=d[i].z;
if(dep[y]!=dep[x]+1) continue;
if(z<=0) continue;
s=dfs(y, min(w, z)); ans+=s; w-=s;
d[i].z-=s; d[i^1].z+=s;
if(!w) break;
}
return ans;
}
signed main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
n=read(); m=read(); S=read(); T=read();
for(i=1, k=1; i<=m; ++i) {
u=read(); v=read(); w=read();
cun(u, v, w); cun(v, u, 0);
}
memcpy(H, h, sizeof(h));
while(bfs()) ans+=dfs(S, 1e18);
printf("%lld", ans);
return 0;
}
如果加了当前弧优化,dfs中循环里直接返回或最后返回复杂度是一样的。