「网络流浅谈」最大流的应用

news/2024/9/22 23:37:34

二分图匹配

考虑如何将二分图匹配问题,转化为流网络。设置 \(1\) 个汇点和源点,从源点向二分图一侧的每一个点连边,从另一侧向汇点连边,边权均为 \(1\),二分图中的边也全部加入,权值设为 \(1\)。这样,二分图的最大匹配等于流网络的最大流。

P2756 飞行员配对方案问题

题意:给定 \(1\) 个二分图,求最大匹配。

匈牙利算法是可以求二分图最大匹配的,不过太慢了。不妨,使用上述的方式建立出流网络,并使用 Dinic 求解出该网络的最大流即可。

举个例子:左图为样例的二分图,而右图为建立的流网络。

浅析流网络最大流与二分图最大匹配的相等性:

对于网络流的题目,只需要考虑对于任意的一个最大匹配,都能对应到一个可行流;而对于任意一个可行流都能对应到一个最大匹配。

对于任意的一个最大匹配,都能对应到一个可行流:若选择边 \(E_1,E_2, \dots ,E_k\),则可行流中的这些边均为 \(1\),且令这些边左端的顶点分别为 \(V_1,V_2,\dots,V_t\),右端的为 \(V'_1,V'_2,\dots, V'_t\),则可行流的 \(s\rightarrow V_i\) 这些边均为 \(1\)\(V'_i\rightarrow t\) 这些边也均为 \(1\)。由于匹配不存在 \(2\) 条边有公共顶点,所以一定满足容量限制与流量守恒。

对于任意的一个可行流,都能对应到一个最大匹配:可行流中流量为 \(1\) 的没有 \(s\)\(t\) 的边即为最大匹配,由于流量守恒,最多有 \(1\) 条边流向一个点,所以满足对于任意 \(2\) 条边,都不存在公共点。

故,只需要用 Dinic 跑一遍最大流即可,输出方案就是找出所有反向边流量为 \(1\)(或正向边流量为 \(0\))的边即可。

注意:二分图下的 Dinic 算法极为特殊,时间复杂度为 \(O(n^2\sqrt n)\)

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 1e2 + 10, M = 1e5 + 10;int n, m, s, t;
int h[N], e[M], ne[M], f[M], idx;
int d[N], cur[N];void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++;e[idx] = a, ne[idx] = h[b], f[idx] = 0, h[b] = idx ++;
}
bool bfs() {memset(d, -1, sizeof d);queue<int> q;q.emplace(s), cur[s] = h[s], d[s] = 0;while (q.size()) {auto u = q.front();q.pop();for (int i = h[u]; ~i; i = ne[i]) {int j = e[i];if (d[j] == -1 && f[i]) {d[j] = d[u] + 1, cur[j] = h[j];if (j == t) return 1;q.emplace(j);}}}return 0;
}
int find(int u, int lim) {if (u == t) return lim;int flow = 0;for (int i = cur[u]; ~i && flow < lim; i = ne[i]) {int j = e[i];if (d[j] == d[u] + 1 && f[i]) {int tmp = find(j, min(lim - flow, f[i]));if (!tmp) d[j] = -1;f[i] -= tmp, f[i ^ 1] += tmp, flow += tmp;}}return flow;
}
int dinic() {int res = 0, flow;while (bfs()) while (flow = find(s, 1e18)) res += flow;return res;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);memset(h, -1, sizeof h);cin >> m >> n;s = 0, t = n + 1;int u, v;while (cin >> u >> v && u != -1) {add(u, v, 1);}for (int i = 1; i <= m; i ++)add(s, i, 1);for (int i = m + 1; i <= n; i ++)add(i, t, 1);cout << dinic() << endl;for (int i = 0; i < idx; i += 2)if (e[i] != t && e[i ^ 1] != s && !f[i])cout << e[i ^ 1] << " " << e[i] << endl;return 0;
}

习题

P3254 圆桌问题,与原建图方式有略微差异。

