跳到正文

题解

我再次学会了网络流

松绒灼茁桌捉浊卓焯琢约 17 分钟
原文
文章信息与版本

文章信息

发布
2026年9月16日
收录
2026年9月17日
更新
2026年11月9日
篇幅
约 17 分钟
获赞
32
题目
P14361

版本

  1. v3当前
  2. v2
  3. v1

前言

本题如果仅作为 CSP-S T1 而言,存在一个非常简短(但对有些人而言不好想)的解法——让所有人直奔第一志愿,再把唯一爆满的社团里亏损最小的人调剂走。

然而,这极度依赖于“容量均为 ⌊n2⌋\lfloor \frac{n}{2} \rfloor”这一特殊限制。当各社团容量为任意上限 c1,c2,…c_1, c_2, \dots,或社团数量进一步增加时,上述贪心将彻底失效。

本题解旨在跳出特殊性质的局限,提供一种基于“模拟费用流”的通用视角。尽管有“大炮打蚊子”之嫌,但该解法能系统性地解决任意容量、更多社团(合理范围内)的情况,具有一定的启发性与推广价值。

题意简述

题目链接

有 nn 个人和 33 个社团,每个人加入不同社团有不同的价值。每个社团人数不能超过 ⌊n2⌋\lfloor \frac{n}{2} \rfloor,求所有人全部加入社团后的最大收益。

网络流

对于这种简单题,我们显然是可以用网络流的,于是我们考虑这样的建模方式:

  • 设点集 V={S,T,G1,G2,G3,1,2,…,n}V = \{S, T, G_1, G_2, G_3, 1, 2, \dots, n\},其中 SS 为源点,TT 为汇点。
  • 边集 EE 包含:
    • S→iS \to i,容量为 11,费用为 00(1≤i≤n1 \le i \le n);
    • i→gi \to g,容量为 11,费用为 ai,ga_{i,g}(1≤i≤n,g∈{G1,G2,G3}1 \le i \le n, g \in \{G_1, G_2, G_3\});
    • g→Tg \to T,容量为 ⌊n2⌋\lfloor \frac{n}{2} \rfloor,费用为 00(g∈{G1,G2,G3}g \in \{G_1, G_2, G_3\})。

我们需要在这个网络中流满 nn 单位流量的最大费用。直接跑费用流算法显然会超时,因此考虑模拟费用流。

模拟费用流

模拟费用流即指我们用一种高效的方式去增广,取代原本要遍历整个图求解最短路的方式。

在此之前,我们要找到所有可能的增广路有哪些。

容易发现我们只需要考虑所有的简单增广路径即可,即不需要考虑有环的情况。因为我们在一个无正环的残量网络增广必定不会出现一个含环的增广路(不然就和前面的无正环矛盾了)。

而简单的增广路径只能在源汇点之间经过社团集合 GG 的一个不重复子排列,经过社团的数量只可能是 1,2,31,2,3:

  • 经过一个社团:A31=3A_{3}^{1}=3 种。
  • 经过两个社团:A32=6A_{3}^{2}=6 种。
  • 经过三个社团:A33=6A_{3}^{3}=6 种。

简单增广路径的总数为 ∑i=1∣G∣A∣G∣i≈e∣G∣!\displaystyle\sum_{i=1}^{|G|}A_{|G|}^{i}\approx e|G|!,本题 ∣G∣=3|G|=3,完全可以接受。

