求助
查看原帖
求助
400333
qzilr楼主2022/9/15 14:37

模拟退火一直有一两个点不过,麻烦哪位dalao帮忙看看

#include<bits/stdc++.h>
#define rg register
using namespace std;
const double eps=1e-15;
const double down=0.996;
const int N=1e3+3;
struct point{double x,y,w;}a[N];
int n;
double tx=0,ty=0,nx,ny,nw,ansx,ansy,answ;
inline double calc(double nx,double ny){
	double tot=0;
	for(rg int i=1;i<=n;++i){
		double dx=nx-a[i].x,dy=ny-a[i].y;
		tot+=sqrt(dx*dx+dy*dy)*a[i].w;
	}
	return tot;
}
inline void sa(){
	double T=30000;
	while(T>eps){
		nx=ansx+(rand()*2-RAND_MAX)*T,ny=ansy+(rand()*2-RAND_MAX)*T,nw=calc(nx,ny);
		double d=nw-answ;
		if(d<0)	ansx=nx,ansy=ny,answ=nw;
		else if(exp(-d/T)*RAND_MAX>rand())	ansx=nx,ansy=ny;
		T*=down;
	}
}
signed main(){
	srand(time(0));
	scanf("%d",&n);
	for(rg int i=1;i<=n;++i)	scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].w),tx+=a[i].x,ty+=a[i].y;
	ansx=tx/(double)n,ansy=ty/(double)n,answ=calc(ansx,ansy);
	for(rg int i=1;i<=10;++i) sa();
	printf("%.3lf %.3lf",ansx,ansy);
	return 0;
}
2022/9/15 14:37
加载中...