zoukankan      html  css  js  c++  java
  • 【 2018南京 】Country Meow (模拟退火)

    In the 24th24^{ ext{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 — ext{---}— 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−3frac{|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;
    }
    

      














  • 相关阅读:
    eslint 屏蔽html的检查
    css 自定义悬浮窗写法
    echarts象形图图例显示问题
    win10上rocketMQ的部署过程
    死锁以及如何避免死锁
    volatile的一个例子-通俗易懂
    Java四种引用类型
    记录下:安全|API接口安全性设计(防篡改和重复调用)
    了解聚集索引,非聚集索引,联合索引,索引覆盖
    mybatis 查询树形结构
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11228015.html
Copyright © 2011-2022 走看看