参考代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 5e2 + 10, M = 1e5 + 10;int m, n, s, t;
int a[N], b[N];
int h[N], e[M], f[M], ne[M], idx;
int d[N], cur[N];void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++;e[idx] = a, ne[idx] = h[b], f[idx] = 0, h[b] = idx ++;
}
bool bfs() {memset(d, -1, sizeof d);queue<int> q;q.emplace(s), d[s] = 0, cur[s] = h[s];while (q.size()) {int u = q.front();q.pop();for (int i = h[u]; ~i; i = ne[i]) {int j = e[i];if (d[j] == -1 && f[i]) {d[j] = d[u] + 1;cur[j] = h[j];if (j == t) return 1;q.emplace(j);}}}return 0;
}
int find(int u, int lim) {if (u == t) return lim;int flow = 0;for (int i = cur[u]; ~i && flow < lim; i = ne[i]) {cur[u] = i;int j = e[i];if (d[j] == d[u] + 1 && f[i]) {int tmp = find(j, min(lim - flow, f[i]));if (!tmp) d[j] = -1;f[i] -= tmp, f[i ^ 1] += tmp, flow += tmp;}}return flow;
}
int dinic() {int res = 0, flow;while (bfs()) while (flow = find(s, 1e18)) res += flow;return res;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);memset(h, -1, sizeof h);cin >> m >> n;s = 0, t = n + m + 1;int tot = 0;for (int i = 1; i <= m; i ++)cin >> a[i], add(s, i, a[i]), tot += a[i];for (int i = 1; i <= n; i ++)cin >> b[i], add(i + m, t, b[i]);for (int i = 1; i <= m; i ++)for (int j = m + 1; j <= n + m; j ++)add(i, j, 1);if (dinic() == tot) {cout << 1 << endl;std::vector<vector<int>> way(m + 1);for (int i = 0; i < idx; i += 2)if (e[i] != t && e[i ^ 1] != s && !f[i])way[e[i ^ 1]].emplace_back(e[i] - m);for (int i = 1; i <= m; i ++) {for (auto v : way[i])cout << v << " ";cout << endl;}} else {cout << 0 << endl;}return 0;
}

多源汇最大流

本质上只不过是源点不是 \(1\) 个,汇点也不是 \(1\) 个了,那么其实只需要再设一个超级源点连向所有源点,边权为 \(+\infty\),表示向这些源点可以流任意多流量,也就是说从这些源点可以流出任意多流量;同样的,从每一个汇点向超级汇点连一条 \(+\infty\) 的边,表示这些汇点可以流向超级汇点任意多流量,也就是说这些汇点都可以接纳任意多的流量。

这样的新流网络的最大流就是源网络的最大流,所以只需要对于新网络跑一遍 Dinic 即可。

习题

AcWing 2234. 多源汇最大流,模版题

参考代码
#include <iostream>
#include <cstring>
#include <queue>
#define int long longusing namespace std;typedef pair<int, int> PII;const int SIZE = 5e5 + 10;int N, M, Sc, Tc, S, T;
int h[SIZE], e[SIZE], ne[SIZE], f[SIZE], idx;
int D[SIZE], Current[SIZE];void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++;e[idx] = a, ne[idx] = h[b], f[idx] = 0, h[b] = idx ++;
}bool BFS() {memset(D, -1, sizeof D);queue<int> Q;Q.push(S), D[S] = 0, Current[S] = h[S];while (Q.size()) {int u = Q.front();Q.pop();for (int i = h[u]; ~i; i = ne[i]) {int j = e[i];if (D[j] == -1 && f[i]) {D[j] = D[u] + 1;Current[j] = h[j];if (j == T) return true;Q.push(j);}}}return false;
}int Find(int u, int limit) {if (u == T) return limit;int flow = 0;for (int i = Current[u]; ~i && flow < limit; i = ne[i]) {Current[u] = i;int j = e[i];if (D[j] == D[u] + 1 && f[i]) {int T = Find(j, min(f[i], limit - flow));if (!T) D[j] = -1;f[i] -= T, f[i ^ 1] += T, flow += T;}}return flow;
}int Dinic() {int Result = 0, flow;while (BFS()) while (flow = Find(S, 1e18)) Result += flow;return Result;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);memset(h, -1, sizeof h);cin >> N >> M >> Sc >> Tc;S = 0, T = N + 1;while (Sc --) {int u;cin >> u;add(S, u, 1e18);}while (Tc --) {int u;cin >> u;add(u, T, 1e18);}while (M --) {int a, b, c;cin >> a >> b >> c;add(a, b, c);}cout << Dinic() << endl;return 0;
}

关键边

POJ3204 Ikki's Story I - Road Reconstruction

题意:给定 \(1\) 个流网络,求有多少条边,满足增加该边边权后能使最大流增加。

考虑一条边满足什么条件使得增加容量后会使得最大流增加,回顾求最大流的过程:每一次在残留网络中找增广路径,并加到最大流中。

