【 2018南京 】Country Meow (模拟退火)

In the 24th24^{\text{th}}24th century, there is a country somewhere in the universe, namely Country Meow. Due to advanced technology, people can easily travel in the 3-dimensional space.

There are NNN cities in Country Meow. The iii-th city is located at (xi,yi,zi)(x_i,y_i,z_i)(xi,yi,zi) in Cartesian coordinate.

Due to the increasing threat from Country Woof, the president decided to build a new combatant command, so that troops in different cities can easily communicate. Hence, the Euclidean distance between the combatant command and any city should be minimized.

Your task is to calculate the minimum Euclidean distance between the combatant command and the farthest city.

Input

The first line contains an integer NNN (1≤N≤1001 \le N \le 1001N100).

The following NNN lines describe the iii-th city located.Each line contains three integers xi,yi,zix_i, y_i, z_ixi,yi,zi (−100000≤xi,yi,zi≤100000-100000 \le x_i, y_i, z_i \le 100000100000xi,yi,zi100000).

Output

Print a real number —\text{---}— the minimum Euclidean distance between the combatant command and the farthest city. Your answer is considered correct if its absolute or relative error does not exceed 10−310^{-3}103. Formally, let your answer be aaa, and the jury's answer be bbb. Your answer is considered correct if ∣a−b∣max⁡(1,∣b∣)≤10−3\frac{|a - b|}{\max(1, |b|)} \le 10^{-3}max(1,b)ab103.

本题答案不唯一,符合要求的答案均正确

样例输入1 复制
3
0 0 0
3 0 0
0 4 0
样例输出1 复制
2.500000590252103
样例输入2 复制
4
0 0 0
1 0 0
0 1 0
0 0 1
样例输出2 复制
0.816496631812619


SOLUTION:

初始化0,0,然后每次向最远的点移动一定的距离
感觉像是退火,但又不是,

CODE:

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=150;
const double eps=1e-3;       //精度
const double start_T=1000;   //初始温度
const double rate=0.98;      //温度下降速率
struct point
{
    double x;
    double y;
    double z;
} p[maxn];
int N;
double dist(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
double solve()
{
    double T=start_T;
    point ans_p={0,0,0};  //初始点
    double ans=1e99;      //预设一个较大值
    while(T>eps)
    {
        point maxd_p=p[1];
        for(int i=2;i<=N;i++)
        {
            if(dist(ans_p,p[i])>dist(ans_p,maxd_p))
                maxd_p=p[i];
        }
        //找到距离ans_p最远的点,maxd_p
        ans=min(ans,dist(ans_p,maxd_p));
        ans_p.x+=(maxd_p.x-ans_p.x)*(T/start_T);    //以一定概率靠近maxd_p
        ans_p.y+=(maxd_p.y-ans_p.y)*(T/start_T);
        ans_p.z+=(maxd_p.z-ans_p.z)*(T/start_T);
        T*=rate;
    }
    return ans;
}
int main()
{
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
        scanf("%lf %lf %lf",&p[i].x,&p[i].y,&p[i].z);
    printf("%.8lf",solve());
    return 0;
}

  














转载于:https://www.cnblogs.com/zhangbuang/p/11228015.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值