#include<cstring>
#include<queue>
#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e4 + 1;
const int maxm = 2*1e5 + 10;
int h[maxn], p;
int pre[maxn], pr[maxn], flow[maxn];
int n, m, s, t;
ll ans;
struct edge {
int nes, to, w;
}e[maxn];
inline void add(int u, int v, int l) {
e[p].nes = h[u], e[p].to = v, e[p].w = l, h[u] = p++;
e[p].nes = h[v], e[p].to = u, e[p].w = 0, h[v] = p++;
}
int bfs(int s, int t) {
queue<int> q;
memset(pre, -1, sizeof(pre));
flow[s] = inf;
pre[s] = 0;
q.push(s);
while(!q.empty()) {
int u = q.front(); q.pop();
if(u == t) break;
for(int i = h[u]; i != -1; i = e[i].nes) {
int v = e[i].to;
if(pre[v] == -1 && e[i].w > 0) { //有容量 并且没有访问过
flow[v] = min(flow[u], e[i].w); //优化最低增广
pre[v] = u; //记录路径
pr[v] = i; // u -> v 边的编号 //记录路径上用到的边的编号
q.push(v);
}
}
}
if(pre[t] != -1) return flow[t];
else return -1;
}
ll EK() {
ll tot = 0, dalte;
while(1) { //一直寻找增广路,知道找不到为止
ll dalte = bfs(s, t);
if(dalte == -1) break;
tot += dalte; //优化最大流
int q = t;
while(q != s) {
e[pr[q]].w -= dalte; //更新残余网络 容量网络 - 流量网络 = 残余量
e[pr[q]^1].w += dalte; // 跟新方向边
q = pre[q];
}
}
return tot;
}
int main() {
memset(h, -1, sizeof(h));
ios::sync_with_stdio(false); // 关闭同步
cin >>n>>m>>s>>t;
for(int i = 0; i < m; i++) {
int u,v,l;
cin>>u>>v>>l;
add(u,v,l);
}
cout<<EK();
return 0;
}
05-06
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交