那么,如果容量增加后,最大流增加,那么必然是增加流量后产生 \(1\) 条增广路径。所以,对于每一条边 \((u,v)\),只需要判断是否存在 \(1\) 条增广路径 \(s\rightarrow u\) 以及 \(1\) 增广路径 \(v\rightarrow t\)。判断的方法就是在最大流的残留网络中 DFS,记录每次走 \(>0\) 的边能到达那些点即可。

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 5e2 + 10, M = 2e4 + 10;int n, m, s, t;
int h[N], e[M], ne[M], f[M], idx;
int d[N], cur[N], vis[2][N];void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++;e[idx] = a, ne[idx] = h[b], f[idx] = 0, h[b] = idx ++;
}
bool bfs() {memset(d, -1, sizeof d);queue<int> q;q.emplace(s), d[s] = 0, cur[s] = h[s];while (q.size()) {int u = q.front();q.pop();for (int i = h[u]; ~i; i = ne[i]) {int j = e[i];if (d[j] == -1 && f[i]) {d[j] = d[u] + 1, cur[j] = h[j];if (j == t) return 1;q.emplace(j);}}}return 0;
}
int find(int u, int lim) {if (u == t) return lim;int flow = 0;for (int i = cur[u]; ~i && flow < lim; i = ne[i]) {cur[u] = i;int j = e[i];if (d[j] == d[u] + 1 && f[i]) {int tmp = find(j, min(lim - flow, f[i]));if (!tmp) d[j] = -1;f[i] -= tmp, f[i ^ 1] += tmp, flow += tmp;}}return flow;
}
int dinic() {int res = 0, flow;while (bfs()) while (flow = find(s, 1e18)) res += flow;return res;
}
void dfs(int u, int k) {vis[k][u] = 1;for (int i = h[u]; ~i; i = ne[i]) {int j = e[i];if (!vis[k][j] && f[i ^ k])dfs(j, k);}
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);memset(h, -1, sizeof h);cin >> n >> m;s = 0, t = n - 1;while (m --) {int u, v, w;cin >> u >> v >> w;add(u, v, w);}dinic();dfs(s, 0), dfs(t, 1);int res = 0;for (int i = 0; i < idx; i += 2)if (vis[0][e[i ^ 1]] && vis[1][e[i]])res ++;cout << res << endl;return 0;
}

拆点

POJ3281 Dining

题意:有 \(n\) 头奶牛,\(F\) 个食物和 \(D\) 个饮料,每头奶牛可以吃某些食物和饮料,但都只能吃食物和饮料各一个。求最多能满足多少头奶牛。(三分图匹配

考虑继续使用类似二分图的建网络流的方式,举个例子:

不过,这样真的能够求出最终的答案吗?答案是否定的。

考虑局部的这样一个位置,最大流得到话会流出 \(2\) 的,也就是这个奶牛会贡献 \(2\),而应该是 \(1\)

所以,就要拆点了!

通过,流量守恒,就可以使得通过每一个点的流量最多为 \(1\),也就满足了题意。

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 4e2 + 10, M = 1e5 + 10;int n, m, k, s, t;
int h[N], e[M], ne[M], f[M], idx;
int d[N], cur[N];void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++;e[idx] = a, ne[idx] = h[b], f[idx] = 0, h[b] = idx ++;
}
bool bfs() {memset(d, -1, sizeof d);queue<int> q;q.emplace(s), d[s] = 0, cur[s] = h[s];while (q.size()) {int u = q.front();q.pop();for (int i = h[u]; ~i; i = ne[i]) {int j = e[i];if (d[j] == -1 && f[i]) {d[j] = d[u] + 1, cur[j] = h[j];if (j == t) return 1;q.emplace(j);}}}return 0;
}
int find(int u, int lim) {if (u == t) return lim;int flow = 0;for (int i = cur[u]; ~i && flow < lim; i = ne[i]) {cur[u] = i;int j = e[i];if (d[j] == d[u] + 1 && f[i]) {int tmp = find(j, min(lim - flow, f[i]));if (!tmp) d[j] = -1;f[i] -= tmp, f[i ^ 1] += tmp, flow += tmp;}}return flow;
}
int dinic() {int res = 0, flow;while (bfs()) while (flow = find(s, 1e18)) res += flow;return res;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);memset(h, -1, sizeof h);cin >> n >> m >> k;s = 0, t = n * 2 + m + k + 1;for (int i = 1; i <= n; i ++) {int cf, cd, x;cin >> cf >> cd;for (int j = 1; j <= cf; j ++)cin >> x, add(x, i + m, 1);for (int j = 1; j <= cd; j ++)cin >> x, add(i + m + n, x + m + n + n, 1);}for (int i = 1; i <= m; i ++)add(s, i, 1);for (int i = m + n * 2 + 1; i < t; i ++)add(i, t, 1);for (int i = m + 1; i <= m + n; i ++)add(i, i + n, 1);cout << dinic() << endl;return 0;
}

