zoj 3717 Balloon

本文从WordPress迁移而来, 查看全部WordPress迁移文章

二分答案 + 2-sat判定

题意:n组气球,每组2个,必须且仅能选择一个,使得气球之间不能重叠,气球的半径有多大(所有气球半径相同)

和 hdu 3622 是一样的题目

二分枚举气球半径,2-sat判定是否能找到一个可行解

这题最后输出答案,奥注意精度的问题

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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
#define N 210
#define MAX 10000
#define EPS 1e-4
#define cl(xx,yy) memset((xx),(yy),sizeof((xx)))
#define pb push_back

int n,x[2*N],y[2*N],z[2*N];
vector<int>e[2*N];
int dfn[2*N],low[2*N],belong[2*N],stack[2*N],dcnt,bcnt,top;
bool ins[2*N];

inline double dis(int i,int j){
double rx = (1. * x[i] - x[j])*(1. * x[i] - x[j]);
double ry = (1. * y[i] - y[j])*(1. * y[i] - y[j]);
double rz = (1. * z[i] - z[j])*(1. * z[i] - z[j]);
return sqrt(rx+ry+rz);
}

void build(double L){
for(int i = 0; i < 2*n; i++)
e[i].clear();
for(int i = 0; i < n; i++)
for(int j = i+1; j < n; j++){
double d;
d = dis(i<<1 , j<<1);
if(d - L < EPS){
e[i<<1].pb(j<<1|1); e[j<<1].pb(i<<1|1);
}
d = dis(i<<1 , j<<1|1);
if(d - L < EPS){
e[i<<1].pb(j<<1); e[j<<1|1].pb(i<<1|1);
}
d = dis(i<<1|1 , j<<1);
if(d - L < EPS){
e[i<<1|1].pb(j<<1|1); e[j<<1].pb(i<<1);
}
d = dis(i<<1|1 , j<<1|1);
if(d - L < EPS){
e[i<<1|1].pb(j<<1); e[j<<1|1].pb(i<<1);
}
}
}

void tarjan(int u){
dfn[u] = low[u] = ++dcnt;
stack[++top] = u; ins[u] = true;
for(int i = 0; i < e[u].size(); i++){
int v = e[u][i];
if(!dfn[v]){
tarjan(v);
low[u] = min(low[u],low[v]);
}
else if(ins[v])
low[u] = min(low[u],dfn[v]);
}
if(dfn[u] == low[u]){
++bcnt;
while(true){
int x = stack[top--];
ins[x] = false;
belong[x] = bcnt;
if(x == u) break;
}
}
}

bool check(){
dcnt = bcnt = top = 0;
cl(dfn,0);
cl(ins,false);
for(int i = 0; i < 2*n; i++)
if(!dfn[i])
tarjan(i);
for(int i = 0; i < n; i++)
if(belong[i<<1] == belong[i<<1|1])
return false;
return true;
}

int main(){
while(scanf("%d",&n)!=EOF){
for(int i = 0; i < n; i++){
scanf("%d%d%d",&x[i<<1],&y[i<<1],&z[i<<1]);
scanf("%d%d%d",&x[i<<1|1],&y[i<<1|1],&z[i<<1|1]);
}
double Low = 0;
double High = 40000;
double ans = 0;
while(High - Low > EPS){
double Mid = (Low + High) / 2;
build(Mid);
if(check()){
Low = Mid; ans = Mid;
}
else High = Mid;
}
printf("%.3lf\n",ans/2 - 0.0005);
}
return 0;
}