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;
}

  

				
时间: 2024-08-10 05:32:44

HDU - 3932 Groundhog Build Home 模拟退火算法的相关文章

hdu 3932 Groundhog Build Home —— 模拟退火

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3932 找一个位置使距离最远的点的距离最小: 上模拟退火: 每次向距离最远的点移动,注意判断一下距离最远的点距离为0的情况. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib&

hdu 3932 Groundhog Build Home

Groundhog Build Home Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2647    Accepted Submission(s): 1074 Problem Description Groundhogs are good at digging holes, their home is a hole, usually

HDU 3932 Groundhog Build Home 【基础模拟退火】

和刚才那道是一模一样 不过求的是最小的,只要稍微修改一下就可以了~ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <stack>

poj-2420 A Star not a Tree?(模拟退火算法)

题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepted: 2491 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allo

PKU 1379 Run Away(模拟退火算法)

题目大意:原题链接 给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大. 解题思路:一开始想到用随机化算法解决,但是不知道如何实现.最后看了题解才知道原来是要用模拟退火算法解决. 不过个人感觉这个算法的实现过程中仍然采用了随机化算法.二者均属于概率算法.  参考链接 Point Goto_Rand_Dir(double key,Point temp)函数中,Point temp必须得定义在参数中,不能定义在函数内部, 否则temp没有初始值,无法进行后面的

两种改进的模拟退火算法求解大值域约束满足问题1.0

0引言 约束满足问题(Constraint Satisfaction Problem,CSP)是人工智能研究领域中一个非常重要的分支,现已成为理论计算机科学.数学和统计物理学等交叉学科研究中的热点问题.人工智能.计算机科学和自动控制等领域中的许多问题都可以归结为约束满足问题.同时,约束满足问题在实际问题如模式识别.决策支持.物流调度及资源分配等领域也有着非常广泛的应用. CSP由一个变量集合和一个约束集合组成.每个变量都有一个非空的可能值域,每个约束描述了一个变量子集与子集内各变量的相容赋值,所

Matlab随笔之模拟退火算法

问题描述: 我方有一个基地,经度和纬度为( 70,40).假设我方飞机的速度为 1000 公里/小时. 我方派一架飞机从基地出发,侦察完敌方所有目标,再返回原来的基地.在敌方每一目 标点的侦察时间不计,求该架飞机所花费的时间(假设我方飞机巡航时间可以充分长). 这是一个旅行商问题.我们依次给基地编号为 1,敌方目标依次编号为 2, 3,…, 101, 最后我方基地再重复编号为 102(这样便于程序中计算). 距离矩阵 D = ( dij )102×102 , 其中 dij 表示表示 i, j 两

大白话解析模拟退火算法(转)

优化算法入门系列文章目录(更新中): 1. 模拟退火算法 2. 遗传算法 一. 爬山算法 ( Hill Climbing ) 介绍模拟退火前,先介绍爬山算法.爬山算法是一种简单的贪心搜索算法,该算法每次从当前解的临近解空间中选择一个最优解作为当前解,直到达到一个局部最优解. 爬山算法实现很简单,其主要缺点是会陷入局部最优解,而不一定能搜索到全局最优解.如图1所示:假设C点为当前解,爬山算法搜索到A点这个局部最优解就会停止搜索,因为在A点无论向那个方向小幅度移动都不能得到更优的解. 图1 二. 模

POJ 1379 模拟退火算法

求规定平面上一点到已知点的最小距离最大的点. 模拟退火的流程是,随机构造几组解作为初始解空间,每次对当前解空间进行随机扩展,若发现更优解则替换. 进行的次数由参数人为控制,而随机扩展的幅度也是随着次数逐渐减小的. #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<cmath> #include<string> #include&