您的位置:首页 > 其它

HDOJ题目1392Surround the Trees(数学几何,凸包模板)

2014-09-04 23:18 711 查看


Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7644    Accepted Submission(s): 2921


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
The minimal length of the rope. The precision should be 10^-2.

 

[align=left]Sample Input[/align]

9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0

 

[align=left]Sample Output[/align]

243.06

 

[align=left]Source[/align]
Asia 1997, Shanghai (Mainland
China)
 

[align=left]Recommend[/align]
Ignatius.L   |   We have carefully selected several similar problems for you:  1115 2150 1348 1147 1558 
 
凸包:http://www.cnblogs.com/jbelial/archive/2011/08/05/2128625.html

ac代码

#include<string.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct s
{
double x,y;
}p[101],temp;
int stack[101];
int top;
double muti(struct s pi,struct s pj,struct s pk)
{
return (pj.y-pk.y)*(pi.x-pk.x)-(pj.x-pk.x)*(pi.y-pk.y);
}
int cmp(const void *a,const void *b)
{
struct s *c=(struct s *)a;
struct s *d=(struct s *)b;
double k=muti(*c,*d,p[0]);
if(k<=0)
return 1;
else
return -1;
}
double dis(struct s a,struct s b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF,n)
{
int i,j,k,u=0;
double sum=0;
for(i=0;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
if(n<2)
{
printf("0.00\n");
continue;
}
if(n==2)
{
printf("%.2lf\n",dis(p[0],p[1]));
continue;
}

for(i=0;i<n;i++)
{
if(p[i].y<p[u].y||((p[i].y==p[u].y)&&p[i].x<p[u].x))
{
/*temp=p[i];
p[i]=p[0];
p[0]=temp;*/
u=i;
}
}
temp=p[u];
p[u]=p[0];
p[0]=temp;
qsort(p+1,n-1,sizeof(p[1]),cmp);
for(i=0;i<=2;i++)
stack[i]=i;
top=2;
for(i=3;i<n;i++)
{
while(muti(p[stack[top]],p[i],p[stack[top-1]])<=0)
top--;
stack[++top]=i;
}
for(i=1;i<=top;i++)
{
sum+=dis(p[stack[i]],p[stack[i-1]]);
}
sum+=dis(p[stack[0]],p[stack[top]]);
printf("%.2lf\n",sum);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: