1670: [Usaco2006 oct]building The excavation of the moat moat time limit: 3 Sec Memory Limit: MB
Submit: 387 Solved: 288
[Submit] [Status] [Discuss] 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.87HINT
Source
Convex packet Jam
convex hull template problem
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) # Define ll long Long#define maxn 5005using namespace Std;int n,top;double ans;struct p{int x, y;} P[maxn],s[maxn];inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} Inline p operator-(const p &A,CONST p &b) {return (P) {A.X-B.X,A.Y-B.Y};} inline ll operator* (const p &A,CONST p &b) {return a.x*b.y-a.y*b.x;} inline ll dis (P a,p b) {return (a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y);} inline bool operator< (const p &A,CONST p &b) {ll t= (a-p[1]) * (b-p[1]), if (t==0) return dis (p[1],a) <dis (p[1), b); else return t<0;} inline void Solve () {int t=1; F (I,2,n) if (p[i].y<p[t].y| | (p[i].y==p[t].y&&p[i].x<p[t].x)) T=i;swap (P[1],p[t]); sort (p+2,p+n+1); s[++top]=p[1];s[++top]=p[2]; F (I,3,n) {while (top>=2&& (S[top]-s[top-1]) * (P[i]-s[top-1]) >=0) top--;s[++top]=p[i];} S[TOP+1]=P[1]; F (i,1,top) ans+=sqrt (DIS (s[i],s[i+1));} int main () {n=read (); F (I,1,n) P[i].x=read (), P[i].y=read (), Solve ();p rintf ("%.2lf\n", ans); return 0;}
bzoj1670 "Usaco2006 Oct" Building excavation of the moat Moat