POJ 1379

模拟退火算法。

随机MAX个点,然后,退火移动,选取距离所有点中最短中最长者即可。理解和我上一篇一样。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
using namespace std;
const int MAXN=1010;
const int MAX=50;
const double inf=1e10;
const double eps=1e-3;

struct point {
	double x,y;
};
point p[MAXN]; point tar[MAX];
double best[MAXN];
int n; double ans_dis; point s,tp;  double tmp_dis;
double X,Y;

double getdouble(){
	double ret=((rand()*rand())%1000000)*1.0/1e6;
	return ret;
}
double dist(point &u,point &v){
	return sqrt((u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y)); 
}

double work(point &t){
	double ret=inf;
	for(int i=0;i<n;i++){
		double tmp=dist(t,p[i]);
		ret=min(ret,tmp);
	}
	return ret;
}

int main(){
	int cas; bool flag;
	scanf("%d",&cas);
	srand(time(0));
	while(cas--){
		scanf("%lf%lf%d",&X,&Y,&n);
		for(int i=0;i<n;i++){
			scanf("%lf%lf",&p[i].x,&p[i].y);
		}
		for(int i=0;i<MAX;i++){
			tar[i].x=getdouble()*X;
			tar[i].y=getdouble()*Y;
			best[i]=work(tar[i]);
		}
		double T=100;
		for(double t=T;t>eps;t*=0.8){
			for(int i=0;i<MAX;i++){
				for(int j=0;j<30;j++){
					double tmp=getdouble();
					if(rand()&1) tmp*=-1;
					tp.x=tar[i].x+tmp*t;
					tmp=getdouble();
					if(rand()&1) tmp*=-1;
					tp.y=tar[i].y+tmp*t;
					if(tp.y>=0&&tp.y<Y&&tp.x>=0&&tp.x<=X){
						double f=work(tp);
						if(f>best[i]) {
							tar[i]=tp; best[i]=f;
						}
					}
				}
			}
		}
		ans_dis=best[0];
		for(int i=0;i<MAX;i++){
			double fe=best[i];
			if(fe>ans_dis){
				s=tar[i];
				ans_dis=fe;
			}
		}
		printf("The safest point is (%.1lf, %.1lf).\n",s.x,s.y);
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/jie-dcai/p/3906869.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值