POJ 1379 模拟退火求矩形内的覆盖点

Run Away

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 10930 Accepted: 3242

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 个洞的坐标,要求找到一点使得这一点距离最近洞的距离最远。

本以为是一个二分,但是牵扯到精度问题,那么这一道题目就不太适合二分了。

求规定平面上一点到已知点的最小距离最大的点。 模拟退火的流程是,随机构造几组解作为初始解空间,每次对当前解空间进行随机扩展,若发现更优解则替换。 进行的次数由参数人为控制,而随机扩展的幅度也是随着次数逐渐减小的。

这个是别人写的模拟退火模型,挺准确的:

      模拟退火算法的模型


① 初始化:初始温度T(充分大),初始解状态S(算法迭代的起点), 每次迭代次数L

② for k=1 to L 做③至⑥

③ 产生新解S’

④ 计算增量Δt′=C(S′)-C(S),其中C(S)为评价函数

⑤ 若Δt′<0则接受S’作为新的当前解,否则以概率e-Δt/k接受S’作为新的当前解

⑥ 如果满足终止条件则输出当前解作为最优解,结束程序

⑦ T逐渐减少,然后转②

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
double INF=1000000000;
const double PI = acos(-1.0);
int n;
struct node{
    double x,y;
}num[2000],moni[30];
double ux,uy;
double dis(node a,node b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double solve(node p){
//    double ans=dis(p,num[0]);
//    int minid=0;
//    rep(i,0,n-1)
//    {
//        if(dis(p,num[minid])>dis(p,num[i]))
//            minid=i;
//    }
//    ans=dis(p,num[minid]);
    double ans=INF;
    for(int i=0;i<n;i++)
        ans=min(ans,dis(p,num[i]));
    return ans;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
   int t;
   scanf("%d",&t);
   while(t--){
    scanf("%lf%lf%d",&ux,&uy,&n);
    double t=10000;//初始化温度T
    rep(i,0,n-1) scanf("%lf%lf",&num[i].x,&num[i].y);
    rep(i,0,19){//随机化20组答案
    moni[i].x=ux*((rand() % 1000+1.0)/ 1000.0);//保证区间是[0,1]
    moni[i].y=uy*((rand() % 1000+1.0) / 1000.0);
    }
    while(t>1e-3){
            node q;
        for(int j=0;j<20;j++){
          double ans=solve(moni[j]);
        for(int i=0;i<30;i++){
       double k= (rand() % 1000+1.0)/ 1000.0 * 2 * PI;
        q.x=moni[j].x+sin(k)*t;
        q.y=moni[j].y+cos(k)*t;
        if(q.x<0||q.x>ux||q.y<0||q.y>uy) continue;
        double res=solve(q);
        if(res>ans){//更新最优解
            ans=res;
            moni[j]=q;
           }
            else if(exp(res/t)<(double)rand()/(double)RAND_MAX){//返回[0,1]内的数,当然也可用(rand() % 1000+1.0)/ 1000.0
            ans=res,moni[j]=q;
            } 
          }
        }
        t*=0.9;
    }
    double ans=0;
    int k;
    for(int i=0;i<20;i++){
            double res=solve(moni[i]);
          if(res>ans){
            ans=res;
            k=i;
        }
    }
    printf("The safest point is (%.1f, %.1f).\n",moni[k].x,moni[k].y);
   }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值