2018icpc D.Country Meow 最小球覆盖

题意:题目给n个三维点坐标(x,y,z),现在希望存在一个点,使得这个点到所有n个点的最大距离最小,然后求该最大距离。即用一个半径最小的球把所有点都放进去,求该球半径

思路:看了半天最小圆,然后发现这个直接可以三分球心坐标

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 150;
const ll mod = (ll)1e9 +7;

typedef struct node
{
    double p[3];
}node;

int n;
node po[maxn];

double dis(node x, node y)
{
    return sqrt((x.p[0]-y.p[0])*(x.p[0]-y.p[0])+
    (x.p[1]-y.p[1])*(x.p[1]-y.p[1])+(x.p[2]-y.p[2])*(x.p[2]-y.p[2]));
}

double cal(node a)
{
    double res = 0;
    for(int i=0;i<n;i++)
    {
        res = max(res,dis(a,po[i]));
    }
    return res;
}

node sol(int dir, node now)
{
    if(dir>=3)return now;
    double l = -100000.0, r = 100000.0, kl, kr;
    node ra, rb, ans, tl, tr;
    ans = tl = tr = now;
    while(r-l>1e-9)
    {
        kl = (2*l+r)/3;
        kr = (l+2*r)/3;
        tl.p[dir] = kl, tr.p[dir] = kr;
        ra = sol(dir+1,tl);
        rb = sol(dir+1,tr);
        if(cal(ra)>cal(rb))
        {
            l = kl;
            ans = rb;
        }
        else
        {
            r = kr;
            ans = ra;
        }
    }
    return ans;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lf%lf%lf",&po[i].p[0],&po[i].p[1],&po[i].p[2]);
    }
    node ans;
    double k = cal(sol(0,ans));
    printf("%.9lf\n",k);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值