萌新求助模拟退火 11pts
查看原帖
萌新求助模拟退火 11pts
600442
DreamSoarUpward楼主2022/12/13 09:51

光速趋势。

#include<bits/stdc++.h>
using namespace std;
int n;
int xx[1005];
int yy[1005];
int ww[1005];
int sx,sy;
double ansx,ansy;
double ans=1e18;
double t;
const double delta=0.98;
double cal(double x,double y){
    double r=0;
    for (int i=1;i<=n;i++) {
    	double dx=x-xx[i];
    	double dy=y-yy[i];
    	r+=sqrt(dx*dx+dy*dy)*ww[i];
    }
    return r;
}
double Rand(){return ((rand()<<1)*RAND_MAX)*t;}
void sa(){
	double x=ansx;
	double y=ansy;
	t=2500;
	while(t<1e-15){
		double nx=x+Rand();
		double ny=y+Rand();
		double now=cal(nx,ny);
		double Delta=now-ans;
		if(Delta<0){
			x=nx; y=ny;
			ansx=x;
			ansy=y;
			ans=now;
		}
		else if(exp(-Delta/t)*RAND_MAX>rand()){
			x=nx; y=ny;
		}
		t*=delta;
	}
}
void solve(){
	ansx=(double)sx/n;
	ansy=(double)sy/n;
	for(int i=1;i<=5;i++) sa();
}
int main(){
	srand(time(0));
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>xx[i]>>yy[i]>>ww[i];
		sx+=xx[i];
		sy+=yy[i];
	}
	solve();
	cout<<fixed<<setprecision(3)<<ansx<<" "<<ansy<<endl;
	return 0;
}
2022/12/13 09:51
加载中...