「HDU3622」 Bomb Game 题解

题目

原题链接 HDU3622

Problem Description

Robbie is playing an interesting computer game. The game field is an unbounded 2-dimensional region. There are N rounds in the game. At each round, the computer will give Robbie two places, and Robbie should choose one of them to put a bomb. The explosion area of the bomb is a circle whose center is just the chosen place. Robbie can control the power of the bomb, that is, he can control the radius of each circle. A strange requirement is that there should be no common area for any two circles. The final score is the minimum radius of all the N circles.

Robbie has cracked the game, and he has known all the candidate places of each round before the game starts. Now he wants to know the maximum score he can get with the optimal strategy.

Input

The first line of each test case is an integer N (2 <= N <= 100), indicating the number of rounds. Then N lines follow. The i-th line contains four integers x1i, y1i, x2i, y2i, indicating that the coordinates of the two candidate places of the i-th round are (x1i, y1i) and (x2i, y2i). All the coordinates are in the range [-10000, 10000].

Output

Output one float number for each test case, indicating the best possible score. The result should be rounded to two decimal places.

Sample Input

Input
1
2
3
4
5
6
2
1 1 1 -1
-1 -1 -1 1
2
1 1 -1 -1
1 -1 -1 1

Sample Output

Output
1
2
1.41
1.00

题目大意

给定你 NN 组炸弹,每组有两个,现要挑选并引爆这其中 NN 个炸弹,并且每组需要且只能引爆一个,已知这 2×N2 \times N 个炸弹的位置,每个炸弹的爆炸半径都是一样的,请你求出一个最大的爆炸半径使得引爆的每个炸弹的爆炸范围不重合。

每组数据输出一个小数,精确到小数点后两位,表示最大使得引爆的炸弹的爆炸范围不重合的半径。

2N1002 \leq N \leq 100 10000x,y10000-10000 \leq x, y \leq 10000

解题

不难发现,对于组内的两个炸弹,一个是否选择决定了另一个是否选择;对于组外的两个炸弹,如果两个炸弹的距离小于等于二倍的半径,则只能选一个,一个是否选择也决定了另一个是否选择。

所以不难得出这是一个 2-SAT 问题,二分当前情况半径,以炸弹相互冲突为条件连边,通过 Tarjan 判断当前情况是否合法,若合法则增大半径尝试,若不合法则减少半径尝试。

连边:

  • 对于每组的两个炸弹:每个炸弹分别从自身的 false 点连向另一个炸弹 true 点,从自身的 true 点连向另一个炸弹的 true

  • 对于组外的两个炸弹:如果这两个炸弹的距离小于等于二倍半径,则分别从两个炸弹的 true 点连向另一个炸弹的 false

最后再通过 Tarjan 求出强连通分量,如果存在任何一个炸弹的 true 点和 false 点在同一个强连通分量,则不合法,否则合法。

代码

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
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 400 + 5;
const int MAXM = 80000 + 5;
int n;
struct Edge {
int e, next;
}ed[MAXM];
int first[MAXN], en;
void add_edge(int s, int e) {
en++;
ed[en].next = first[s];
first[s] = en;
ed[en].e = e;
}
int nn;
struct Node {
int x, y;
}nd[MAXN];
int dfn[MAXN], low[MAXN], s[MAXN], belong[MAXN], size, t, cnt;
bool instack[MAXN];
void dfs(int now) {
t++;
dfn[now] = low[now] = t;
instack[now] = true;
s[++size] = now;
for(int i = first[now]; i; i = ed[i].next) {
int e = ed[i].e;
if(!dfn[e]) {
dfs(e);
low[now] = min(low[now], low[e]);
}
else {
if(instack[e]) low[now] = min(low[now], dfn[e]);
}
}
if(low[now] == dfn[now]) {
cnt++;
belong[now] = cnt;
while(s[size] != now) {
belong[s[size]] = cnt;
instack[s[size]] = false;
size--;
}
size--;
instack[now] = false;
}
}
double get_dis(int i, int j) {
return sqrt((nd[i].x - nd[j].x) * (nd[i].x - nd[j].x) + (nd[i].y - nd[j].y) * (nd[i].y - nd[j].y));
}
bool SAT() {
for(int i = 1; i <= nn; i++)
if(!dfn[i]) dfs(i);
for(int i = 1; i <= nn; i += 2) {
if(belong[i] == belong[i + 1]) return false;
}
return true;
}
bool check(double r) {
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(instack, 0, sizeof(instack));
memset(first, 0, sizeof(first));
t = cnt = en = 0;
for(int i = 1; i <= nn; i += 4) {
add_edge(i, i + 3);
add_edge(i + 3, i);
add_edge(i + 1, i + 2);
add_edge(i + 2, i + 1);
}
for(int i = 1; i <= nn; i += 4) {
for(int j = 1; j <= nn; j += 4) {
if(i == j) continue;
if(get_dis(i, j) < 2 * r) add_edge(i, j + 1);
if(get_dis(i, j + 2) < 2 * r) add_edge(i, j + 3);
if(get_dis(i + 2, j) < 2 * r) add_edge(i + 2, j + 1);
if(get_dis(i + 2, j + 2) < 2 * r) add_edge(i + 2, j + 3);
}
}
return SAT();
}
int main() {
while(cin >> n) {
nn = 0;
for(int i = 1; i <= n; i++) {
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
nn += 2;
nd[nn - 1].x = x1, nd[nn - 1].y = y1;
nd[nn].x = x1, nd[nn - 1].y = y1;
nn += 2;
nd[nn - 1].x = x2, nd[nn - 1].y = y2;
nd[nn].x = x2, nd[nn - 1].y = y2;
}
double l = 0, r = 10000;
double ans = 0;
while(r - l >= 0.000001) {
double mid = double((l + r) / 2);
if(check(mid)) {
l = mid;
ans = mid;
}
else r = mid;
}
printf("%.2lf\n", ans);
}
return 0;
}