- 平面中有 个点,坐标为 ,权值为 。每一行每一列最多只能选一个点,最大化选点个数 并输出。
- 同时对于 ,最大化选出 个点的权值和。
- 。
对于一个点 ,如果选了它则第 行第 列都不能再选其它点,如果把第 行向第 列连边,这是一个二分图最大匹配模型。跑一遍最大流即可求出 的值。
考虑第二问其实就是二分图最大权匹配,直接暴力跑 遍费用流。令 ,时间复杂度 ,不知道能不能过。
使用 acl 的费用流,单次时间复杂度为 ,总复杂度 ,可以通过。
#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";
}