P3376 【模板】网络最大流

题目链接
存模板:
最大流EK模板:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define int long long
using namespace std;
const int maxn = 205;
const int inf = 0x7f7f7f7f;
int cnt[maxn][maxn], flow[maxn], pre[maxn];
int n, m, s, t;
queue<int> kk;
int bfs(int a, int b) {
	while (!kk.empty()) kk.pop();
	memset(pre, 0, sizeof(pre));
	kk.push(a); pre[a] = -1; flow[a] = inf;
	while (!kk.empty()) {
		int ll = kk.front(); kk.pop();
		if (ll == b) break;
		for (int i = 1; i <= n; i++) {
			if (cnt[ll][i] > 0 && pre[i] == 0 && i != a) {
				pre[i] = ll;
				flow[i] = min(flow[ll], cnt[ll][i]);
				kk.push(i);
			}
		}
	}
	if (pre[b] == 0) return -1;
	return flow[b];
}
int EK(int a, int b) {
	int mm = 0, tot = 0;
	while (1) {
		mm = bfs(s, t);
		if (mm == -1) break;
		tot += mm;
		int p = b;
		while (p != a) {
			cnt[pre[p]][p] -= mm;
			cnt[p][pre[p]] += mm;
			p = pre[p];
		}
	}
	return tot;
}
signed main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m >> s >> t;
	for (int i = 0; i < m; i++) {
		int u, v, w; cin >> u >> v >> w;
		cnt[u][v] += w;
	}
	int ans = EK(s, t);
	cout << ans << endl;
	return 0;
}

你可能感兴趣的:(P3376 【模板】网络最大流)