因此即可得到以下 3+6+63+6+6 种情况:

  • S→i→G1→TS\to i \to G_1 \to T:意思是将 ii 加入 G1G_1 组。
  • S→i→G2→TS\to i \to G_2 \to T:意思是将 ii 加入 G2G_2 组。
  • S→i→G3→TS\to i \to G_3 \to T:意思是将 ii 加入 G3G_3 组。
  • S→i→G1→j→G2→TS\to i \to G_1 \to j \to G_2 \to T:意思是将 ii 加入 G1G_1 组,并将原本在 G1G_1 组的 jj 移去 G2G_2。
  • S→i→G1→j→G3→TS\to i \to G_1 \to j \to G_3 \to T:意思是将 ii 加入 G1G_1 组,并将原本在 G1G_1 组的 jj 移去 G3G_3。
  • S→i→G2→j→G1→TS\to i \to G_2 \to j \to G_1 \to T:意思是将 ii 加入 G2G_2 组,并将原本在 G2G_2 组的 jj 移去 G1G_1。
  • S→i→G2→j→G3→TS\to i \to G_2 \to j \to G_3 \to T:意思是将 ii 加入 G2G_2 组,并将原本在 G2G_2 组的 jj 移去 G3G_3。
  • S→i→G3→j→G1→TS\to i \to G_3 \to j \to G_1 \to T:意思是将 ii 加入 G3G_3 组,并将原本在 G3G_3 组的 jj 移去 G1G_1。
  • S→i→G3→j→G2→TS\to i \to G_3 \to j \to G_2 \to T:意思是将 ii 加入 G3G_3 组,并将原本在 G3G_3 组的 jj 移去 G2G_2。
  • S→i→G1→j→G2→k→G3→TS\to i \to G_1 \to j \to G_2 \to k \to G_3 \to T:意思是将 ii 加入 G1G_1 组,并将原本在 G1G_1 组的 jj 移去 G2G_2,再将原本 G2G_2 组的 kk 移去 G3G_3。
  • S→i→G1→j→G3→k→G2→TS\to i \to G_1 \to j \to G_3 \to k \to G_2 \to T:意思是将 ii 加入 G1G_1 组,并将原本在 G1G_1 组的 jj 移去 G3G_3,再将原本 G3G_3 组的 kk 移去 G2G_2。
  • S→i→G2→j→G1→k→G3→TS\to i \to G_2 \to j \to G_1 \to k \to G_3 \to T:意思是将 ii 加入 G2G_2 组,并将原本在 G2G_2 组的 jj 移去 G1G_1,再将原本 G1G_1 组的 kk 移去 G3G_3。
  • S→i→G2→j→G3→k→G1→TS\to i \to G_2 \to j \to G_3 \to k \to G_1 \to T:意思是将 ii 加入 G2G_2 组,并将原本在 G2G_2 组的 jj 移去 G3G_3,再将原本 G3G_3 组的 kk 移去 G1G_1。
  • S→i→G3→j→G1→k→G2→TS\to i \to G_3 \to j \to G_1 \to k \to G_2 \to T:意思是将 ii 加入 G3G_3 组,并将原本在 G3G_3 组的 jj 移去 G1G_1,再将原本 G1G_1 组的 kk 移去 G2G_2。
  • S→i→G3→j→G2→k→G1→TS\to i \to G_3 \to j \to G_2 \to k \to G_1 \to T:意思是将 ii 加入 G3G_3 组,并将原本在 G3G_3 组的 jj 移去 G2G_2,再将原本 G2G_2 组的 kk 移去 G1G_1。

注意,以上的 i,j,ki,j,k 一定是互不相等的,因为 ii 一定来自未分配集合,j,kj,k 一定来自两个不同的 GG,这三个集合两两互不相交的。

这样,我们就考虑维护以下 99 类转移代价信息:

  • Wi,0,1W_{i,0,1}:表示将一个未分配的 ii 流向 G1G_1 的代价(即 ai,1a_{i,1})。
  • Wi,0,2W_{i,0,2}:表示将一个未分配的 ii 流向 G2G_2 的代价(即 ai,2a_{i,2})。
  • Wi,0,3W_{i,0,3}:表示将一个未分配的 ii 流向 G3G_3 的代价(即 ai,3a_{i,3})。
  • Wi,1,2W_{i,1,2}:表示将一个原本分配给 G1G_1 的 ii 改为流向 G2G_2 的代价(即 −ai,1+ai,2-a_{i,1}+a_{i,2})。
  • Wi,1,3W_{i,1,3}:表示将一个原本分配给 G1G_1 的 ii 改为流向 G3G_3 的代价(即 −ai,1+ai,3-a_{i,1}+a_{i,3})。
  • Wi,2,3W_{i,2,3}:表示将一个原本分配给 G2G_2 的 ii 改为流向 G3G_3 的代价(即 −ai,2+ai,3-a_{i,2}+a_{i,3})。
  • 另外三种情况为上述后三者的反向转移,即 Wi,2,1,Wi,3,1,Wi,3,2W_{i,2,1}, W_{i,3,1}, W_{i,3,2}。

