BZOJ 1337 最小圆覆盖

Description
给出平面上N个点,N<=10^5.请求出一个半径最小的圆覆盖住所有的点


【题目分析】
同bzoj1336


【代码】

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-8;
const int mxn=100000;
int n;
struct point{
    double x,y;
    friend point operator +(const point a,const point b){return (point){a.x+b.x,a.y+b.y};}
    friend point operator -(const point a,const point b){return (point){a.x-b.x,a.y-b.y};}
    friend point operator /(const point a,double b){return (point){a.x/b,a.y/b};}
}p[mxn];
inline double dis(point a,point b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
point center(point a,point b,point c){
    double a1,a2,b1,b2,c1,c2;
    point ans;
    a1=2*(b.x-a.x);b1=2*(b.y-a.y);c1=(b.x*b.x)-(a.x*a.x)+(b.y*b.y)-(a.y*a.y);
    a2=2*(c.x-a.x);b2=2*(c.y-a.y);c2=(c.x*c.x)-(a.x*a.x)+(c.y*c.y)-(a.y*a.y);
    if(fabs(a1)<eps) ans.y=c1/b1,ans.x=(c2-ans.y*b2)/a2;
    else if(fabs(b1)<eps) ans.x=c1/a1,ans.y=(c2-ans.x*a2)/b2;
    else ans.x=(c2*b1-c1*b2)/(a2*b1-a1*b2),ans.y=(c2*a1-c1*a2)/(b2*a1-b1*a2);
    return ans;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
    random_shuffle(p+1,p+n+1);
    point t=p[1];
    double r=0.0;
    for(int i=2;i<=n;i++)
      if(dis(t,p[i])>r+eps){
          t=(p[i]+p[1])/2,r=dis(p[i],t);
          for(int j=2;j<i;j++)
            if(dis(t,p[j])>r+eps){
                t=(p[i]+p[j])/2,r=dis(t,p[i]);
                for(int k=1;k<j;k++)
                    if(dis(p[k],t)>r+eps)
                        t=center(p[i],p[j],p[k]),r=dis(p[i],t);
          }         
    }
    printf("%.3lf\n",r);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值