Find a point on a two-dimensional plane so that it can reach the distance and minimum of all given points. The distance is defined as Euclidean distance, what is the minimum distance and sum (the result must be rounded down )?
Train of Thought: if you cannot add some points, ask the distance and minimum values of all points, that is, the classic MST. If you can add only one point, then ask the minimum value, that is, the question of the horse point in the broad sense, if the number of points is not limited, the problem becomes the issue of the Stanner tree (which is an issue of the NPC)
This is obviously a broad-based Fermat problem. You can use the local greedy method to start from an initial point and continue to expand to the upper, lower, and left directions, if the distance from the current vertex to all vertices in one direction is smaller than the distance and distance from the current vertex to all vertices, the value of the current vertex is updated, until the expansion from four directions cannot be better than the current point, it means that the current point is local optimal, then the step size is reduced and the search continues from the given point, until the accuracy is smaller than the precision required by the question (integer)
It should be noted that local greedy (also called the mountain crawling method) is easily stuck in the local optimum, so it is often the Random Initial Point Multiple times to ensure that the local optimum is the global optimum, however, in the broad sense, Fermat points do not seem to have many local optimal points. You can select the origin point as the initial point.
// Poj2420
# Include <cstdio>
# Include <iostream>
# Include <string. h>
# Include <math. h>
# Define maxn110
Const short DX [5] = {,-, 0}, Dy [5] = {,-1 };
Using namespace STD;
Double X [maxn] = {0}, Y [maxn] = {0}; int N;
Double sumlen (double XX, double YY)
{
Double ret = 0;
For (INT I = 1; I <= N; I ++)
{
RET + = SQRT (XX-X [I]) * (XX-X [I]) + (yy-y [I]) * (yy-y [I]);
}
Return ret;
}
Int main ()
{
Scanf ("% d", & N );
For (INT I = 1; I <= N; I ++) scanf ("% lf", & X [I], & Y [I]);
Double A = 0, B = 0, min = sumlen (a, B), step = 100;
While (Step> 0.1)
{
Short flag = 1;
While (flag = 1) // if this layer of loop ends, it means that none of the four directions can get a better solution, that is, the local optimal solution is achieved.
{
Flag = 0;
For (INT I = 1; I <= 4; I ++)
{
Doublexx = a + dx [I] * step, YY = B + dy [I] * step, t = sumlen (XX, YY );
If (T <min) {min = T; A = xx; B = YY; flag = 1 ;}
}
}
STEP/= 2;
}
Printf ("% d", (INT) (min + 0.5 ));
Return 0;
}
What about poj 2420 a star not a tree? [Mountain crawler]