前言
本题如果仅作为 CSP-S T1 而言,存在一个非常简短(但对有些人而言不好想)的解法——让所有人直奔第一志愿,再把唯一爆满的社团里亏损最小的人调剂走。
然而,这极度依赖于“容量均为 ”这一特殊限制。当各社团容量为任意上限 ,或社团数量进一步增加时,上述贪心将彻底失效。
本题解旨在跳出特殊性质的局限,提供一种基于“模拟费用流”的通用视角。尽管有“大炮打蚊子”之嫌,但该解法能系统性地解决任意容量、更多社团(合理范围内)的情况,具有一定的启发性与推广价值。
题意简述
有 个人和 个社团,每个人加入不同社团有不同的价值。每个社团人数不能超过 ,求所有人全部加入社团后的最大收益。
网络流
对于这种简单题,我们显然是可以用网络流的,于是我们考虑这样的建模方式:
- 设点集 ,其中 为源点, 为汇点。
- 边集 包含:
- ,容量为 ,费用为 ();
- ,容量为 ,费用为 ();
- ,容量为 ,费用为 ()。
我们需要在这个网络中流满 单位流量的最大费用。直接跑费用流算法显然会超时,因此考虑模拟费用流。
模拟费用流
模拟费用流即指我们用一种高效的方式去增广,取代原本要遍历整个图求解最短路的方式。
在此之前,我们要找到所有可能的增广路有哪些。
容易发现我们只需要考虑所有的简单增广路径即可,即不需要考虑有环的情况。因为我们在一个无正环的残量网络增广必定不会出现一个含环的增广路(不然就和前面的无正环矛盾了)。
而简单的增广路径只能在源汇点之间经过社团集合 的一个不重复子排列,经过社团的数量只可能是 :
- 经过一个社团: 种。
- 经过两个社团: 种。
- 经过三个社团: 种。
简单增广路径的总数为 ,本题 ,完全可以接受。
因此即可得到以下 种情况:
- :意思是将 加入 组。
- :意思是将 加入 组。
- :意思是将 加入 组。
- :意思是将 加入 组,并将原本在 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 ,再将原本 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 ,再将原本 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 ,再将原本 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 ,再将原本 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 ,再将原本 组的 移去 。
- :意思是将 加入 组,并将原本在 组的 移去 ,再将原本 组的 移去 。
注意,以上的 一定是互不相等的,因为 一定来自未分配集合, 一定来自两个不同的 ,这三个集合两两互不相交的。
这样,我们就考虑维护以下 类转移代价信息:
- :表示将一个未分配的 流向 的代价(即 )。
- :表示将一个未分配的 流向 的代价(即 )。
- :表示将一个未分配的 流向 的代价(即 )。
- :表示将一个原本分配给 的 改为流向 的代价(即 )。
- :表示将一个原本分配给 的 改为流向 的代价(即 )。
- :表示将一个原本分配给 的 改为流向 的代价(即 )。
- 另外三种情况为上述后三者的反向转移,即 。
这样,原本 种增广情况就都可以转化为若干个 代价的相加。我们只需要维护 个大根堆,分别维护上述 种转移对应的合法候选人的最大值。
以 型增广为例,其增加的总费用为 。我们要求解:
注意到 、、,由于这三个候选集合两两互不相交,人员的选取完全独立,因此多元最大值可以拆分:
这说明我们直接分别取对应堆的堆顶相加即可得到该路径的最大增广费用。
复杂度
- 时间复杂度:算法总共进行 次增广。单次增广需要遍历 种路径情况;每次增广最多涉及 个人员的社团变动,每次变动会向 个堆中推入新状态。结合惰性删除机制,全局每个元素最多进出堆 次,均摊到单次增广的堆操作时间为 。因此总时间复杂度为 ,在本题中 ,常数极小,可以通过。
- 空间复杂度:全局累计推入堆中的元素总量为 ,辅助数组空间为 ,故总空间复杂度为 。
代码
有点长
#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 网络流。