HDU3656 Fire Station Dancing Links

这题 Dancing Links 写的好纠结,二分写挂了一次。

这题的思路是预处理出所有点对的距离,然后进行对半径二分,跑重复覆盖即可。

注意二分的时候不能对距离二分,那样会超时,我们考虑极限情况,也就是消防队恰好可以到达某一房子,也就是距离相等时的情况,这样,我们就维护一个 que 数组表示可以选到的距离,对此进行二分。

我的代码:

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

const int oo = 0x3f3f3f3f;
const int MAX = 120;
const int head = 0;

int up[MAX * MAX], down[MAX * MAX], left[MAX * MAX], right[MAX * MAX];
int cnt[MAX], h[MAX], col[MAX * MAX];
int K, ans;
int nc;

void remove(const int& c) {
for (int i = down[c]; i != c; i = down[i]) {
left[right[i]] = left[i];
right[left[i]] = right[i];
cnt[col[i]]--;
}
}

void resume(const int& c) {
for (int i = up[c]; i != c; i = up[i]) {
left[right[i]] = i;
right[left[i]] = i;
cnt[col[i]]++;
}
}

int evalute() {
bool vis[MAX] = {0};
int ret = 0;
for (int i = right[head]; i != head; i = right[i]) {
if (!vis[i]) {
ret++;
vis[i] = true;
for (int j = down[i]; j != i; j = down[j]) {
for (int k = right[j]; k != j; k = right[k]) {
vis[col[k]] = true;
}
}
}
}
return ret;
}

bool dfs(const int& k) {
if (k + evalute() > ans) {
return false;
}
if (right[head] == head) {
return true;
}
int s = oo, c = 0;
for (int i = right[head]; i != head; i = right[i]) {
if (cnt[i] < s) {
s = cnt[i];
c = i;
if (cnt[i] <= 1) {
break;
}
}
}
for (int i = down[c]; i != c; i = down[i]) {
remove(i);
for (int j = right[i]; j != i; j = right[j]) {
remove(j);
}
if (dfs(k + 1)) {
return true;
}
for (int j = left[i]; j != i; j = left[j]) {
resume(j);
}
resume(i);
}
return false;
}

void init() {
for (int i = 0; i <= nc; i++) {
h[i] = -1;
left[i] = i - 1;
right[i] = i + 1;
up[i] = down[i] = i;
cnt[i] = 0;
col[i] = i;
}
left[head] = nc;
right[nc] = head;
K = nc;
for (int i = 1; i <= nc; i++) {
h[i] = -1;
}
}

void add(const int& r, const int& c) {
K++;
col[K] = c;
up[K] = c;
down[K] = down[c];
if (h[r] == -1) {
h[r] = right[K] = K;
}
left[K] = h[r];
right[K] = right[h[r]];
left[right[K]] = K;
right[left[K]] = K;
up[down[K]] = K;
down[up[K]] = K;
}

struct Node {
int x, y;
} city[MAX];
int dis[MAX][MAX];
int que[MAX * MAX], top;

int sqr(int x) { return x * x; }

int operator*(const Node& a, const Node& b) {
return sqr(a.x - b.x) + sqr(a.y - b.y);
}

void build(const int& mid) {
init();
for (int i = 1; i <= nc; i++) {
for (int j = 1; j <= nc; j++) {
if (dis[i][j] <= mid) {
add(i, j);
}
}
}
}

void run() {
int l = 0, r = top;
int mid, ans;
while (l <= r) {
mid = (l + r) / 2;
build(que[mid]);
if (dfs(0)) {
ans = que[mid];
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%.6f\n", sqrt((double)ans));
}

int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &nc, &ans);
for (int i = 1; i <= nc; i++) {
scanf("%d%d", &city[i].x, &city[i].y);
}
top = 0;
// here we deal with the distance between city and city.
for (int i = 1; i <= nc; i++) {
for (int j = 1; j <= nc; j++) {
que[top++] = dis[i][j] = city[i] * city[j];
}
}
int tmp = top;
sort(que, que + top);
top = 1;
for (int i = 1; i < tmp; i++) {
if (que[i] != que[i - 1]) {
que[top++] = que[i];
}
}
run();
}
return 0;
}