Minimal Circle zoj无法用模拟退火(未掌握)

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>

using namespace std;
const int maxn=111;
const double eps=1e-8;

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

struct Point
{
    double x,y;
    double r;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};

Point p[maxn];
Point c;

double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}

Point waixin(Point a,Point b,Point c)
{
    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;
    return Point(a.x + (c1*b2 - c2*b1)/d, a.y + (a1*c2 -a2*c1)/d);
}

void min_cover_circle(int n)
{
    random_shuffle(p,p+n);
    c=p[0];
    c.r=0;
    for(int i=1;i<n;i++)
    {
        if(sgn(dist(p[i],c)-c.r)>0)
        {
            c=p[i];
            c.r=dist(p[i],c);
            for(int j=0;j<i;j++)
            {
                if(sgn(dist(p[j],c)-c.r)>0)
                {
                    c.x=(p[i].x+p[j].x)/2;
                    c.y=(p[i].y+p[j].y)/2;
                    c.r=dist(p[i],c);
                    for(int k=0;k<j;k++)
                    {
                        if(sgn(dist(p[k],c)-c.r)>0)
                        {
                            c=waixin(p[i],p[j],p[k]);
                            c.r=dist(p[i],c);
                        }
                    }
                }
            }
        }
    }
}
int main()
{
    int n;
    while(1)
    {
        scanf("%d",&n);
            if(n==0)
                break;
        for(int i=0;i<n;i++)
        {
            scanf("%lf %lf",&p[i].x,&p[i].y);
        }
        min_cover_circle(n);
        printf("%.2lf %.2lf %.2lf\n",c.x,c.y,c.r);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值