最小圆覆盖

传送门HDU3007

描述

Each person had do something foolish along with his or her growth.But,when he or she did this that time,they could not predict that this thing is a mistake and they will want this thing would rather not happened.
The world king Sconbin is not the exception.One day,Sconbin was sleeping,then swakened by one nightmare.It turned out that his love letters to Dufein were made public in his dream.These foolish letters might ruin his throne.Sconbin decided to destroy the letters by the military exercises’s opportunity.The missile is the best weapon.Considered the execution of the missile,Sconbin chose to use one missile with the minimum destruction.
Sconbin had writen N letters to Dufein, she buried these letters on different places.Sconbin got the places by difficult,he wants to know where is the best place launch the missile,and the smallest radius of the burst area. Let’s help Sconbin to get the award.

输入

There are many test cases.Each case consists of a positive integer N(N<500,^V^,our great king might be a considerate lover) on a line followed by N lines giving the coordinates of N letters.Each coordinates have two numbers,x coordinate and y coordinate.N=0 is the end of the input file.

输出

For each case,there should be a single line in the output,containing three numbers,the first and second are x and y coordinates of the missile to launch,the third is the smallest radius the missile need to destroy all N letters.All output numbers are rounded to the second digit after the decimal point.

样例

  • Input
    3
    1.00 1.00
    2.00 2.00
    3.00 3.00
    0
  • Output
    2.00 2.00 1.41

思路

  • 题意:狗血剧情懒得讲,裸最小覆盖圆;多组输入n,n=0停止,下面n行为每个点的坐标。
  • 随机增量法是一个可以在 期望 O(n)时间内求出最小圆覆盖的算法,首先它的算法流程是这样的
    • 枚举第一个点 i,若不在目前圆内,设它为圆心
    • 枚举第二个点 j,若不在当前圆内,设当前圆为以 i,j 为直径的圆
    • 枚举第三个点 k,若不在当前圆内,设当前圆为 i,j,k 的外接圆
  • 正确性:显然最优解一定是两个点为直径的圆或者一个三角形的外接圆,否则肯定能缩的更小。那么这么枚举的正确性是比较显然的了。
  • 时间复杂度:
    这是一个重点,这么做看似是O(n^3) 的,不过对于随机顺序的点,是可以期望O(n) 的。下面考虑证明:
    显然,最后一层循环枚举从 1~j,只要进入循环就一定要跑完,所以是O(j) 的
    考虑倒数第二层循环,什么情况下会进入第三层循环呢?仅当 j 不在前 j-1 个点形成的圆中,考虑 j 个点形成的圆是由三个点确定的,那么第 j 个 (最后一个点) 若是三个点之一,则需要扩大圆,否则不需要进入第三层循环,这个概率是3/j 的,所以第二层的复杂度是O(i) 的同理,第一层的复杂度就是O(n)的了
    当然,这是基于随机数据的期望复杂度,所以我们一般需要手动打乱排列

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
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const double eps=1e-8;
struct Point {
double x,y;
} p[505];
double dis(const Point &a,const Point &b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Point circumcenter(const Point &a,const Point &b,const Point &c) { //返回三角形的外心
Point ret;
double a1=b.x-a.x,b1=b.y-a.y,c1=(a1*a1+b1*b1)/2;
double a2=c.x-a.x,b2=c.y-a.y,c2=(a2*a2+b2*b2)/2;
double d=a1*b2-a2*b1;
ret.x=a.x+(c1*b2-c2*b1)/d;
ret.y=a.y+(a1*c2-a2*c1)/d;
return ret;
}
void MCC(Point *p,int n,Point &c,double &r) { //c为圆心,r为半径
random_shuffle(p,p+n);
c=p[0];
r=0;
for(int i=1; i<n; i++) {
if(dis(p[i],c)>r+eps) { //第一个点
c=p[i];r=0;
for(int j=0; j<i; j++)
if(dis(p[j],c)>r+eps) { //第二个点
c.x=(p[i].x+p[j].x)/2;
c.y=(p[i].y+p[j].y)/2;
r=dis(p[j],c);
for(int k=0; k<j; k++)
if(dis(p[k],c)>r+eps) { //第三个点
//求外接圆圆心,三点必不共线
c=circumcenter(p[i],p[j],p[k]);
r=dis(p[i],c);
}
}
}
}
}
int main() {
int n;
Point c;
double r;
while(~scanf("%d",&n) && n) {
for(int i=0; i<n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
random_shuffle(p,p+n);
MCC(p,n,c,r);
printf("%.2lf %.2lf %.2lf\n",c.x,c.y,r);
}
return 0;
}
Donate comment here