1. 程式人生 > >POJ 1379 模擬退火法 詳解

POJ 1379 模擬退火法 詳解

Run Away

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions:10619 Accepted: 3174

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

題意:在給定矩陣範圍內找一個點,該點距離矩陣內所有點的最小值最大,

在演算法書看到這道題,演算法為  模擬退火法 在書上看過,又看別人的寫法,先找20個隨機種子,然後逐步更新,最後取最優解。

具體看程式碼:

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)//3.14
typedef long long ll;
struct node
{
  double x,y,dis;
} a[2000],b[100],ans;
int n,X,Y;
double dis(node c)
{
  double sum=1e8;
  for (int i=1; i<=n; ++i)
  {
    double dd=sqrt((c.x-a[i].x)*(c.x-a[i].x)+(c.y-a[i].y)*(c.y-a[i].y));
    if(sum>dd)sum=dd;//找到最小距離
  }
  return sum;
}
void Solve()
{
  for (int i=1; i<=20; ++i)//預20個並行解狀態處理
  {
    b[i].x=rand()%X+1,b[i].y=rand()%Y+1;//生成隨機位置
    b[i].dis=dis(b[i]);//找到位置最小距離
  }
  node t;
  for (double i=max(X,Y); i>=1e-2; i*=0.9)//初始溫度 再溫度未退至足夠低 退火
    for (int j=1; j<=20; j++)//列舉每個狀態
      for (int k=1; k<=20; k++)//隨機調節20個方向 依次迭代
      {
        double ran=rand();
        t.x=b[j].x+cos(ran)*i;//取出第j個解狀態
        t.y=b[j].y+sin(ran)*i;//+隨機方向
        if (0>t.x || t.x>X || 0>t.y || t.y>Y)
          continue;//若t在界外
        t.dis=dis(t);
        if (t.dis>b[j].dis) b[j]=t;//更新最大距離狀態
      }
  ans.dis=-1;
  for (int i=1; i<=20; ++i)if (ans.dis<b[i].dis) ans=b[i];//找出最大距離
}

int main()
{
  int test;
  scanf("%d",&test);
  while (test--)
  {
    scanf("%d %d %d",&X,&Y,&n);//矩陣XY
    for (int i=1; i<=n; i++)
      scanf("%lf %lf",&a[i].x,&a[i].y);//矩陣內的點
    Solve();
    printf("The safest point is (%.1f, %.1f).\n",ans.x,ans.y);
  }
  return 0;
}