#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
int n,totx,toty;double ansx,ansy;
struct node{int x,y,z;}a[1005];
inline double calc(double x,double y){//计算当前的答案
double cnt=0;
for(int i=1;i<=n;++i){
double xx=x-a[i].x,yy=y-a[i].y;
cnt+=sqrt(xx*xx+yy*yy)*a[i].z;
}
return cnt;
}
inline void mnth(){
double T=2003,eps=1e-14;//这两个东西可以尽情改
while(T>eps){
double nowx=ansx+(rand()*2-RAND_MAX)*T;
double nowy=ansy+(rand()*2-RAND_MAX)*T;
//rand()*2-RAND_MAX可以得到[-RAND_MAX,RAND_MAX]之间的数
double delta=calc(nowx,nowy)-calc(ansx,ansy);
if(delta<0)ansx=nowx,ansy=nowy;
else if(exp(-delta/T)*RAND_MAX>rand())ansx=nowx,ansy=nowy;
T*=0.999;//0.999也可以尽情改,最好在[0.95,0.99]之间
}
}
int main(){
srand((int)time(NULL));n=read();
for(int i=1;i<=n;++i){
a[i].x=read();a[i].y=read();a[i].z=read();
totx+=a[i].x;toty+=a[i].y;
}
ansx=(double)totx*1.0/n;ansy=(double)toty*1.0/n;//先猜一个较优值
mnth();printf("%.3lf %.3lf\n",ansx,ansy);
return 0;
}
原文:https://www.cnblogs.com/PPXppx/p/11668559.html