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.
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
Sample Output
243.06
Source
Recommend
凸包入门:
//78MS 248K 1553 B C++
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define N 105
struct node{
double x,y;
}p[N],stack[N];
double dist(node a,node b) //两点距离
{
return sqrt((a.y-b.y)*(a.y-b.y)+(a.x-b.x)*(a.x-b.x));
}
double crossprod(node a,node b,node c) //计算叉积
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int cmp(const void*a,const void*b) //按要求排序
{
node c=*(node*)a;
node d=*(node*)b;
double k=crossprod(p[],c,d);
if(k< || !k && dist(p[],c)>dist(p[],d))
return ;
return -;
}
double Graham(int n) //Graham扫描法
{
for(int i=;i<n;i++)
if(p[i].x<p[].x || p[i].x==p[].x && p[i].y<p[].y){
node temp=p[];
p[]=p[i];
p[i]=temp;
}
qsort(p+,n-,sizeof(p[]),cmp);
p[n]=p[];
for(int i=;i<;i++)
stack[i]=p[i];
int top=;
for(int i=;i<n;i++){
while(crossprod(stack[top-],stack[top],p[i])<= && top>=)
top--;
stack[++top]=p[i];
}
double ans=dist(stack[],stack[top]);
for(int i=;i<top;i++)
ans+=dist(stack[i],stack[i+]);
return ans;
}
int main(void)
{
int n;
while(scanf("%d",&n),n)
{
for(int i=;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
if(n==){
puts("0.00");continue;
}
if(n==){
printf("%.2lf\n",dist(p[],p[]));
continue;
}
printf("%.2lf\n",Graham(n));
}
return ;
}