hdu3932 Groundhog Build Home

Link

Solution

so that the longest distance between xiaomi’s home and the other groundhog’s home is minimum.

二分答案?然而二分之后有什么用呢? 注意到,选取一个点,要让平面内所有点到它的距离最近,转化一下,不就是最小覆盖圆吗。。 # Tips 在计算几何中,当一般算法题的套路用不上(二分答案。。。),就研究一下求解问题的几何性质(圆。) # Code

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
//Code by Lucida
#include<bits/stdc++.h>
#define red(x) scanf("%d",&x)
#define fred(x) scanf("%lf",&x)
template <class T> inline bool chkmx(T &a,const T &b){return a<b?a=b,1:0;}
template <class T> inline bool chkmn(T &a,const T &b){return a>b?a=b,1:0;}
const int MAXN=1000+10;
using std::max_element;
using std::random_shuffle;
ld dist(point a,point b){return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}
bool parl(vec a,vec b){return fcmp(outer(a,b))==0;}
struct line
{
point a;vec v;
line(point _a,vec _v):a(_a),v(_v){}
};
point cross(line a,line b){return a.a+a.v*outer(a.a-b.a,b.v)/outer(b.v,a.v);}
vec rotate(vec a,ld rad){return vec(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}
point excenter(point a,point b,point c)
{
if(parl(b-a,c-a))
{
ld lens[]={dist(a,b),dist(a,c),dist(b,c)};
switch(max_element(lens,lens+3)-lens)
{
case 0:return (a+b)/2;break;
case 1:return (a+c)/2;break;
case 2:return (b+c)/2;break;
}
}
else return cross(line((a+b)/2,rotate(b-a,pi/2)),line((a+c)/2,rotate(c-a,pi/2)));
}
void WORK(int n)
{
static point p[MAXN];
for(int i=1;i<=n;i++) fred(p[i].x),fred(p[i].y);
random_shuffle(p+1,p+1+n);
point O=p[1];ld r=0;
for(int i=2;i<=n;i++)
{
if(fcmp(dist(O,p[i])-r)>0)
{
O=p[i],r=0;
for(int j=1;j<i;j++)
{
if(fcmp(dist(O,p[j])-r)>0)
{
O=(p[i]+p[j])/2,r=dist(p[i],p[j])/2;
for(int k=1;k<j;k++)
{
if(fcmp(dist(O,p[k])-r)>0)
{
O=excenter(p[i],p[j],p[k]);
r=dist(O,p[i]);
}
}
}
}
}
}
printf("(%.1lf,%.1lf).\n%.1f\n",O.x,O.y,r);
}
int main()
{
freopen("input","r",stdin);
int x,y,n;
while((~red(x)) && (~red(y)) && (~red(n))) WORK(n);
return 0;
}

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2017 Hey,Lucida here. All Rights Reserved.

Lucida Lu 保留所有权利