Title Link: http://poj.org/problem?id=2420
Test instructions: Give n points, find a point, so that the point to all other points of the sum of the smallest, that is, the cost of horse point.
Reference Link: http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html
This article is very good, I have a little white is a little clear.
Note the notes:
Simulated annealing: It is in a certain range to accept some not the optimal solution, to jump out of the local optimal, near the overall optimal.
Paste the Pseudo-code:
//#pragma COMMENT (linker, "/stack:1024000000,1024000000")#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<deque>#include<functional>#include<iterator>#include<Set>#include<utility>#include<stack>#include<queue>#include<iomanip>#include<cmath>using namespacestd;#defineN 1005#defineEPS 1e-8#defineINF 1e99#defineDelta 0.98#defineT 100intdx[4] = {0,0,-1,1};intdy[4] = {-1,1,0,0};structpoint{Doublex, y;} P[n];DoubleDist (Point A,point b) {returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y));}DoubleGetsum (Point p[],intN,point T) { DoubleAns =0; while(n--) {ans+=Dist (p[n],t); } returnans;}DoubleSearch (Point p[),intN) {Point S= p[0]; Doublet =T; DoubleAns =INF; while(t>EPS) { BOOLFlag =1; while(flag) {flag=0; for(intI=0; i<4; i++) {point z; z.x= S.x + dx[i]*T; Z.y= S.y + dy[i]*T; DoubleTP =getsum (p,n,z); if(ans>TP) {ans=TP; S=Z; Flag=1; } }} t*=Delta; } returnans;}intMain () {intN; while(~SCANF ("%d",&N)) { for(intI=0; i<n; i++) scanf ("%LF%LF",&p[i].x,&p[i].y); printf ("%.0lf\n", Search (p,n)); } return 0;}
View Code
POJ 2420, simulated annealing algorithm, cost of Horse point