hdu3007 Buried memory(最小圆覆盖)

题目链接

分析:
经典算法之最小圆覆盖

tip

就是练练手,感觉增量法挺神奇

#include<bits/stdc++.h>
#define LD long double

using namespace std;

const double eps=1e-8;
const double Pi=acos(-1.0);
struct node{
    double x,y;
    node (double xx=0,double yy=0) {
        x=xx;y=yy;
    }
};
node p[505];

node operator +(const node &a,const node &b) {return node(a.x+b.x,a.y+b.y);}
node operator -(const node &a,const node &b) {return node(a.x-b.x,a.y-b.y);}
node operator *(const node &a,const double &b) {return node(a.x*b,a.y*b);}
node operator /(const node &a,const double &b) {return node(a.x/b,a.y/b);}

double Dot(node a,node b) {return a.x*b.x+a.y*b.y;}
double Cross(node a,node b) {return a.x*b.y-a.y*b.x;}
double lenth(node a) {return sqrt(Dot(a,a));}

int n;
double r;
node c;

int dcmp(double x) {
    if (fabs(x)<eps) return 0;
    else if (x>0) return 1;
    else return -1;
}

node rotate(node a,double t) {
    return node(a.x*cos(t)-a.y*sin(t),a.x*sin(t)+a.y*cos(t));
} 

node jiao(node P,node v,node Q,node w) {
    node u=P-Q;
    double t=Cross(w,u)/Cross(v,w);
    return P+v*t;
}

node get_c(node a,node b,node c)
{
    node P=(a+b)/2.0;
    node Q=(a+c)/2.0;
    node v=rotate(b-a,Pi/2.0),w=rotate(c-a,Pi/2.0);
    if (dcmp(Cross(v,w))==0) {
        if (dcmp(lenth(a-b)+lenth(b-c)-lenth(a-c))==0)
            return (a+c)/2.0;
        if (dcmp(lenth(a-c)+lenth(c-b)-lenth(a-b))==0)
            return (a+b)/2.0;
        if (dcmp(lenth(b-a)+lenth(a-c)-lenth(b-c))==0)
            return (b+c)/2.0;
    }
    return jiao(P,v,Q,w);
}

void mcc()
{
    random_shuffle(p+1,p+1+n);
    c=p[1],r=0;
    for (int i=2;i<=n;i++)
        if (dcmp(lenth(p[i]-c)-r)>0) {
            c=p[i],r=0;
            for (int j=1;j<i;j++) 
                if (dcmp(lenth(p[j]-c)-r)>0) {
                    c=(p[i]+p[j])/2.0;
                    r=lenth(p[i]-c);
                    for (int k=1;k<j;k++)
                        if (dcmp(lenth(p[k]-c)-r)>0) {
                            c=get_c(p[i],p[j],p[k]);
                            r=lenth(p[i]-c);
                        }
                }
        }
}

int main()
{
    while (scanf("%d",&n)!=EOF&&n) {
        for (int i=1;i<=n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        mcc();
        printf("%0.2lf %0.2lf %0.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、付费专栏及课程。

余额充值