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;
    }
    

      














  • 相关阅读:
    jsfl 常用自定义方法
    jsfl 常用方法
    Java 跨平台原理
    JDK、JRE 和 JVM 的区别
    win10 中安装 JDK8 以及环境配置
    JDK 14 都已经发布了,Java 8 依然是我的最爱
    DOS 命令大全用法详解
    《Java程序设计基础》 第4章手记
    堆和栈的区别
    CC#JavaPython 基本数据类型比较
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11228015.html
Copyright © 2011-2022 走看看