HDU-3932 Groundhog Build Home simulated annealing algorithm

Source: Internet
Author: User
Tags integer numbers

  

Groundhogs is good at digging holes, their home is a hole, usually a group of groundhogs would find a more suitable area F or their activities and build their home at the this area. Xiaomi have grown up, can no longer live with its parents.so it need s to build their own home.xiaomi like to visit other family so much, at each visit it always start from the point of his own Home. Xiaomi would visit all of the Groundhogs ' home on this area (it would chose the linear distance between). To save Energy,xiaomi would what do you-to-do it find where its home built,so that the longest distance between Xiaomi ' s Ho Me and the other groundhog's home is minimum.

Inputthe input consists of many test cases,ending of EOF. Each test case is begins with a line containing three integers X, Y, N separated by space. The numbers satisfy conditions:1 <= x, y <=10000, 1 <= n<= 1000. Groundhogs acivity at a rectangular area, and X, Y is the of the side of this rectangle, the number N stands for the number O F holes. Then exactly N lines follow, each containing the integer numbers XI and yi (0 <= Xi <= X, 0 <= yi <= Y) indica Ting the coordinates of one home. Outputprint exactly the lines for each test case. The first line was the coordinate of Xiaomi ' s home which we help to find. The second line is he longest distance between Xiaomi's home and the other groundhog ' s home. The output round to the nearest number with exactly one digit after the decimal point (0.05 rounds up to 0.1). Sample Input

1000 50 110 101000 50 40 01 00 11 1

Sample Output

(10.0,10.0). 0.0 (0.5,0.5). 0.7

Test instructions
The smallest cover circle, there are n points, give an axis, give an X, Y range, in this range to find a point, so that the maximum distance to all points minimum, that is, the smallest circle covering n points, the output of the Circle's center coordinates.
Solution:
1. Minimum coverage circle algorithm--No
2. Simulated annealing algorithm
Randomly take m points, and then take a temperature value t,t each decrease of 10%, each time let each point extend M points, if the answer is better than the current optimization, if there is no current excellent, to

The probability to update.

Until the temperature is less than K.

I'm taking the 0.05 m here, it's 20.

Code:

#include <iostream>using namespace std #include <cstdio> #include <cstring> #include <cmath># include<queue> #include <cstdlib> #include <ctime>double ans,ansx,ansy,x,y;int n;const int m=20; Double xx[1200],yy[1200];d ouble getdis (double x,double y) {double ans=0,a,b,zans=0;for (int i=0;i<n;i++) {a= (xx[i]-x ), b= (yy[i]-y); ans=sqrt (a*a+b*b); if (Zans<ans) {Zans=ans;}} return Zans;} struct point{double X,y,dis;}; Point A[m+10];int Main () {Srand (time (0)) and while (scanf ("%lf%lf%d", &x,&y,&n)!=eof) {for (int i=0;i<n;i++ {scanf ("%lf%lf", Xx+i,yy+i);} for (int i=0;i<m;i++) {A[i].x=rand ()% (int) X;a[i].y=rand ()% (int) Y;a[i].dis=getdis (A[I].X,A[I].Y);} Double T=max (x, y); t>=0.05; t*=0.9) {for (int i=0;i<m;i++) {for (int. j=0;j<m;j++) {Point temp;temp.x=a[i].x+sin (rand ()% (int) x) *t;temp.y=a[i] . Y+cos (rand ()% (int) y) *t;if (temp.x<0| | temp.y<0| | temp.x>x| | Temp.y>y) Continue;temp.dis=getdis (TEMP.X,TEMP.Y); if (Temp.dis<a[i].dis) {A[i].x=temp.x;a[i].y=temp.y;a[I].dis=temp.dis;} Else{int P=rand ()%10000;double e=exp ((A[i].dis-temp.dis)/T); int q= (int) e*p;if (p<=q) {a[i].x=temp.x;a[i].y= Temp.y;a[i].dis=temp.dis;}}}} Double zans=1e9,ansx,ansy;for (int i=0;i<m;i++) {if (A[i].dis<zans) zans=a[i].dis,ansx=a[i].x,ansy=a[i].y;} printf ("(%.1f,%.1f). \n%.1f\n", Ansx,ansy,zans);} return 0;}

  

HDU-3932 Groundhog Build Home simulated annealing algorithm

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.