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

Posted 晓风微微

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 3932 Groundhog Build Home 模拟退火算法相关的知识,希望对你有一定的参考价值。

 

  

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 模拟退火算法的主要内容,如果未能解决你的问题,请参考以下文章

hdu 3932 Groundhog Build Home——模拟退火

hdu 3932 Groundhog Build Home —— 模拟退火

HDU 4790:Just Random(容斥)

HDU 1042 N! 高精度乘法

bzoj3932 CQOI2015—任务查询系统

3932: [CQOI2015]任务查询系统