求助,模拟退火10pts,只对了#7,调参调了一页了
查看原帖
求助,模拟退火10pts,只对了#7,调参调了一页了
508530
P7GAB楼主2023/10/12 22:23
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+234;
const double delta=0.99433;
const double aim=1e-10;
const double rd=RAND_MAX;
struct node {
	int x,y,r;
} build[11],en[maxn];
int n,m,r;
double dis(double x,double y,double xx,double yy) {
	double dist=sqrt((xx-x)*(xx-x)+(yy-y)*(yy-y));
	return dist;
}
double calculate(double x,double y) {
	double ans=0;
	double R=r;
	for(int i=1; i<=n; i++)
		R=min(R,dis(build[i].x,build[i].y,x,y)-build[i].r);
	for(int i=1; i<=m; i++) {
		if(dis(en[i].x,en[i].y,x,y)<=R)
			ans+=1;
	}
	return ans;
}
int ansx,ansy;
double ans;
void sa() {
	double temp=2449,x=ansx,y=ansy;
	while(temp>aim) {
		double nowx=x+((rand()<<1)-rd)*temp;
		double nowy=y+((rand()<<1)-rd)*temp;
		double nowans=calculate(nowx,nowy);
		double check=nowans-ans;
		if(check>0) {
			x=nowx,y=nowy;
			ansx=nowx,ansy=nowy;
			ans=nowans;
		} else if(exp(-check/temp)*rd<rand()) {
			x=nowx;
			y=nowy;
		}
		temp*=delta;
	}
}
int main() {
	srand(4327432);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>r;
	for(int i=1; i<=n; i++)
		cin>>build[i].x>>build[i].y>>build[i].r;
	for(int i=1; i<=m; i++) {
		cin>>en[i].x>>en[i].y;
		ansx+=en[i].x;
		ansy+=en[i].y;
	}
	ansx/=m,ansy/=m;
	ans=calculate(ansx,ansy);
	sa();
	sa();
	sa();
	cout<<ans<<endl;
	return 0;
}
2023/10/12 22:23
加载中...