习题

P2766 最长不下降子序列问题

参考代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 1e3 + 10, M = 4e5 + 10;int n, s, t;
int a[N], dp[N];
int h[N], e[M], ne[M], f[M], idx;
int d[N], cur[N];
void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++;e[idx] = a, ne[idx] = h[b], f[idx] = 0, h[b] = idx ++;
}
bool bfs() {memset(d, -1, sizeof d);queue<int> q;q.emplace(s), d[s] = 0, cur[s] = h[s];while (q.size()) {int u = q.front();q.pop();for (int i = h[u]; ~i; i = ne[i]) {int j = e[i];if (d[j] == -1 && f[i]) {d[j] = d[u] + 1, cur[j] = h[j];if (j == t) return 1;q.emplace(j);}}}return 0;
}
int find(int u, int lim) {if (u == t) return lim;int flow = 0;for (int i = cur[u]; ~i && flow < lim; i = ne[i]) {cur[u] = i;int j = e[i];if (d[j] == d[u] + 1 && f[i]) {int tmp = find(j, min(lim - flow, f[i]));if (!tmp) d[j] = -1;f[i] -= tmp, f[i ^ 1] += tmp, flow += tmp;}}return flow;
}
int dinic() {int res = 0, flow;while (bfs()) while (flow = find(s, 1e18)) res += flow;return res;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);memset(h, -1, sizeof h);cin >> n;for (int i = 1; i <= n; i ++)cin >> a[i];s = 0, t = 2 * n + 1;for (int i = 1; i <= n; i ++) {dp[i] = 1, add(i, i + n, 1);std::vector<int> opt;for (int j = 1; j < i; j ++)if (a[j] <= a[i] && dp[j] + 1 > dp[i])opt.clear(), opt.emplace_back(j), dp[i] = dp[j] + 1;else if (a[j] <= a[i] && dp[j] + 1 == dp[i])opt.emplace_back(j);for (auto v : opt)add(n + v, i, 1);}int res = 0;for (int i = 1; i <= n; i ++)res = max(res, dp[i]);cout << res << endl;for (int i = 1; i <= n; i ++) {if (dp[i] == res)add(i + n, t, 1);if (dp[i] == 1)add(s, i, 1);}res = dinic();cout << res << endl;for (int i = 0; i < idx; i += 2) {if (e[i ^ 1] == 1 && e[i] == 1 + n || e[i ^ 1] == 1 + n && e[i] == t || e[i ^ 1] == s && e[i] == 1)f[i] = 1e18;else if (e[i ^ 1] == n && e[i] == n + n || e[i ^ 1] == n + n && e[i] == t || e[i ^ 1] == s && e[i] == n)f[i] = 1e18;}res += dinic();cout << min(res, n) << endl;return 0;
}

POJ3498 March of the Penguins

