萌新鸽子求助56分
查看原帖
萌新鸽子求助56分
227728
冰糖鸽子「僕は…」楼主2021/5/5 10:29

RT,正常大概的模拟退火


// Problem: P1337 [JSOI2004]平衡点 / 吊打XXX
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1337
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define M 1005
#define int long long
const int inf=1e9+7;
int n;
double ans,nans,oans,rans=inf;
double ox,oy,nx,ny,rx,ry;
struct node {double x,y,w;} p[M];
double d(node X,node Y) {return sqrt((X.x-Y.x)*(X.x-Y.x)+(X.y-Y.y)*(X.y-Y.y));}
double CHECK(double wx,double wy)
{
	double res=0;
	for(int i=1;i<=n;i++) {res+=p[i].w*(d(p[i],node{wx,wy,0}));}
	if(res<rans)
	{
		rans=res;rx=wx;ry=wy;
		// cout<<rans<<' '<<rx<<' '<<ry<<endl;
	}
	return res;
}
void SA()
{
	nx=ox;ny=oy;ans=oans;
	double Tn=2333,Te=1e-16,T0=0.999,px,py;
	while(Tn>Te)
	{
		px=(rand()+rand()-RAND_MAX)*Tn+nx;
		py=(rand()+rand()-RAND_MAX)*Tn+ny;
		nans=CHECK(px,py);
		if(nans<ans) {ans=nans;nx=px;ny=py;}
		else if(exp((ans-nans)/Tn)*RAND_MAX>rand()) {ans=nans;nx=px;ny=py;}
		Tn*=T0;
	}
}
signed main()
{
	cin>>n;srand(20090227);
	for(int i=1;i<=n;i++) {cin>>p[i].x>>p[i].y>>p[i].w;nx+=p[i].x;ny+=p[i].y;}
	nx/=n;ny/=n;ans=CHECK(nx,ny);ox=nx;oy=ny;oans=ans;
	while ((double)clock()/CLOCKS_PER_SEC<0.7) SA();
	printf("%.3lf %.3lf",rx,ry);
	return 0;
}
2021/5/5 10:29
加载中...