本文主要是介绍P3376 【模板】网络最大流(dinic模板),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
因为牛客第一场,被迫学习网络流。
代码是《进阶指南》上的模板代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int inf = 1 << 29, N = 50010, M = 300010;
int head[N], ver[M], edge[M], Next[M], d[N];
int n, m, s, t, tot;
ll maxflow;
queue<int> q;void add(int x, int y, int z) {ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot;ver[++tot] = x, edge[tot] = 0, Next[tot] = head[y], head[y] = tot;
}bool bfs() { // 在残量网络上构造分层图memset(d, 0, sizeof(d));while (q.size()) q.pop();q.push(s); d[s] = 1;while (q.size()) {int x = q.front(); q.pop();for (int i = head[x]; i; i = Next[i])if (edge[i] && !d[ver[i]]) {q.push(ver[i]);d[ver[i]] = d[x] + 1;if (ver[i] == t) return 1;}}return 0;
}int dinic(int x, int flow) { // 在当前分层图上增广if (x == t) return flow;int rest = flow, k;for (int i = head[x]; i && rest; i = Next[i])if (edge[i] && d[ver[i]] == d[x] + 1) {k = dinic(ver[i], min(rest, edge[i]));if (!k) d[ver[i]] = 0; // 剪枝,去掉增广完毕的点edge[i] -= k;edge[i ^ 1] += k;rest -= k;}return flow - rest;
}int main() {cin >> n >> m;cin >> s >> t; // 源点、汇点tot = 1;for (int i = 1; i <= m; i++) {int x, y, c;scanf("%d%d%d", &x, &y, &c);add(x, y, c);}int flow = 0;while (bfs())while (flow = dinic(s, inf)) maxflow += flow;cout << maxflow << endl;
}
这篇关于P3376 【模板】网络最大流(dinic模板)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!