Question: it is to find the fenma point of the polygon and output the smallest distance.
Http://poj.org/problem? Id = 2420
Method: randomization, variable step size greedy method (Simulated Annealing ???)
First, randomly select a point. I took 0, 0 directly.
Select a step and start searching in four directions. If it is better, continue. Otherwise, reduce the step until the accuracy meets the requirements of the question.
Or eight directions.
[Cpp]
# Include <iostream>
# Include <fstream>
# Include <iomanip>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <cstdlib>
# Include <cmath>
# Include <set>
# Include <map>
# Include <queue>
# Include <stack>
# Include <string>
# Include <vector>
# Include <sstream>
# Include <cassert>
# Define LL long
# Define eps 1e-6
# Define inf 1ll <50
# Define zero (a) fabs (a) <eps
# Define N 20005
Using namespace std;
Struct Point {
Double x, y;
} P [105], cur, pre;
Int n;
Int way [4] [2] = {, 0,-, 0 };
Double dist (Point p1, Point p2 ){
Return sqrt (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y ));
}
Double get_dist (Point cen ){
Double sum = 0;
For (int I = 0; I <n; I ++)
Sum + = dist (cen, p [I]);
Return sum;
}
Int main (){
While (scanf ("% d", & n )! = EOF ){
For (int I = 0; I <n; I ++)
Scanf ("% lf", & p [I]. x, & p [I]. y );
Pre. x = 0; pre. y = 0;
Double step = 100, best = get_dist (pre );
While (step & gt; 0.2 ){
Bool OK = true;
While (OK ){
OK = false;
For (int I = 0; I <4; I ++ ){
Cur. x = way [I] [0] * step + pre. x;
Cur. y = way [I] [1] * step + pre. y;
Double dis = get_dist (cur );
If (dis <best) {best = dis; pre = cur; OK = true ;}
}
}
Step/= 2.0;
}
Printf ("% d \ n", (int) (best + 0.5) * 100/100 );
}
Return 0;
}