hdu 1392 Surround the Trees (凸包)

Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7043    Accepted Submission(s): 2688


Problem Description
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him? 
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.



There are no more than 100 trees.
 
Input
The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

Zero at line for number of trees terminates the input for your program.
 
Output
The minimal length of the rope. The precision should be 10^-2.
 
Sample Input
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
 
Sample Output
243.06
 
Source
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  2150 1147 1558 1374 2202 
 

 凸包入门:

 1 //78MS    248K    1553 B    C++
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<math.h>
 5 #define N 105
 6 struct node{
 7     double x,y;
 8 }p[N],stack[N];
 9 double dist(node a,node b) //两点距离 
10 {
11     return sqrt((a.y-b.y)*(a.y-b.y)+(a.x-b.x)*(a.x-b.x));
12 }
13 double crossprod(node a,node b,node c) //计算叉积 
14 {
15     return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
16 }
17 int cmp(const void*a,const void*b) //按要求排序  
18 {
19     node c=*(node*)a;
20     node d=*(node*)b;
21     double  k=crossprod(p[0],c,d);
22     if(k<0 || !k && dist(p[0],c)>dist(p[0],d))
23         return 1;
24     return -1;
25 }
26 double Graham(int n) //Graham扫描法 
27 {
28     for(int i=1;i<n;i++)
29         if(p[i].x<p[0].x || p[i].x==p[0].x && p[i].y<p[0].y){
30             node temp=p[0];
31             p[0]=p[i];
32             p[i]=temp;
33         }
34     qsort(p+1,n-1,sizeof(p[0]),cmp);
35     p[n]=p[0];
36     for(int i=0;i<3;i++)
37         stack[i]=p[i];
38     int top=2;
39     for(int i=3;i<n;i++){
40         while(crossprod(stack[top-1],stack[top],p[i])<=0 && top>=2) 
41             top--;
42         stack[++top]=p[i];
43     }
44     double ans=dist(stack[0],stack[top]);
45     for(int i=0;i<top;i++)
46         ans+=dist(stack[i],stack[i+1]);
47     return ans;
48 }
49 int main(void)
50 {
51     int n;
52     while(scanf("%d",&n),n)
53     {
54         for(int i=0;i<n;i++)
55             scanf("%lf%lf",&p[i].x,&p[i].y);
56         if(n==1){
57             puts("0.00");continue;
58         }
59         if(n==2){
60             printf("%.2lf
",dist(p[0],p[1]));
61             continue;
62         }
63         printf("%.2lf
",Graham(n));
64     }
65     return 0;
66 }
原文地址:https://www.cnblogs.com/GO-NO-1/p/3765139.html