抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

题解:Space Rescuers>>模拟退火/二分

1.题意:

地址:https://vjudge.net/problem/CodeForces-106E
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 ($x_i, y_i, z_i$) and ($x_j, y_j, z_j$) 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 $x_i, y_i, z_i$ — the coordinates of the i-th planet ($ -10^4 ≤ x_i, y_i, z_i ≤ 10^4, 1 ≤ i ≤ n$). No two planets coincide.

Output

Print on the first line of the output file three space-separated real numbers $x_0,y_0,z_0$ — 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.

2.思路:

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

2.1 方法一>>模拟退火

这个题是非常简单的模拟退火,很适合入门学习。

首先,设$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)=T_0$,注意,$f(time)$一般情况下为单调不增函数。

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

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

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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.2方法2(正确性存疑)

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

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

评论