HDU 3932 groundhog build home -- Simulated Annealing

Source: Internet
Author: User

Question: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3932

Note that the distance from the farthest point is 0. Therefore, set the initial value to-1.

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<ctime>#include<cmath>#include<cstdlib>#define db doubleusing 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 -- Simulated Annealing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.