Buried memory HDU - 3007 最小覆盖圆算法&&模板

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.

Input

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.

Output

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. 

Sample Input

3
1.00 1.00
2.00 2.00
3.00 3.00
0

Sample Output

2.00 2.00 1.41

题意:给你n个点 求一下最小的覆盖圆

题解:这个题解有一个专门的算法,可以作为模板来使用,具体算法描述是这样的

1、在点集中任取三个点A、B、C。

2、做一个包含ABC三点的最小圆,圆周可能通过这三点,也可能只通过其中两点,但包含第三个点。后一种情况圆周上的两点一定是位于圆周直径的两端。

3、在点集中找出距离第2步所建圆圆心最远的D点,若D点在已知的圆内或圆周上,即该圆即为所求的圆,算法结束,否则执行第4步。

4、在A、B、C、D中选3点,使由它们生成的一个包含这4个点圆为最小,这3点成为新的ABC,返回执行第2步。若在第4步生成的圆只能通过A、B、C、D中的两点,则圆周上的两点取成新的A、B,从另外两点中任选一点作为新的C。

#include <bits/stdc++.h>
#define maxn 500+5
#define eps 1e-8
using namespace std;
struct Point{
    double x,y;
};
Point p[maxn];int n;
Point c;double r;//圆心和半径
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(){
    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(){
    while(scanf("%d",&n)!=EOF&&n){
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        mcc();
        printf("%.2lf %.2lf %.2lf\n",c.x,c.y,r);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值