B - Run Away解题报告(来自网络)

B - Run Away
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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).
 

题目大意:


在一个给定长宽的矩形中,已知有n个地雷,现在要求出矩形内一个点,使该点到其最近的地雷的距离最远,且没有其他点到其最近地雷的距离会比该点的大。


 


解析:


    此题按照"pku_3285 Point of view in Flatland"讲的那样模拟退火,只是因为可能会出现在地雷成堆聚集,然后几个堆有相隔很远的情况,这样单一退火很容易导致WA,所以采用多组初始解并行退火,最后求这几组解的最优解。


 


源代码如下:(来自duolon)

#include <cstdio>
 #include <cmath>
 #include <ctime>
 #include <cstdlib>
 #include <algorithm>
 using namespace std;
 
 const int N = 1050;
 const int M = 30;
 const int L = 30;
const double eps = 1e-3;
 struct Point
 {
     double x, y, d;
 };
 Point p[N];
 int n;
 int X, Y;
 Point s[M];
 
 double sqr(double x)  
 {
     return x * x;
 }
 
 double dis(int idx)
 {
     if (s[idx].x < 0 || s[idx].x > X) return 0;
     if (s[idx].y < 0 || s[idx].y > Y) return 0;
     double ret = 1e10;
     for (int i = 1; i <= n; i++)
         ret = min(ret, sqr(s[idx].x - p[i].x) + sqr(s[idx].y - p[i].y));
     return ret;
 }
 
 void work()
 {
     scanf("%d%d%d", &X, &Y, &n);
     for (int i = 1; i <= n; i++)
         scanf("%lf%lf", &p[i].x, &p[i].y);
     for (int i = 1; i < M; i++)
     {
         s[i].x = rand() % (X+1);
         s[i].y = rand() % (Y+1);
         s[i].d = dis(i);
     }
     for (double delta = max(X, Y); delta > eps; delta *= 0.88)
         for (int i = 1; i < M; i++)
             for (int j = 1; j <= L; j++)
             {
                 double phi = rand();
                 s[0].x = s[i].x + delta * cos(phi);
                 s[0].y = s[i].y + delta * sin(phi);
                 if ((s[0].d = dis(0)) > s[i].d)
                     s[i] = s[0];
             }
     int idx = 1;
     for (int i = 1; i < M; i++)
         if (s[i].d > s[idx].d)
             idx = i;
     printf("The safest point is (%.1f, %.1f).\n", s[idx].x, s[idx].y);
 }
 
 int main()
 {
     int T;
     scanf("%d", &T);
     while (T--)
         work();
 }


注意:
1、srand(time(NULL)); 不能用这个来初始化随机种子,pku上禁止time(NULL),会返回runtime error。若要用srand,则srand(100)这样随便写个int进去就可以。
2、vs c++里的rand()返回 [0,2^16)的整数;gcc里rand()返回 [0,2^32)的整数。
      要获得[0,1)的实数,必须写 rand() / RAND_MAX,其中RAND_MAX是定义在cstdlib头文件里的一个返回最大随机值的常量。
3、标准规定,浮点输出全都要用%f,如果是%lf 则会WA~~呃,需要提醒的是,double输入是%lf,输出是%f ;float则都是%f 。 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值