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

hdu 3932 Groundhog Build Home——模拟退火

时间:2018-10-31 23:21:56      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:print   +=   math   http   style   pac   return   ace   include   

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

注意与最远的点距离为0的情况。所以初值设成-1。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<cstdlib>
#define db double
using namespace std;
const int N=1005;
const db dc=0.99,eps=1e-17,INF=2e4;
int n,nx[N],ny[N],pid;
db ans,px,py;
int rdn()
{
  int ret=0;bool fx=1;char ch=getchar();
  while(ch>9||ch<0){if(ch==-)fx=0;ch=getchar();}
  while(ch>=0&&ch<=9) ret=(ret<<3)+(ret<<1)+ch-0,ch=getchar();
  return fx?ret:-ret;
}
db dis(db x0,db y0,db x1,db y1)
{
  return sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));
}
void SA(db T)
{
  db x0=px,y0=py,pr=ans;int id=pid;
  while(T>eps)
    {
      pr=-1;
      for(int i=1;i<=n;i++)
    {
      db tmp=dis(x0,y0,nx[i],ny[i]);
      if(tmp>pr)pr=tmp,id=i;
    }
      if(pr!=-1&&pr<ans)ans=pr,pid=id,px=x0,py=y0;
      x0=x0+(nx[id]-x0)/pr*T; y0=y0+(ny[id]-y0)/pr*T;
      T*=dc;
    }
}
int main()
{
  srand(time(0));
  int tx,ty;
  while(scanf("%d%d%d",&tx,&ty,&n)==3)
    {
      for(int i=1;i<=n;i++)nx[i]=rdn(),ny[i]=rdn(),px+=nx[i],py+=ny[i];
      px/=n; py/=n; ans=INF;
      SA(10000);SA(10000);
      printf("(%.1lf,%.1lf).\n%.1lf\n",px,py,ans);
    }
  return 0;
}

 

hdu 3932 Groundhog Build Home——模拟退火

标签:print   +=   math   http   style   pac   return   ace   include   

原文地址:https://www.cnblogs.com/Narh/p/9886381.html

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