Luogu P3358 最长k可重区间集问题

题目描述

https://www.luogu.com.cn/problem/P3358

简要题意:给定 $n$ 个开区间和 $k$,每个区间的价值为区间长度,要求选择尽量多的开区间,使得不存在任何一个点不覆盖了 $k$ 次以上,且选择的区间的价值最大

$n\le 500,k\le 3$

Solution

首先为了方便,将开区间变成闭区间

我们参照 志愿者招募 这题的建图方法,可以得到如下建图

建图:

$s$ 连 $1$,容量为 $k$,费用为 $0$

$i$ 连 $i+1$,$i\in[1,n-1]$,容量为 $k$,费用为 $0$

$n$ 连 $t$,容量为 $k$,费用为 $0$

对于一个区间 $l$ 连 $r+1$,容量为 $1$,费用为 $-(r-l+1)$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#define maxn 1010
#define INF 1000000000
using namespace std;

int n, k;

struct Edge {
int to, next, w, fi;
} e[1000000]; int c1, head[maxn];
inline void add_edge(int u, int v, int w, int fi) {
e[c1].to = v; e[c1].w = w; e[c1].fi = fi;
e[c1].next = head[u]; head[u] = c1++;
}

inline void Add_edge(int u, int v, int w, int fi) {
add_edge(u, v, w, fi); add_edge(v, u, 0, -fi);
}

struct node {
int x, y;
} a[maxn];

int b[maxn], cnt;
void init_hash() {
for (int i = 1; i <= n; ++i) b[i] = a[i].x, b[i + n] = a[i].y;
sort(b + 1, b + 2 * n + 1); cnt = unique(b + 1, b + 2 * n + 1) - b - 1;
for (int i = 1; i <= n; ++i) {
a[i].x = lower_bound(b + 1, b + cnt + 1, a[i].x) - b;
a[i].y = lower_bound(b + 1, b + cnt + 1, a[i].y) - b;
}
}

int pre[maxn], la[maxn], dis[maxn], fl[maxn];
bool vis[maxn]; int s, t;
bool spfa() {
fill(dis, dis + maxn, INF); dis[s] = 0;
fill(vis, vis + maxn, 0); vis[s] = 1;
deque<int> Q; Q.push_front(s); pre[t] = -1; fl[s] = INF;
while (!Q.empty()) {
int u = Q.front(); Q.pop_front(); vis[u] = 0;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to, w = e[i].w, fi = e[i].fi;
if (w > 0 && dis[v] > dis[u] + fi) {
dis[v] = dis[u] + fi; pre[v] = u;
fl[v] = min(fl[u], w); la[v] = i;
if (vis[v]) continue; vis[v] = 1;
if (Q.empty() || dis[v] <= dis[Q.front()]) Q.push_front(v);
else Q.push_back(v);
}
}
}
return ~pre[t];
}

int mc, mf;
void mcmf() {
while (spfa()) {
int now = t;
mc += fl[t] * dis[t]; mf += fl[t];
while (now != s) {
e[la[now]].w -= fl[t];
e[la[now] ^ 1].w += fl[t];
now = pre[now];
}
}
}

int main() { fill(head, head + maxn, -1);
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i].x >> a[i].y, --a[i].y;
init_hash(); s = 0; t = cnt + 1;
for (int i = 0; i <= cnt; ++i) Add_edge(i, i + 1, k, 0);
for (int i = 1; i <= n; ++i) Add_edge(a[i].x, a[i].y + 1, 1, b[a[i].x] - b[a[i].y] - 1);
mcmf(); cout << -mc << "\n";
return 0;
}

这个题目还有另外一种建图方法

我们考虑将一系列不相交的区间称为一条路径

那么我们最多选择 $k$ 条路径

所以我们考虑这样建图:

$s$ 连 $s’$,容量为 $k$,费用为 $0$

$s’$ 连 $i$,容量为 $1$,费用为 $0$

$i$ 连 $i’$,容量为 $1$,费用为 $-(r[i]-l[i]+1)$

$i’$ 连 $j$,容量为 $1$,费用为 $0$,其中 $j$ 的左端点大于 $i$ 的右端点

$i’$ 连 $t$,容量为 $1$,费用为 $0$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#define cn const node
#define maxn 1010
#define INF 1000000000
using namespace std;

int n, k;

struct Edge {
int to, next, w, fi;
} e[1000000]; int c1, head[maxn];
inline void add_edge(int u, int v, int w, int fi) {
e[c1].to = v; e[c1].w = w; e[c1].fi = fi;
e[c1].next = head[u]; head[u] = c1++;
}

inline void Add_edge(int u, int v, int w, int fi) {
add_edge(u, v, w, fi); add_edge(v, u, 0, -fi);
}

struct node {
int x, y;
} a[maxn];

int pre[maxn], la[maxn], dis[maxn], fl[maxn];
bool vis[maxn]; int s, t, _s;
bool spfa() {
fill(dis, dis + maxn, INF); dis[s] = 0;
fill(vis, vis + maxn, 0); vis[s] = 1;
deque<int> Q; Q.push_front(s); pre[t] = -1; fl[s] = INF;
while (!Q.empty()) {
int u = Q.front(); Q.pop_front(); vis[u] = 0;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to, w = e[i].w, fi = e[i].fi;
if (w > 0 && dis[v] > dis[u] + fi) {
dis[v] = dis[u] + fi; pre[v] = u;
fl[v] = min(fl[u], w); la[v] = i;
if (vis[v]) continue; vis[v] = 1;
if (Q.empty() || dis[v] <= dis[Q.front()]) Q.push_front(v);
else Q.push_back(v);
}
}
}
return ~pre[t];
}

int mc, mf;
void mcmf() {
while (spfa()) {
int now = t;
mc += fl[t] * dis[t]; mf += fl[t];
while (now != s) {
e[la[now]].w -= fl[t];
e[la[now] ^ 1].w += fl[t];
now = pre[now];
}
}
}

int main() { fill(head, head + maxn, -1);
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> k; s = 0; _s = 2 * n + 1; t = 2 * n + 2;
for (int i = 1; i <= n; ++i) cin >> a[i].x >> a[i].y, --a[i].y;
sort(a + 1, a + n + 1, [](cn &u, cn &v) { return u.x < v.x; });
Add_edge(s, _s, k, 0);
for (int i = 1; i <= n; ++i) {
Add_edge(_s, i, 1, 0);
Add_edge(i, i + n, 1, a[i].x - a[i].y - 1);
Add_edge(i + n, t, 1, 0);
for (int j = i + 1; j <= n; ++j)
if (a[j].x > a[i].y) Add_edge(i + n, j, 1, 0);
}
mcmf(); cout << -mc << "\n";
return 0;
}