Problem Description
One of the traps we will encounter in the Pyramid is located in the Large Room. A lot of small holes are drilled into the floor. They look completely harmless at the first sight. But when activated, they start to throw out very hot java, uh … pardon, lava. Unfortunately, all known paths to the Center Room (where the Sarcophagus is) contain a trigger that activates the trap. The ACM were not able to avoid that. But they have carefully monitored the positions of all the holes. So it is important to find the place in the Large Room that has the maximal distance from all the holes. This place is the safest in the entire room and the archaeologist has to hide there.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing three integers X, Y, M separated by space. The numbers satisfy conditions: 1 <= X,Y <=10000, 1 <= M <= 1000. The numbers X and Yindicate the dimensions of the Large Room which has a rectangular shape. The number M stands for the number of holes. Then exactly M lines follow, each containing two integer numbers Ui and Vi (0 <= Ui <= X, 0 <= Vi <= Y) indicating the coordinates of one hole. There may be several holes at the same position.
Output
Print exactly one line for each test case. The line should contain the sentence “The safest point is (P, Q).” where P and Qare the coordinates of the point in the room that has the maximum distance from the nearest hole, rounded to the nearest number with exactly one digit after the decimal point (0.05 rounds up to 0.1).
Sample Input
3
1000 50 1
10 10
100 100 4
10 10
10 90
90 10
90 90
3000 3000 4
1200 85
63 2500
2700 2650
2990 100
Sample Output
The safest point is (1000.0, 50.0).
The safest point is (50.0, 50.0).
The safest point is (1433.0, 1669.8).
Source
Central Europe 1999
模拟退火,听起来很高大上的样子,但是实现起来非常简单…
对于这道题,我们可以随机选择30个源点,以step为步长,让它们沿任意角度跳跃,直到不管如何跳跃,答案都比当前最优值差。
但是这道题并不需要“正统”模拟退火一般设立几率接受比当前差的结果。
- 代码
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
const int Inf = 1 << 31 -1, maxn = 1005;
const double exps = 1e-3; //最小步长,比精度低2位即可
const double pi = acos(-1.0);
int X, Y, M;
double x[maxn], y[maxn];
double a[maxn], b[maxn], best[maxn];
inline double Rand(double l, double r) //生成区间[l, r]间的随机数
{
return (rand() % 10000) / 10000.0 * (r - l) + l;
}
inline double Pow(double x)
{
return x * x;
}
inline double dist(double x1, double y1, double x2, double y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main()
{
int T, i, j, k;
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &X, &Y, &M);
for(i = 1; i <= M; i++)
scanf("%lf%lf", &x[i], &y[i]);
srand(time(0));
for(i = 1; i <= 30; i++) {//随机设立30个源点
a[i] = Rand(1, X); b[i] = Rand(1, Y);
best[i] = Inf;
for(j = 1; j <= M; j++)
best[i] = min(best[i], dist(a[i], b[i], x[j], y[j]));
}
double step = max(X, Y);
while(step > exps) {
for(i = 1; i <= 30; i++)
for(j = 1; j <= 30; j++) {
double angel = Rand(0, 2 * pi); //随机生成角度,使源点向四处扩散
double x1 = a[i] + cos(angel) * step, y1 = b[i] + sin(angel) * step;
if(x1 < 0 || x1 > X || y1 < 0 || y1 > Y) continue;
double dis = Inf;
for(k = 1; k <= M; k++)
dis = min(dis, dist(x1, y1, x[k], y[k]));
if(dis > best[i]) {
best[i] = dis;
a[i] = x1; b[i] = y1;
}
}
step *= 0.85;
}
int t = 1;
for(i = 2; i <= 30 ;i++)
if(best[i] >= best[t]) t = i;
printf("The safest point is (%.1lf, %.1lf).\n", a[t], b[t]);
}
return 0;
}