hdu 1392 Surround the Trees

来源:互联网 发布:大数据分析入门书籍 编辑:程序博客网 时间:2024/06/08 01:30

Surround the Trees

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


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
 
#include<stdio.h>#include<math.h>#include<queue>#include<iostream>#include<algorithm>using namespace std;#define Max 1003#define PI 3.141592653int top;struct point{double x,y;}p[Max],stack[Max];double dis(point p1,point p2)      // 两点的距离的平方{return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);}double multi(point p1,point p2,point p0)    //叉积{return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);  }int cmp(point a,point b)     {if(multi(a,b,p[0])>0) return 1;if(multi(a,b,p[0])==0 && dis(a,p[0])<dis(b,p[0]))  return 1;return 0;} void GS(point p[],point stack[],int n)     //Graham_scan算法 {int i,k=0;top=2;point temp;for(i=0;i<n;i++)          //寻找最下且偏左的点if(p[i].y<p[k].y||((p[i].y)==p[k].y)&&(p[i].x<p[k].x))    k=i;temp=p[0];            //将最下且偏左的点指定为p[0]p[0]=p[k];p[k]=temp;sort(p+1,p+n,cmp);     //按极角从小到大,距离偏短进行排序stack[0]=p[0];stack[1]=p[1];stack[2]=p[2];for(i=3;i<n;i++){while(top>1 && multi(p[i],stack[top],stack[top-1])>=0)  top--;            // 出栈stack[++top]=p[i];    // 进栈}}int main (){   double sum;int t,n,l,i,j;while(~scanf("%d",&n)&&n!=0){sum=0;for(i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);    if(n==1)    printf("0.00\n");     // 需要特判    else if(n==2)                    // {double c=sqrt(dis(p[1],p[0]));printf("%.2lf\n",c);//continue;}           else if (n>2){GS(p,stack,n);        // n个点for(i=0;i<top;i++)sum+=sqrt(dis(stack[i],stack[i+1]));   // 凸包每条边的长度sum+=sqrt(dis(stack[top],stack[0]));       // 注意 首尾两点连接成边 的长度printf("%.2lf\n",sum);       // 求凸包周长}     }return 0;}


0 0
原创粉丝点击