poj 1379 Run away 模拟退火

         第一道模拟退火题是看了别人的代码才会的,现在对这种题目只能是乱搞了。

        题意:求距离所有点中最近的点的距离最大。

       代码:

  

#include<cstdio>
#include<iostream>
#include<math.h>
#include<cstdlib>
#define sqr(x) ((x)*(x))
#define eps 1e-3
#define Maxn 1000+10
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define F(i,n) for(int i=0;i<n;i++)

using namespace std;

struct cpoint
{
    double x,y,d;
    cpoint (){}
    cpoint (double x,double y) :x(x),y(y) {}
};
cpoint cp[Maxn],rp;
double x,y;
int n;
double dissqr(cpoint p,cpoint q)
{
    return sqr(p.x-q.x)+sqr(p.y-q.y);
}
void solve()
{
    int L=100;
    rp.x=double(rand()%1000/1000.000*x);
    rp.y=double(rand()%1000/1000.000*y);
    rp.d=INF;
    F(i,n)  rp.d=Min(rp.d,dissqr(rp,cp[i]));
    double dal=Max(x,y)/sqrt(n*1.0);
    while(dal>eps)
   {
            cpoint t;
            F(j,L)
            {
                double angle=2*pi*(rand()%10000+1)/10000.000;
                t.x=rp.x+dal*cos(angle);
                t.y=rp.y+dal*sin(angle);
                t.d=INF;
                if(t.x>x||t.x<0||t.y>y||t.y<0)
                   continue;
                F(k,n)
                {
                    t.d=Min(t.d,dissqr(t,cp[k]));
                }
                if(t.d>rp.d)
                  rp=t;
            }
       dal*=0.9;
    }
    printf("The safest point is (%.1lf, %.1lf).\n",rp.x,rp.y);
}
int main()
{
    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%lf %lf %d",&x,&y,&n);
        F(i,n)
           scanf("%lf %lf",&cp[i].x,&cp[i].y);
        solve();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值