码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3932 Groundhog Build Home —— 模拟退火

时间:2018-10-31 23:26:37      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:get   ref   style   type   http   移动   ctime   ios   php   

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3932

找一个位置使距离最远的点的距离最小;

上模拟退火;

每次向距离最远的点移动,注意判断一下距离最远的点距离为0的情况。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#define eps 1e-15
#define dc 0.99
using namespace std;
typedef double db;
int const xn=1005;
int n,xx[xn],yy[xn],rx,ry;
db ansx,ansy,ans;
db dist(db x,db y,db a,db b){return sqrt((x-a)*(x-a)+(y-b)*(y-b));}
void SA()
{
  db T=1000,x=ansx/n,y=ansy/n,tx,ty; int id;
  ans=1e9;
  while(T>eps)
    {
      db ret=-1,k;
      for(int i=1;i<=n;i++)
      if(ret<(k=dist(x,y,xx[i],yy[i])))id=i,ret=k;
      if(ret!=-1&&ret<ans)ans=ret,ansx=x,ansy=y;
      x+=(xx[id]-x)/ret*T;
      y+=(yy[id]-y)/ret*T;
      T*=dc;
    }
}
int main()
{
  srand(time(0));
  while(~scanf("%d%d%d",&rx,&ry,&n))
    {
      ansx=0; ansy=0;
      for(int i=1;i<=n;i++)
    scanf("%d%d",&xx[i],&yy[i]),ansx+=xx[i],ansy+=yy[i];
      SA();
      printf("(%.1lf,%.1lf).\n%.1lf\n",ansx,ansy,ans);
    }
  return 0;
}

 

hdu 3932 Groundhog Build Home —— 模拟退火

标签:get   ref   style   type   http   移动   ctime   ios   php   

原文地址:https://www.cnblogs.com/Zinn/p/9886344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!