码迷,mamicode.com
首页 > 编程语言 > 详细

HDU - 3932 Groundhog Build Home 模拟退火算法

时间:2017-11-08 22:22:21      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:解法   a*   输出   test   like   number   nts   覆盖   from   

 

  

Groundhogs are good at digging holes, their home is a hole, usually a group of groundhogs will find a more suitable area for their activities and build their home at this area .xiaomi has grown up, can no longer live with its parents.so it needs to build its own home.xiaomi like to visit other family so much, at each visit it always start from the point of his own home.Xiaomi will visit all of the groundhogs‘ home in this area(it will chose the linear distance between two homes).To save energy,xiaomi would like you to help it find where its home built,so that the longest distance between xiaomi‘s home and the other groundhog‘s home is minimum.

InputThe input consists of many test cases,ending of eof.Each test case 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 two side of this rectangle, The number N stands for the number of holes.Then exactly N lines follow, each containing two integer numbers xi and yi (0 <= xi <= X, 0 <= yi <= Y) indicating the coordinates of one home.OutputPrint exactly two lines for each test case.The first line is 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 1
10 10
1000 50 4
0 0
1 0
0 1
1 1

Sample Output

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

题意:
最小覆盖圆,有n个点,给一个坐标轴,给一个X,Y的范围,在这个范围内找一个点,使得到所有点的最大距离最小,也就是覆盖n个点的最小圆,输出这个圆的圆心坐标。
解法:
1.最小覆盖圆算法——不会
2.模拟退火算法
  随机取m个点,然后取一个温度值T,T每次减少10%,每次让每个点延伸m个点,如果答案比当前优更新,如果没有当前优,以

技术分享

的概率去更新。

直到温度小于k.

我这里k取的是0.05 m取的是20

 

代码:

 

#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];
double 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));
	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);
		for(;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 模拟退火算法

标签:解法   a*   输出   test   like   number   nts   覆盖   from   

原文地址:http://www.cnblogs.com/xfww/p/7806306.html

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