「网络流24题」最长k可重线段集问题-题解

题目传送门: 「Luogu P3357」最长k可重线段集问题

题目大意

给定一个开线段的集合$\mathbf{I}$,从$\mathbf{I}$中取出一些开线段,组成集合$\mathbf{S}$
使对于任何一条直线$x=p$,满足$\mathbf{S}$中与$x=p$相交的线段数不超过$k$,且$\mathbf{S}$中开线段总长度最大

求最大的长度

题解

「Luogu P3358」最长k可重区间集问题类似
仅修改一下区间的输入部分即可

将每条线段映射到x轴上,发现可能不对,因为可能会存在垂直于x轴的线段,像P3358一样建图会省略掉这条线段

所以要将每个点扩大一倍,将左右端点x坐标相同的区间$(x, x)$更改为$(2x, 2x+1)$,使得不存在与x轴垂直的线段
如果左右端点x坐标不相同,则将$(x_1, x_2)$更改为$(2x_1+1, 2x_2)$

大概就是这么个道理:

剩下的做法和P3358一样,P3358题解

代码

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <bits/stdc++.h>
using namespace std;

inline int read() {
int x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();}
return x * f;
}

const int maxn = 2010;
const int inf = 0x3f3f3f3f;
const int ninf = 0xc0c0c0c0;

int n, m, s, t, ansflow;
int vis[maxn], d[maxn], p[maxn], a[maxn];
long long anscost;

struct Edge {
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w): from(u), to(v), cap(c), flow(f), cost(w){}
};
vector<Edge> edges;
vector<int> G[maxn];
void add(int u, int v, int c, int w) {
edges.push_back(Edge(u, v, c, 0, w));
edges.push_back(Edge(v, u, 0, 0,-w));
int mm = edges.size();
G[u].push_back(mm - 2);
G[v].push_back(mm - 1);
}

bool BellmanFord(int& flow, long long& cost) {
memset(d, 0xc0, sizeof(d));
memset(vis, 0, sizeof(vis));
d[s] = 0; vis[s] = 1; p[s] = 0; a[s] = inf;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int x = Q.front(); Q.pop();
vis[x] = 0;
for (int i = 0; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (e.cap > e.flow && d[e.to] < d[x] + e.cost) {
d[e.to] = d[x] + e.cost;
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap - e.flow);
if (!vis[e.to]) {
Q.push(e.to);
vis[e.to] = 1;
}
}
}
}
if (d[t] == ninf) return false;
flow += a[t];
cost += (long long)d[t] * (long long)a[t];
for (int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
}
return true;
}

int MaxCostMaxFlow(long long& cost) {
int flow = 0; cost = 0;
while (BellmanFord(flow, cost));
return flow;
}

struct Interval {
int l, r, len;
}inter[510];
int nums[1020], old[1020];

int length(int a, int b, int c, int d) {
return (int)sqrt((long long)(a - c) * (a - c) + (long long)(b - d) * (b - d));
}

int main() {
int n_ = read(), k = read();
for (int i = 1; i <= n_; ++i) {
int a = read(), b = read(), c = read(), d = read();
inter[i].len = length(a, b, c, d);
inter[i].l = a * 2;
inter[i].r = c * 2;
if (a == c) inter[i].r += 1;
else inter[i].l += 1;
nums[i * 2 - 1] = inter[i].l;
nums[i * 2] = inter[i].r;
}

for (int i = 1; i <= n_ * 2; ++i) old[i] = nums[i];
sort(old + 1, old + 1 + n_ * 2);
int len = unique(old + 1, old + 1 + n_ * 2) - old - 1;
for (int i = 1; i <= n_ * 2; ++i) nums[i] = lower_bound(old + 1, old + 1 + len, nums[i]) - old;

for (int i = 1; i <= n_; ++i) {
inter[i].l = nums[i * 2 - 1];
inter[i].r = nums[i * 2];
}
s = 0; n = len + 2; t = n - 1;
add(s, 1, k, 0);
for (int i = 1; i <= len; ++i) {
add(i, i + 1, inf, 0);
}
for (int i = 1; i <= n_; ++i) {
add(inter[i].l, inter[i].r, 1, inter[i].len);
}
ansflow = MaxCostMaxFlow(anscost);
printf("%d\n", anscost);
return 0;
}

「网络流24题」最长k可重线段集问题-题解

https://blog.tonycrane.cc/p/f48af49a.html

作者

TonyCrane

发布于

2020-04-21

更新于

2020-05-05

许可协议