跳到正文

题解

ABC247G 题解

sunkuangzheng约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年7月24日
更新
2026年11月9日
篇幅
约 3 分钟

版本

  1. v1当前

ABC247G\textbf{ABC247G}

  • 平面中有 nn 个点,坐标为 (ai,bi)(a_i,b_i),权值为 cic_i。每一行每一列最多只能选一个点,最大化选点个数 kk 并输出。
  • 同时对于 i∈[1,k]i \in [1,k],最大化选出 ii 个点的权值和。
  • 1≤n≤3⋅104,1≤ai,bi≤1501 \le n \le 3 \cdot 10^4,1 \le a_i,b_i \le 150。

对于一个点 (x,y)(x,y),如果选了它则第 xx 行第 yy 列都不能再选其它点,如果把第 xx 行向第 yy 列连边,这是一个二分图最大匹配模型。跑一遍最大流即可求出 kk 的值。

考虑第二问其实就是二分图最大权匹配,直接暴力跑 kk 遍费用流。令 N=300N = 300,时间复杂度 O(N3n)\mathcal O(N^3n),不知道能不能过。

使用 acl 的费用流,单次时间复杂度为 O(Nnlog⁡n)\mathcal O(Nn \log n),总复杂度 O(N2nlog⁡n)\mathcal O(N^2n\log n),可以通过。

C++
#include<bits/stdc++.h>
#include <atcoder/all>
using ll = long long;
const int N = 5e5+5;
const ll inf = 1e10;
using namespace std;
using namespace atcoder;
int T,n,a[N],b[N],c[N],s,ss,tt,x;
int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n;
    for(int i = 1;i <= n;i ++) cin >> a[i] >> b[i] >> c[i];
    auto build = [&](int lim){
        mcf_graph<int,ll> g(302);
        ss = 0,tt = 301;
        for(int i = 1;i <= 150;i ++) g.add_edge(ss,i,1,0),g.add_edge(i+150,tt,1,0);
        for(int i = 1;i <= n;i ++) g.add_edge(a[i],b[i]+150,1,inf - c[i]);
        return g.flow(ss,tt,lim);
    };
    cout << (x = build(1e9).first) << "\n";
    for(int i = 1;i <= x;i ++) cout << - build(i).second + inf * i << "\n";
}
原文