我再次学会了网络流
松绒灼茁桌捉浊卓焯琢 · · 题解
前言
本题如果仅作为 CSP-S T1 而言,存在一个非常简短(但对有些人而言不好想)的解法——让所有人直奔第一志愿,再把唯一爆满的社团里亏损最小的人调剂走。
然而,这极度依赖于“容量均为
本题解旨在跳出特殊性质的局限,提供一种基于“模拟费用流”的通用视角。尽管有“大炮打蚊子”之嫌,但该解法能系统性地解决任意容量、更多社团(合理范围内)的情况,具有一定的启发性与推广价值。
题意简述
题目链接
有
网络流
对于这种简单题,我们显然是可以用网络流的,于是我们考虑这样的建模方式:
- 设点集
V = \{S, T, G_1, G_2, G_3, 1, 2, \dots, n\} ,其中S 为源点,T 为汇点。 - 边集
E 包含: -
我们需要在这个网络中流满
模拟费用流
模拟费用流即指我们用一种高效的方式去增广,取代原本要遍历整个图求解最短路的方式。
在此之前,我们要找到所有可能的增广路有哪些。
容易发现我们只需要考虑所有的简单增广路径即可,即不需要考虑有环的情况。因为我们在一个无正环的残量网络增广必定不会出现一个含环的增广路(不然就和前面的无正环矛盾了)。
而简单的增广路径只能在源汇点之间经过社团集合
- 经过一个社团:
A_{3}^{1}=3 种。 - 经过两个社团:
A_{3}^{2}=6 种。 - 经过三个社团:
A_{3}^{3}=6 种。
简单增广路径的总数为
因此即可得到以下
注意,以上的
这样,我们就考虑维护以下
-
-
-
-
-
-
- 另外三种情况为上述后三者的反向转移,即
W_{i,2,1}, W_{i,3,1}, W_{i,3,2} 。
这样,原本
以
注意到
这说明我们直接分别取对应堆的堆顶相加即可得到该路径的最大增广费用。
复杂度
- 时间复杂度:算法总共进行
n 次增广。单次增广需要遍历O(|G|!) 种路径情况;每次增广最多涉及O(|G|) 个人员的社团变动,每次变动会向O(|G|) 个堆中推入新状态。结合惰性删除机制,全局每个元素最多进出堆O(|G|^2) 次,均摊到单次增广的堆操作时间为O(|G|^2 \log n) 。因此总时间复杂度为O\big(n(|G|! + |G|^2\log n)\big) ,在本题中|G|=3 ,常数极小,可以通过。 - 空间复杂度:全局累计推入堆中的元素总量为
O(n |G|^2) ,辅助数组空间为O(n) ,故总空间复杂度为O(n |G|^2) 。
代码
:::warning[有点长]
#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 网络流。