POJ 1379 模拟退火算法

求规定平面上一点到已知点的最小距离最大的点。

模拟退火的流程是,随机构造几组解作为初始解空间,每次对当前解空间进行随机扩展,若发现更优解则替换。

进行的次数由参数人为控制,而随机扩展的幅度也是随着次数逐渐减小的。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<ctime>
using namespace std;
#define eps 1e-2
double pi=acos(-1.0);
double x[10004],y[10004];
double qx[10004],qy[10004];
double X,Y;
int n;
const int num=15;   //初始解空间大小
const int numf=30;  //扩展解空间大小
double dist(double x,double y,double xx,double yy)
{
    return sqrt((x-xx)*(x-xx)+(y-yy)*(y-yy));
}
double get(double xx,double yy)
{
    double mins=1111111111;
    for(int i=1;i<=n;i++)
        mins=min(mins,dist(xx,yy,x[i],y[i]));
    return mins;
}
double getr()   //返回[0,1]的浮点数
{
    return (rand()%10000+1.0)/10000.0;
}
int main()
{
    srand(time(NULL));  //多次提交= =防RP差
    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%lf%lf%d",&X,&Y,&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&x[i],&y[i]);
        }
        for(int i=1;i<=num;i++) //构造初始解空间
        {
            qx[i]=X*getr();
            qy[i]=Y*getr();
        }
        double dd=max(Y,X);     //最大温度
        while(dd>eps)
        {
            for(int i=1;i<=num;i++)
            {
                double now=get(qx[i],qy[i]),t;
                for(int j=1;j<=numf;j++)
                {
                    double dt=getr()*2*pi;      //随机角度
                    double tmpx=qx[i]+dd*cos(dt);
                    double tmpy=qy[i]+dd*sin(dt);
                    if(tmpx<0||tmpx>X||tmpy<0||tmpy>Y) continue;
                    if((t=get(tmpx,tmpy))>now) //扩展新解
                    {
                        qx[i]=tmpx;
                        qy[i]=tmpy;
                        now=t;
                    }
                }
            }
            dd*=0.9;            //冷却速度
        }
        double mins=0;
        int k;
        for(int i=1;i<=num;i++)
        {
            double tmp=get(qx[i],qy[i]);
            if(tmp>mins) {mins=tmp;k=i;}
        }
        printf("The safest point is (%.1lf, %.1lf).\n",qx[k],qy[k]);
    }
    return 0;
}
/*
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
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TommyTT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值