1143. Electric Pathtime limit:1.0 Second
Memory limit:64 Mbbackgroundat The team competition of the 10th National Student Informatics Olympic, which is organized At Hanoi National University, there is
NTeams participating. Each team was assigned to work in a camp. On the map, it can is seen that the camps is positioned on the vertices of a convex polygon with
NVertices
P1,
P2,...,
PN(The vertices is enumerated around the polygon in counter-clockwise order.) In order to achieve absolute safety providing electricity to the camps, besides an electric supplying system, the host Org Anization set up a path from a reserved electricity generator (which was placed in one of the camps) to every camp once, an D
The path ' s total length is minimum. Problemgiven the coordinates of the polygons ' vertices (the camps ' positions), determine the length of the electric path C Orresponding to the host organization ' s arrangement. Inputthe first line contains the integer
N(1≤
N≤200). The
I' th line of the next
NLines contains, real numbers
XI,
Yi, separated by a space, with no more than 3 digits after the decimal points, is vertex
Pi' s coordinates on the plane (with
I= 1, 2, ...,
N). The length of the path connecting the vertex (
XI,
Yi) and (
XJ,
YJ) is computed with the FORMULA:SQRT (
XI−
XJ) 2 + (
Yi−
YJ) 2). Outputthe only line should contain real number
L(written in real number format, with 3 digits after the decimal point), which are the total length of the electric path. Sample
input |
Output |
450.0 1.05.0 1.00.0 0.045.0 0.0 |
50.211 |
problem Source:The competition for selecting the Vietnam IOI team
Tags:None()difficulty:532 Test Instructions: The order gives the n points of a convex polygon on a plane, asking for the shortest length of a line to connect them. Analysis: First, you can feel that the line will not cross. Then we prove it: Consider the situation of four points first. Take four points of intersection. If cross, then the four-sided shape of these four points, it is certain that two diagonal lines will be selected, and then add an edge. Then, obviously, you can change a diagonal edge to an adjacent edge (connecting adjacent points). This is obviously shorter. Consider more points of the situation. You can select two endpoints of two lines that intersect, with a total of four points. The other points are then abstracted into edges, and because of the convex polygon, the same nature must be satisfied. It can be abstracted into four-point situations. Then there is this nature that can be done. Starting from both sides dp,dp[i][j][0..1] means from the first I to the first J, from the left or the right to start wiring. The transfer is obvious, there are only two kinds.
1 /**2 Create by Yzx-stupidboy3 */4#include <cstdio>5#include <cstring>6#include <cstdlib>7#include <cmath>8#include <deque>9#include <vector>Ten#include <queue> One#include <iostream> A#include <algorithm> -#include <map> -#include <Set> the#include <ctime> -#include <iomanip> - using namespacestd; -typedefLong LongLL; +typedefDoubleDB; - #defineMIT (2147483647) + #defineINF (1000000001) A #defineMLL (1000000000000000001LL) at #defineSZ (x) ((int) (x). Size ()) - #defineCLR (x, y) memset (x, y, sizeof (x)) - #definePUF Push_front - #definePub push_back - #definePOF Pop_front - #definePOB pop_back in #defineFT first - #defineSD Second to #defineMk Make_pair + -InlineintGetint () the { * intRet =0; $ CharCh =' ';Panax Notoginseng BOOLFlag =0; - while(! (Ch >='0'&& Ch <='9')) the { + if(Ch = ='-') Flag ^=1; ACh =GetChar (); the } + while(Ch >='0'&& Ch <='9') - { $RET = RET *Ten+ Ch-'0'; $Ch =GetChar (); - } - returnFlag? -Ret:ret; the } - Wuyi Const intN =410; the struct Point - { Wu DB x, y; - AboutInlinevoidRead () $ { -scanf"%LF%LF", &x, &y); - } - } Arr[n]; A intN; +DB dp[n][n][2]; the BOOLvisit[n][n][2]; - $InlinevoidInput () the { thescanf"%d", &n); the for(inti =1; I <= N; i++) Arr[i]. Read (); the } - in Inline DB SQR (db x) the { the returnX *x; About } the theInline DB Dist (ConstPoint &a,ConstPoint &B) the { + returnsqrt (SQR (a.x-b.x) + SQR (A.Y-b.y)); - } the BayiInline DB Work (intLeftintRightBOOLtype) the { the if(left >= right)return 0; - if(Visit[left][right][type]) - returnDp[left][right][type]; theVisit[left][right][type] =1; theDB ret =1.0*INF, CNT; the if(Type = =0) the { -CNT = Dist (Arr[left], Arr[left +1]); theCNT + = Work (left +1, right,0); theRET =min (ret, CNT); the 94CNT =Dist (Arr[left], arr[right]); theCNT + = Work (left +1, right,1); theRET =min (ret, CNT); the }98 Else About { -CNT = Dist (Arr[right], arr[right-1]);101CNT + = Work (left, right-1,1);102RET =min (ret, CNT);103 104CNT =Dist (Arr[right], arr[left]); theCNT + = Work (left, right-1,0);106RET =min (ret, CNT);107 }108 109 returnDp[left][right][type] =ret; the }111 theInlinevoidSolve ()113 { the for(inti = n +1; I <=2N i++) theArr[i] = arr[i-n]; the 117DB ans =1.0*INF, CNT;118 for(inti =1; I <= N; i++)119 { -CNT = Work (i, i + N-1,0);121Ans =min (ans, cnt);122CNT = Work (i, i + N-1,1);123Ans =min (ans, cnt);124 } the 126printf"%.3lf\n", ans);127 } - 129 intMain () the {131Freopen ("a.in","R", stdin); the Input ();133 Solve ();134 return 0;135}
View Code
Ural 1143. Electric Path