洛谷SP34 RUNAWAY - Run Away

题意翻译
在给定范围内找一个点,使得距离所有点的最小值最大。
模拟退火

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#define int long long 
using namespace std;
const int N=10005;
int t,n,x,y;
double ansx,ansy; 
struct node{
	int x,y;	
}dian[N],zhn;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
double cal(double now_x,double now_y){
	double maxx=2147483640.00;
	for(int i=1;i<=n;i++){
		double detx=(double)(dian[i].x)-now_x;
		double dety=(double)(dian[i].y)-now_y;
		double dis=sqrt(detx*detx+dety*dety);
		maxx=min(dis,maxx);
	}
	return maxx;
}
void SA(){
	double xx=ansx,yy=ansy;
	double ans=0.00;
	double b=2000.00,e=1e-14;
	double ch=0.993;
	for(double i=b;i>e;i*=ch){
		double new_x =xx+((rand()<<1)-RAND_MAX)*i;
        double new_y =yy+((rand()<<1)-RAND_MAX)*i;
        if(new_x>x||new_x<0||new_y>y||new_y<0) continue;
        double lgr=cal(new_x,new_y);
        if(lgr>ans){
        	ans=lgr;
        	xx=new_x;
        	yy=new_y;
        	ansx=xx;
        	ansy=yy;
		}
		else if((exp((lgr-ans)/i)*RAND_MAX)>rand()) {
			xx=new_x;
			yy=new_y;
		}
	}
}
main(){
    t=read();
    srand(23333333);
    srand(rand());
    srand(rand());//玄学种子
    while(t--){
    	int sum_x=0;
    	int sum_y=0;
    	x=read();y=read();n=read();
    	for(int i=1;i<=n;i++) {
    		dian[i].x=read();
    		dian[i].y=read();
    		sum_x+=dian[i].x;
    		sum_y+=dian[i].y;
		}
		ansx=(double)(sum_x)/n;
		ansy=(double)(sum_y)/n;
        SA();SA();SA();
        printf("The safest point is (%.1lf, %.1lf).\n", ansx, ansy);  
	}
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值