这样,原本 1515 种增广情况就都可以转化为若干个 WW 代价的相加。我们只需要维护 99 个大根堆,分别维护上述 99 种转移对应的合法候选人的最大值。

以 S→i→G3→j→G1→k→G2→TS\to i \to G_3 \to j \to G_1 \to k \to G_2 \to T 型增广为例,其增加的总费用为 Wi,0,3+Wj,3,1+Wk,1,2W_{i,0,3}+W_{j,3,1}+W_{k,1,2}。我们要求解:

max⁡i,j,k is valid(Wi,0,3+Wj,3,1+Wk,1,2)\max_{i,j,k\text{ is valid}}(W_{i,0,3}+W_{j,3,1}+W_{k,1,2})

注意到 i∉G1∪G2∪G3i \notin G_1\cup G_2\cup G_3、j∈G3j\in G_3、k∈G1k\in G_1,由于这三个候选集合两两互不相交,人员的选取完全独立,因此多元最大值可以拆分:

max⁡i∉G1∪G2∪G3(Wi,0,3)+max⁡j∈G3(Wj,3,1)+max⁡k∈G1(Wk,1,2)\max_{i \notin G_1\cup G_2\cup G_3}(W_{i,0,3}) + \max_{j\in G_3}(W_{j,3,1}) + \max_{k\in G_1}(W_{k,1,2})

这说明我们直接分别取对应堆的堆顶相加即可得到该路径的最大增广费用。

复杂度

  • 时间复杂度:算法总共进行 nn 次增广。单次增广需要遍历 O(∣G∣!)O(|G|!) 种路径情况;每次增广最多涉及 O(∣G∣)O(|G|) 个人员的社团变动,每次变动会向 O(∣G∣)O(|G|) 个堆中推入新状态。结合惰性删除机制,全局每个元素最多进出堆 O(∣G∣2)O(|G|^2) 次,均摊到单次增广的堆操作时间为 O(∣G∣2log⁡n)O(|G|^2 \log n)。因此总时间复杂度为 O(n(∣G∣!+∣G∣2log⁡n))O\big(n(|G|! + |G|^2\log n)\big),在本题中 ∣G∣=3|G|=3,常数极小,可以通过。
  • 空间复杂度:全局累计推入堆中的元素总量为 O(n∣G∣2)O(n |G|^2),辅助数组空间为 O(n)O(n),故总空间复杂度为 O(n∣G∣2)O(n |G|^2)。

代码

有点长
C++
#include <bits/stdc++.h>
using namespace std;
#define cs(x) isp(x.top().second); ist(x.top().second); sel[x.top().second] = 1; x.pop();
#define ct(x) its(x.top().second); itp(x.top().second); sel[x.top().second] = 2; x.pop();
#define cp(x) ips(x.top().second); ipt(x.top().second); sel[x.top().second] = 3; x.pop();
#define ips(x) ps.push({-::p[x] + ::s[x], x})
#define isp(x) sp.push({-::s[x] + ::p[x], x})
#define ipt(x) pt.push({-::p[x] + ::t[x], x})
#define itp(x) tp.push({-::t[x] + ::p[x], x})
#define ist(x) st.push({-::s[x] + ::t[x], x})
#define its(x) ts.push({-::t[x] + ::s[x], x})

