poj 1379 模拟退火

/*

题意:给定n个点和范围,在范围内找到一个点,使得n个点到这个点的最小距离最大
模拟退火法
模拟退火的过程
1 找到这些点所在的范围,用两个点框定(代码中e1,e2两个点)
2 在这个范围内生成NUM个点(NUM自定)
3 对于每个生成的点i,在其周围生成NUM个点,一旦有点优于i,则替换。
4 缩小范围D,若D<精度,退出,否则执行 3
5 遍历所有NUM个点,找到val的最大值
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include<map>
#include<ctime>
using namespace std;
const int NUM=20;
const int RAD=1000;
struct point
{
	double x,y,val;
	point(){}
	point(double _x,double _y):x(_x),y(_y){}
}p[10001],May[NUM],e1,e2;
int n;
double X,Y;
double dis(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double judge(point t)//评价函数,得到点t的评价值val
{
	double len;
	len=1LL<<45;
	for(int i=0;i<n;i++)
	len=min(len,dis(t,p[i]));
	return len;
}
double Rand(){return rand()%(RAD+1)/(1.0*RAD);}//随机产生0-1的浮点数
point Rand_point(point a,point b)//在a,b框定的四边形内随机生成点
{
	double xx=a.x+(b.x-a.x)*Rand();
	double  yy=a.y+(b.y-a.y)*Rand();
	point tmp=point(xx,yy);
	tmp.val=judge(tmp);
	return tmp;
}
void solve(double D)
{
	May[0]=point(0,0);
	May[1]=point(X,Y);
	May[2]=point(0,Y);
	May[3]=point(X,0);
	//4个顶点的可能行较大,所以特殊构造
	for(int i=4;i<NUM;i++)
	May[i]=Rand_point(May[0],May[1]);//步骤2
	while(D>0.01)//步骤 3
	{
		for(int i=0;i<NUM;i++)
		for(int j=0;j<NUM;j++)
		{
			point tmp=Rand_point(point(max(0.0,May[i].x-D),max(0.0,May[i].y-D)),point(min(X,May[i].x+D),min(Y,May[i].y+D)));
			if(tmp.val>May[i].val)
			{
				May[i]=tmp;
			}
		}
		D*=0.9;
	}
	point ans;
	ans.val=0;
	for(int i=0;i<NUM;i++)
	if(May[i].val>ans.val)
	ans=May[i];
	printf("The safest point is (%.1f, %.1f).\n",ans.x,ans.y);
}
int main()
{
	srand(time(0));
	e2=point(0,0);
	int Case;
	scanf("%d",&Case);
	while(Case--)
	{
		scanf("%lf%lf%d",&X,&Y,&n);
		for(int i=0;i<n;i++)
		{
		scanf("%lf%lf",&p[i].x,&p[i].y);
		}
		solve(max(Y,X));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值