ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-07-22 20:03:51  阅读:325  来源: 互联网

标签:yi Meow Country ans int 模拟退火 100000 double maxd


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 1001≤N≤100).

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 100000−100000≤xi​,yi​,zi​≤100000).

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}10−3. 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∣)∣a−b∣​≤10−3.

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

样例输入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;
}

  














标签:yi,Meow,Country,ans,int,模拟退火,100000,double,maxd
来源: https://www.cnblogs.com/zhangbuang/p/11228015.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有