参考代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long longusing namespace std;typedef pair<int, int> PII;
typedef long long LL;const int N = 2e2 + 10, M = 2e4 + 10;int n, s, t;
double ld;
int h[N], e[M], ne[M], f[M], idx;
int d[N], cur[N];
struct Node {int x, y;int tot, cnt;double operator- (const Node &tmp)const {int a = x - tmp.x, b = y - tmp.y;return sqrt(a * a * 1.0 + b * b * 1.0);}
}pg[N];
void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++;e[idx] = a, ne[idx] = h[b], f[idx] = 0, h[b] = idx ++;
}
bool bfs() {memset(d, -1, sizeof d);queue<int> q;q.emplace(s), d[s] = 0, cur[s] = h[s];while (q.size()) {int u = q.front();q.pop();for (int i = h[u]; ~i; i = ne[i]) {int j = e[i];if (d[j] == -1 && f[i]) {d[j] = d[u] + 1, cur[j] = h[j];if (j == t) return 1;q.emplace(j);}}}return 0;
}
int find(int u, int lim) {if (u == t) return lim;int flow = 0;for (int i = cur[u]; ~i && flow < lim; i = ne[i]) {cur[u] = i;int j = e[i];if (d[j] == d[u] + 1 && f[i]) {int tmp = find(j, min(lim - flow, f[i]));if (!tmp) d[j] = -1;f[i] -= tmp, f[i ^ 1] += tmp, flow += tmp;}}return flow;
}
int dinic() {int res = 0, flow;while (bfs()) while (flow = find(s, 1e18)) res += flow;return res;
}void solve() {cin >> n >> ld;int sum = 0;for (int i = 1; i <= n; i ++)cin >> pg[i].x >> pg[i].y >> pg[i].tot >> pg[i].cnt, sum += pg[i].tot;s = 0;std::vector<int> res;for (t = 1; t <= n; t ++) {memset(h, -1, sizeof h);idx = 0;for (int i = 1; i <= n; i ++) {add(s, i, pg[i].tot), add(i, i + n, pg[i].cnt);for (int j = 1; j <= n; j ++)if (i != j && pg[j] - pg[i] <= ld)add(i + n, j, 1e18);}if (dinic() == sum)res.emplace_back(t);}if (res.empty())cout << -1 << endl;else {for (auto v : res)cout << v - 1 << " ";cout << endl;}
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/31346.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

单元测试

实验项目名称:实验四 单元测试2一、 实验目的 1、 掌握单元测试技术,并按单元测试的要求设计测试用例。  2、 掌握一种单元测试工具的使用。 二、 实验内容 自行学习C#或python或C++的其中一种单元测试工具的使用,自选一段单元代码(不少于15行),进行测试。完成实验报告…

同一个函数/不同函数的接口关联

第一种:在同一个方法中接口关联,可以直接提取后引用第二种:在不同方法中,声明全局变量,提取后引用第三种:通过在conftest.py文件中定义一个夹具,在测试用例函数中使用这个夹具 # 定义一个登录成功后获取token的夹具 @pytest.fixture(scope="session") def log…

windows 安装Nginx服务

一、版本说明Nginx版本:1.26.0 二、下载Nginx下载地址:https://nginx.org/en/download.html选择一个版本,这里选择最新稳定版本下载后解压到一个目录,注意解压目录最好不要有中文、空格因为电脑只有一个C盘所以地址在C盘,可以选择自己习惯的安装位置 三、下载winsw下载地…

统计力学中的概率论基础(一)

本文的主要内容是一些统计力学中的基础的概率论知识,如密度函数、分布函数和贝叶斯定理的一些基本概念,主要作为一个简单的知识内容记录和分享,后续还有更多的同系列文章。技术背景 统计力学是一门通过粒子的纯粹微观量来表示系统宏观量的学科,从统计分布出发,用无偏/有偏…

【转】十年技术进阶路,让我明白了三件要事(8000字长文)

前言【本文于2022-5-10日首发于ITPUB微信公众号平台】该篇文章是我第一次跟DTCC合作编写的,整篇文章大概8000字,可能花您15分钟阅读。我和DTCC的韩楠老师,共花7了天时间,每天把该文章打磨到晚上12点,在这非常感谢编辑老师的负责与付出。这篇也是我分享里为数不多“进阶”与…

springmvc中的转发和重定向

转发和重定向的区别转发是一个请求一次响应,重定向是两次请求两次响应;转发地址栏不发生变化,重定向地址栏发生变化(会显示重定向后的地址);转发只能转发到本项目中其他控制器(在学习JavaWeb的时候,说的是:转发只能转发到本项目中其他Servlet),重定向不仅能重定向到本…

SQL Server什么是索引

目录什么是索引 1.1. 优点 1.2. 缺点 创建索引 使用索引 删除索引 在什么地方可以使用索引 在什么地方不使用索引什么是索引 问:我们用字典查一个字,但是字典非常厚,我们要怎么查到我们想要查的字呢? 答:我们可以使用字典中的目录,根据目录可以快速查到我们想差的字。 我…

基于TIA WINCC的审计追踪功能

1. 哪些HMI可以用auditTrailTP277/OP277/MP277等等 精致面板系列 wincc runtime advanced wincc runtime advanced的面板PC2. 设置步骤进入"优良系统设置",启动"组态与GMP相符",启动GMP功能进入"记录"-"审计追踪"HMI保存记录可以选S…