int n, s[100005], t[100005], p[100005], sel[100005];
void solve() {
	cin >> n;
	priority_queue<pair<int,int>> s1, s2, s3, sp, ps, st, ts, pt, tp;
	// s=1, t=2, p=3;
	memset(sel, 0, sizeof(sel));
	for (int i = 1; i <= n; ++i) {
		cin >> s[i] >> t[i] >> p[i];
		s1.push({s[i], i});
		s2.push({t[i], i});
		s3.push({p[i], i});
	}
	int p = n/2, s = n/2, t = n/2;
	int ans = 0;
	while (1) {
		int mxw = 0, typ = -1;
		while (!s1.empty() && sel[s1.top().second] != 0) s1.pop();
		while (!s2.empty() && sel[s2.top().second] != 0) s2.pop();
		while (!s3.empty() && sel[s3.top().second] != 0) s3.pop();
		while (!sp.empty() && sel[sp.top().second] != 1) sp.pop();
		while (!st.empty() && sel[st.top().second] != 1) st.pop();
		while (!tp.empty() && sel[tp.top().second] != 2) tp.pop();
		while (!ts.empty() && sel[ts.top().second] != 2) ts.pop();
		while (!ps.empty() && sel[ps.top().second] != 3) ps.pop();
		while (!pt.empty() && sel[pt.top().second] != 3) pt.pop();
		if (!s1.empty()) {
			auto to = s1.top();
			if (s && to.first > mxw) {
				mxw = to.first;
				typ = 0;
			}
			if (t && !st.empty() && to.first + st.top().first > mxw) {
				mxw = to.first + st.top().first;
				typ = 1;
			}
			if (p && !sp.empty() && to.first + sp.top().first > mxw) {
				mxw = to.first + sp.top().first;
				typ = 2;
			}
			if (t && !sp.empty() && !pt.empty() && to.first + pt.top().first + sp.top().first > mxw) {
				mxw = to.first + pt.top().first + sp.top().first;
				typ = 3;
			}
			if (p && !st.empty() && !tp.empty() && to.first + st.top().first + tp.top().first > mxw) {
				mxw = to.first + st.top().first + tp.top().first;
				typ = 4;
			}
		}
		if (!s2.empty()) {
			auto to = s2.top();
			if (t && to.first > mxw) {
				mxw = to.first;
				typ = 5;
			}
			if (s && !ts.empty() && to.first + ts.top().first > mxw) {
				mxw = to.first + ts.top().first;
				typ = 6;
			}
			if (p && !tp.empty() && to.first + tp.top().first > mxw) {
				mxw = to.first + tp.top().first;
				typ = 7;
			}
			if (s && !tp.empty() && !ps.empty() && to.first + tp.top().first + ps.top().first > mxw) {
				mxw = to.first + tp.top().first + ps.top().first;
				typ = 8;
			}
			if (p && !ts.empty() && !sp.empty() && to.first + ts.top().first + sp.top().first > mxw) {
				mxw = to.first + ts.top().first + sp.top().first;
				typ = 9;
			}
		}
		if (!s3.empty()) {
			auto to = s3.top();
			if (p && to.first > mxw) {
				mxw = to.first;
				typ = 10;
			}
			if (s && !ps.empty() && to.first + ps.top().first > mxw) {
				mxw = to.first + ps.top().first;
				typ = 11;
			}
			if (t && !pt.empty() && to.first + pt.top().first > mxw) {
				mxw = to.first + pt.top().first;
				typ = 12;
			}
			if (s && !pt.empty() && !ts.empty() && to.first + pt.top().first + ts.top().first > mxw) {
				mxw = to.first + pt.top().first + ts.top().first;
				typ = 13;
			}
			if (t && !ps.empty() && !st.empty() && to.first + ps.top().first + st.top().first > mxw) {
				mxw = to.first + ps.top().first + st.top().first;
				typ = 14;
			}
		}
		if (typ == -1) break;
		ans += mxw;
		if (typ == 0) { --s; cs(s1); }
		else if (typ == 1) { --t; cs(s1); ct(st); }
		else if (typ == 2) { --p; cs(s1); cp(sp); }
		else if (typ == 3) { --t; cs(s1); cp(sp); ct(pt); }
		else if (typ == 4) { --p; cs(s1); ct(st); cp(tp); }
		else if (typ == 5) { --t; ct(s2); }
		else if (typ == 6) { --s; ct(s2); cs(ts); }
		else if (typ == 7) { --p; ct(s2); cp(tp); }
		else if (typ == 8) { --s; ct(s2); cp(tp); cs(ps); }
		else if (typ == 9) { --p; ct(s2); cs(ts); cp(sp); }
		else if (typ == 10) { --p; cp(s3); }
		else if (typ == 11) { --s; cp(s3); cs(ps); }
		else if (typ == 12) { --t; cp(s3); ct(pt); }
		else if (typ == 13) { --s; cp(s3); ct(pt); cs(ts); }
		else if (typ == 14) { --t; cp(s3); cs(ps); ct(st); }
	}
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int tc;
	cin >> tc;
	while (tc--) solve();
}

闲话

祝大家 CSP2026 rp++,不会像我一样在考场上思考 1.5h 网络流。

原文
存档评论10 条抓取于