#include<bits/stdc++.h>
#define fo(i,a,b) for (ll (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for (ll (i)=(b);(i)>=(a);(i)--)
#define lc (o<<1)
#define rc ((o<<1)|1)
#define mk(x,y) make_pair((x),(y))
#define eb emplace_back
#define A puts("Yes")
#define B puts("No")
#define fi first
#define se second
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef long long i64;
const ll inf=1ll<<60;
const ll mo1=1e9+9;
const ll mo2=1e9+7;
const ll P=131;
const ll Q=13331;
struct MF {
const static int N=1e5+5;
const static int M=1e6+5;
const static int INF=1e9;
struct edge {
int v, nxt;
ll cap, flow;
} e[M*2];
int head[N], tot = 0;
int n, S, T;
ll maxflow = 0;
int dep[N], cur[N];
MF(int nn,int s,int t) {
n=nn;
S=s;
T=t;
memset(head, -1, sizeof head);
tot = 0;
}
void init() {
memset(head, -1, sizeof head);
tot = 0;
}
void addedge(int u, int v, int w) {
e[tot] = {v, head[u], w, 0};
head[u] = tot++;
e[tot] = {u, head[v], 0, 0};
head[v] = tot++;
}
bool bfs() {
queue<int> q;
memset(dep, 0, sizeof(int) * (n + 1));
dep[S] = 1;
q.push(S);
while (q.size()) {
int u = q.front();
q.pop();
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if ((!dep[v]) && (e[i].cap > e[i].flow)) {
dep[v] = dep[u] + 1;
q.push(v);
}
}
}
return dep[T];
}
ll dfs(int u, ll flow) {
if ((u == T) || (!flow)) return flow;
ll ret = 0;
for (int& i = cur[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
ll d;
if ((dep[v] == dep[u] + 1) &&
(d = dfs(v, min(flow - ret, e[i].cap - e[i].flow)))) {
ret += d;
e[i].flow += d;
e[i ^ 1].flow -= d;
if (ret == flow) return ret;
}
}
return ret;
}
void dinic() {
maxflow = 0;
while (bfs()) {
memcpy(cur, head, sizeof(int) * (n + 1));
maxflow += dfs(S, INF);
}
}
};
int n,m,s,t,x,y,z;
int main(){
// freopen("data.in","r",stdin);
scanf("%d %d %d %d",&n,&m,&s,&t);
MF mf(n,s,t);
fo(i,1,m) {
scanf("%d %d %d",&x,&y,&z);
mf.addedge(x,y,z);
}
mf.dinic();
printf("%lld\n",mf.maxflow);
return 0;
}
标签:return,最大,int,ll,flow,ret,dep,模板
From: https://www.cnblogs.com/ganking/p/18302520