题解:CF_106E_Space Rescuers : 模拟退火/二分

题解:CF_106E_Space Rescuers : 模拟退火/二分

#1题意:

The Galaxy contains n planets, there are many different living creatures inhabiting each planet. And each creature can get into troubles! Space rescuers know it perfectly well and they are always ready to help anyone who really needs help. All you need to do is call for them.

Now the space rescuers plan to build the largest in the history of the Galaxy rescue station; however, the rescue station's location is yet to be determined. As some cases are real emergencies, the rescuers want to find such a point in the Galaxy from which it would be possible to get to the remotest planet in the minimum possible time. In other words, the rescuers need such point in the space that the distance between it and the planet remotest from it was minimal (if we compare this point with all other possible points in the space). Unfortunately, the rescuers can't sole this problem.

As the planets are quite remote from each other, they can be considered as points in Euclidean three-dimensional space. The distance between points (xi, yi, zi) and (xj, yj, zj) can be calculated by the formula

img

. The rescue station can be positioned in any point in the space. It can also coincide with some planet.

Galaxy is in danger! Save the space rescuers and find the required point for them.

Input

The first line of the input file contains integer n — the number of planets (1 ≤ N ≤ 100). Each of the following n lines contains information about the planets. The i-th line contains three integers xi, yi, zi — the coordinates of the i-th planet ( -10^4 ≤ xi, yi, zi ≤ 10^4, 1 ≤ i ≤ n). No two planets coincide.

Output

Print on the first line of the output file three space-separated real numbers x0,y0,z0 — the coordinates for the future base. If there are several solutions, you are allowed to print any of them. The answer will be accepted if the distance from this point to the remotest planet will differ from the juries' variant in no more than 10 ^-6 in absolute or relative value.

思路:

本文提供两种做法,但只有一种做法写了程序并AC

方法一:模拟退火

 此方法为非常简单的模拟退火,很适合入门学习。

 首先,设g(x,y,z)为距离点 P(x,y,z)距离最远的点的距离,很容易证明g(x,y,z)在定义域内连续:考虑边界情况,当距离P最远的点发生改变时,一定会有两个点以上的点距离P的距离相等,一些是即将成为离P最远的点,另一些是之前距离P最远的点。那么在任何情况下,g(x,y,z)不会发生突变。

 模拟退火的过程如下:我们不妨设置初始温度为T,T随着时间推移以f(t,time)减小,即:T_i=f(time) & f(0)=T0,注意,f(time)一般情况下为单调不增函数。

 我们以T标定我们对于不一定是最优解的方案的选择概率。标定方法多种多样,对于本题,我们首先随意取一个点作为当前的答案点,对于当前,我们遍历所有的n个点,找出离当前点最远的一个点Q,将当前点向Q移动T_i * dis(P,Q)的距离。当T逐步减小的时候,答案逐步向正确答案靠近。

 一种更容易的理解是:考虑一个粒子,在一个三维曲面上做热运动,由于一开始环境温度非常高,粒子每次沿着曲面梯度下降最快的方向运动,但由于粒子具有非常大的能量,所以它十分容易冲过头导致冲出当前的凹陷。这就是以一定的概率接受可能不是最优的解。当体系温度逐步降低的时候,粒子保持在能量较低处的概率是比较大的。所以一般模拟退火都会进行多次,以防止退出来一个较优解而不是最优解。但由于本题过于特殊(简单),所以我把初始位置设在重心,只退一次就能得到答案。同理,当初始位置选的足够好,三分法也可以解决本题(即直接对于初始位置梯度下降)。

代码:

#include <iostream>
 #include <cstdio>
 #include <vector>
 using namespace std;
 const int MAXN = 1e2+7;
 const double EPS = 1e-8;
 template <typename _TP>
 inline _TP read(_TP &X){
     char ch=0;int w;X=0;
     while(!isdigit(ch)){w=ch=='-';ch=getchar();}
     while(isdigit(ch)){X=(X<<1)+(X<<3)+(ch^48);ch=getchar();}
     X=w?-X:X;
     return X;
 }
 struct Point{
     int x,y,z;
 }v[MAXN];
 int main(){
     double x=0,y=0,z=0,T=1;
     int n,minPos;
     read(n);
     for(int i=1;i<=n;i++){
         Point tmp;
         read(tmp.x);read(tmp.y);read(tmp.z);
         v[i]=tmp;
         x+=tmp.x;y+=tmp.y;z+=tmp.z;
     }
     x/=n;y/=n;z/=n; //初始位置在重心 较优
     while(T > EPS){
         double maxx = -1;
         minPos = -1;
         for(int i=1;i<=n;i++){
             double t = (x-v[i].x)*(x-v[i].x)+(y-v[i].y)*(y-v[i].y)+(z-v[i].z)*(z-v[i].z);
             if(t > maxx){
                 maxx = t;
                 minPos = i;
             }
         }
         T *= 0.999;
         x+=T*(v[minPos].x-x);
         y+=T*(v[minPos].y-y);
         z+=T*(v[minPos].z-z);
     }
     printf("%.10f %.10f %.10f",x,y,z);
     return 0;
 }

方法2:

 本方法是我在场上写的方法,即:二分答案点到离它最远的点的距离,注意在judge的时候进行判断,如果在此距离下发现了两个以上的点,那这种方案也不合法。由此得到了答案距离。

 接下来就是寻找答案点的位置。答案点一定由至少三个点确定,所以我们可以枚举三个点,计算答案点的位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值