poj-1379 Run Away(模拟退火算法)

题目链接:

Run Away

 

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 7982 Accepted: 2391

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).
题意:求矩形里的一个点,使这个点到所有已知点的最小距离尽量大;
思路:看网上说是一个模拟退火算法就简单的学了一下,随机取一些点然后在这些点一点范围内再随机的取点,取得点更符合就更新,指导温度冷却到一定程度就可以得到答案了;
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <algorithm>
using namespace std;
const int num=30;//每次的子集有多少元素,就是你要选多少个点进行迭代
const int cnt=30;//每次一个点要随机的生成多少个点
const double inf=1e14+1;
const double PI=acos(-1.0);
int fx,fy,n;
double x[1002],y[1002],px,py;
struct node
{
  double X,Y,dis;
};
node ans[100];
double get_dis(double a,double b,double c,double d)
{
    return sqrt((c-a)*(c-a)+(d-b)*(d-b));
}
void Iint()
{
    for(int i=0;i<num;i++)
    {
        ans[i].X=1.0*(rand()%32767+1)/32767*fx;
        ans[i].Y=1.0*(rand()%32767+1)/32767*fy;
        ans[i].dis=inf;
        for(int j=0;j<n;j++)
        {
            ans[i].dis=min(ans[i].dis,get_dis(ans[i].X,ans[i].Y,x[j],y[j]));
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    srand((unsigned)time(NULL));
    while(t--)
    {
        scanf("%d%d%d",&fx,&fy,&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&x[i],&y[i]);
        }
        Iint();
        double temp=max(fx*1.0,fy*1.0)/(sqrt(1.0*n));
        while(temp>=0.001)
        {
            for(int i=0;i<num;i++)
            {
                px=ans[i].X;
                py=ans[i].Y;
                for(int j=0;j<cnt;j++)
                {
                    double s=PI*2*(rand()%32767)/32767,dx,dy;
                    dx=px+cos(s)*temp;
                    dy=py+sin(s)*temp;
                    if(dx<0||dx>fx*1.0||dy<0||dy>fy*1.0)continue;
                    double cur=inf;
                    for(int k=0;k<n;k++)
                    {
                        cur=min(cur,get_dis(dx,dy,x[k],y[k]));
                    }
                    if(cur>ans[i].dis)
                    {
                        ans[i].X=dx;
                        ans[i].Y=dy;
                        ans[i].dis=cur;
                    }
                }

            }
            temp*=0.83;
        }
        double ans_dis=0,ans_x,ans_y;
        for(int i=0;i<num;i++)
        {
            if(ans[i].dis>ans_dis)
            {
                ans_x=ans[i].X;
                ans_y=ans[i].Y;
                ans_dis=ans[i].dis;
            }
        }
        printf("The safest point is (%.1lf, %.1lf).\n",ans_x,ans_y);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5288606.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值