Description
To prevent the thirsty anteater from entering his farm, Farmer John decided to dig a moat around his farm. There is a total of N (8<=n<=5,000) spring water in the farm, and the moat is always connected straight to the adjacent two-strand spring water on the channel. The moat must be able to protect all the springs, that is to say, to surround all the springs. The spring must be in the interior of the moat, or just on the river. Of course, the moat forms a closed ring. Digging a moat is an expensive project, so the savings of FJ hope the total length of the moat is as small as possible. Please write a program to calculate, in satisfying the requirements of the condition, the total length of the moat is the smallest. The coordinates of all springs are on the whole point of the range (1..10,000,000,1..10,000,000), and a spring water corresponds to a single definite coordinate. Also, any three-strand spring water is not in a straight line. Here is a map containing 20 springs, which is indicated by "*".
The line in the diagram is the best excavation scheme for the moat, that is, the shortest route that can surround all the springs. The route is from the upper left corner, followed by the spring's coordinates: (18,0), (6,-6), (0,-5), ( -3,-3), ( -17,0), ( -7,7), (0,4), (3,3). The total length of the path around the week is 70.8700576850888 (...). The answer only needs to retain two decimal places, so the output is 70.87.
Input
* Line 1th: An integer, N * 2nd. N+1 Line: Each line contains 2 integers separated by spaces, x[i] and y[i], i.e. the position coordinates of the spring of the unit I
Output
* Line 1th: Outputs a number that represents the shortest length of the moat that satisfies the condition. Keep Two decimal places
Sample Input20
2 10
3 7
22 15
12 11
20 3
28 9
1 12
9 3
14 14
25 6
8 1
25 1
28 4
24 12
4 15
13 5
26 5
21 11
24 4
1 8
Sample Output70.87
1 /**/2#include <iostream>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <algorithm>7 #definell Long Long8 using namespacestd;9 Const intmxn=10000;Ten intN; One structp{ A intx, y; - }P[MXN],S[MXN]; - inttop=0; the Doublesum=0; - -InlineintRead () { - intx=0, f=1; + CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}; + while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();}; A returnx*F; at } -Inline Poperator-(P a,p b) { -P T; t.x=a.x-b.x; T.Y=A.Y-B.Y;returnT; - } -inline LLoperator*(P a,p b) { - returna.x*b.y-a.y*b.x; in } - inline ll dis (P a,p b) { to return(a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y); + } -InlineBOOL operator<(P a,p b) { thell t= (a-p[1]) * (b-p[1]); * if(t==0)returnDis (p[1],a) <dis (p[1],b); $ returnT>0;Panax Notoginseng } - the voidGraham () { + intt=1, I; A for(intI=2; i<=n;i++) the if(P[i].y<p[t].y | | (p[i].y==p[t].y&&p[i].x<p[t].x)) t=i;//Find the Y-value minimum point as the starting point +Swap (p[1],p[t]); -Sort (p+2, p+n+1); $s[++top]=p[1]; $s[++top]=p[2]; - for(i=3; i<=n;i++){ - while((s[top]-s[top-1]) * (p[i]-s[top-1]) <=0) top--;//find "More out of the point" and discard the point on top of the stack. thes[++top]=P[i]; - }Wuyis[top+1]=p[1]; the for(i=1; i<=top;i++){ -Sum+=sqrt (DIS (s[i],s[i+1]));//Calculate Distance Wu } - return; About } $ intMain () { -n=read (); - inti,j; - intx, y; A for(i=1; i<=n;i++){ +P[i].x=read ();p [i].y=read (); the } - Graham (); $printf"%.2lf\n", sum); the return 0; the}
bzoj1670 Usaco2006 Building The moat moat excavation [convex hull template problem]