代码临时存放
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 0x3f3f3f3f;
const int N = 250, M = 1e5+5;
int head[N], now[N], d[N];
int n, m, s, t, cnt = 1;
struct Edge{
int u, v, w;
}edges[M];
void add_edge(int u, int v, int w){
edges[++cnt] = (Edge){v, head[u], w};
head[u] = cnt;
}
int q[N], hd, tl;
bool bfs(){
for(int i = 1; i <= n; i++) d[i]=inf;
d[s] = 0, now[s] = head[s];
hd = 1, tl = 1, q[1] = s;
while(hd <= tl){
int u = q[hd++];
for(int i = head[u]; i; i = edges[i].v){
Edge e = edges[i];
if(e.w>0 && d[e.u] == inf){
q[++tl] = e.u;
now[e.u] = head[e.u];
d[e.u] = d[u] + 1;
if(e.u == t)
return 1;
}
}
}
return 0;
}
int dfs(int u, int sum){
if(u == t) return sum;
int k, flow = 0;
for(int i = now[u]; i && sum; i = edges[i].v){
now[u] = i; Edge e = edges[i];
if(e.w>0 && (d[e.u] == d[u]+1)){
k = dfs(e.u, min(sum, e.w));
if(!k) d[e.u] = inf;
edges[i].w -= k, sum -= k;
edges[i^1].w += k, flow += k;
}
}
return flow;
}
void init(){
scanf("%lld%lld%lld%lld", &n, &m, &s, &t);
for(int i = 1; i <= m; i++){
int u, v, w;
scanf("%lld%lld%lld", &u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, 0);
}
}
signed main(){
// freopen("in.in", "r", stdin);
// freopen("a.out", "w", stdout);
init(); int ans = 0;
while(bfs()) ans += dfs(s, inf);
printf("%lld\n", ans);
return 0;
}
本文来自博客园,作者:_kilo-meteor,转载请注明原文链接:https://www.cnblogs.com/meteor2008/p/17866882.html