我再次学会了网络流

· · 题解

前言

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

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

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

题意简述

题目链接

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

网络流

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

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

模拟费用流

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

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

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

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

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

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

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

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

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

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

\max_{i,j,k\text{ is valid}}(W_{i,0,3}+W_{j,3,1}+W_{k,1,2})

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

\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})

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

复杂度

代码

:::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 网络流。