#include #include #include using namespace std; template inline void read(T&x) { x=0; char s=getchar(); bool f=false; while(!(s>='0'&&s<='9')) { if(s=='-') f=true; s=getchar(); } while(s>='0'&&s<='9') { x=(x<<1)+(x<<3)+s-'0'; s=getchar(); } if(f) x=(~x)+1; return; } #define re register #define temperature 1e5 #define cold 0.996 const int N=1e3+10; int numOfWeights; double ans, ansX, ansY; struct node { int x, y, w; } weights[N]; inline double calculatePotentialEnergy(double x, double y)//计算势能,势能越小解越优 { double energy=0.0; for(re int i=1; i<=numOfWeights; i++) energy+=sqrt((x-weights[i].x)(x-weights[i].x)+(y-weights[i].y)(y-weights[i].y))*weights[i].w; return energy; } inline void solve() { double t=temperature; while(t>1e-18) { double tmpX=ansX+(rand()+rand()-RAND_MAX)*t,tmpY=ansY+(rand()+rand()-RAND_MAX)*t; double tmp=calculatePotentialEnergy(tmpX,tmpY); double d=tmp-ans; if(d<0.0) ans=tmp,ansX=tmpX,ansY=tmpY; else if(exp(-d/t)RAND_MAX>rand()) ansX=tmpX,ansY=tmpY; t=cold; } } int main() { srand(rand()); read(numOfWeights); for(re int i=1; i<=numOfWeights; i++) { read(weights[i].x),read(weights[i].y),read(weights[i].w); ansX+=weights[i].x; ansY+=weights[i].y; } ansX/=numOfWeights,ansY/=numOfWeights,ans=calculatePotentialEnergy(ansX,ansY);//乱搞一个初始值 for(re int i=1; i<=4; i++)//多搞几次 solve(); printf("%.3lf %.3lf\n",ansX,ansY); return 0;

请把这份代码的变量名:#includecstdio#includecmath#includecstdlibusing namespace std;templatetypename Tinline void readT&x	x=0;	char s=getchar;	bool f=false;	while!s=0&&s=9			ifs==-			f=true;		s=getchar;		whiles=

原文地址: https://www.cveoy.top/t/topic/ixNE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录