This is the most God-made mess I've ever seen!
Open the pit first.
AC POJ 2420
1#include <cstdio>2#include <fstream>3#include <iostream>4 5#include <cstdlib>6#include <cstring>7#include <algorithm>8#include <cmath>9 Ten#include <queue> One#include <vector> A#include <map> -#include <Set> -#include <stack> the#include <list> - -typedef unsignedint UINT; -typedefLong Long intll; +typedef unsignedLong Long intull; -typedefDoubledb; + A using namespacestd; at -InlineintGetint () - { - intres=0; - CharC=GetChar (); - BOOLMi=false; in while(c<'0'|| C>'9') mi= (c=='-'), c=GetChar (); - while('0'<=c && c<='9') res=res*Ten+c-'0', c=GetChar (); to returnMi? -Res:res; + } - inline ll getll () the { *ll res=0; $ CharC=GetChar ();Panax Notoginseng BOOLMi=false; - while(c<'0'|| C>'9') mi= (c=='-'), c=GetChar (); the while('0'<=c && c<='9') res=res*Ten+c-'0', c=GetChar (); + returnMi? -Res:res; A } the +DB eps=1e- -; -InlineBOOLfeq (db a,db B) ${returnFabs (A-B) <EPS;} $ -Template<typename type> -Inline Type avg (ConstType A,ConstType b) the{returnA + ((b-a)/2); } - Wuyi the //============================================================================== - //============================================================================== Wu //============================================================================== - //============================================================================== About $ - - intN; - ADB x[ the]; +DB y[ the]; the - Inline db dis2 (db x1,db y1,db x2,db y2) ${returnsqrt ((x1-x2) * (X1-X2) + (y1-y2) * (y1-y2)); } the the Inline db getdist (db x,db Y) the { theDB l=0; - for(intI=0; i<n;i++) inl+=Dis2 (X,y,x[i],y[i]); the returnl; the } About the int_cnt=0; the intrands=0; theInlineintGetrand () + { -_cnt++; the if(_cnt==32768*32768) Srand (+ +rands);Bayi returnRAND () +32768*rand (); the } the - Inline db Dbrand () -{return(DB) (rand () +32768*rand ())/(DB) (32768*32768); } the the DB Res; the the - intMain () the { the Srand (rands); the 94 while(SCANF ("%d", &n) >0) the { the for(intI=0; i<n;i++) the {98x[i]=getint (); Abouty[i]=getint (); - }101 102 //searching.103DB cx=0, cy=0;104res=getdist (cx,cy); the 106DB t=1e5;107 108 while(t>=1e-2)109 { theDB rx=cx+ (Dbrand ()-0.5) *t*2.0;111DB ry=cy+ (Dbrand ()-0.5) *t*2.0; theDB dist=getdist (rx,ry);113DB d=res-Dist; the if(d>0) the { theres=Dist;117cx=Rx;118cy=ry;119 } -t*=0.98;121 }122 123printf"%.0f\n", res);124 } the 126 127 return 0; -}
View Code
Note that the program does not write a complete simulated annealing .... We only accept the optimal parameters, rather than accept some of the worse parameters ...
But the search scope here uses the number of thermometers t ....
DB rx=cx+ (Dbrand ()-0.5) *t*2.0;d b ry=cy+ (Dbrand ()-0.5) *t*2.0;
This should also be simulated annealing bar (simulating the irregular movement of molecules) ... Anyway, I can drop this question ...
Specific algorithm reference
Http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html
Simulated annealing algorithm