POJ 2420, simulated annealing algorithm, cost of Horse point

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.