HDU 1392 Surround the Trees(凸包)

來源:互聯網
上載者:User
Surround the Trees

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


Problem DescriptionThere 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. 


InputThe 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. 


OutputThe 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
 


SourceAsia 1997, Shanghai (Mainland China) 


RecommendIgnatius.L 赤裸裸的凸包,不解釋了,注意不要越界!

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <cmath>using namespace std;#define eps 1e-6#define PI 3.14159265struct point{double x;double y;}po[1500],temp;int n,l,pos;bool zero(double a){return fabs(a) < eps;}double dis(point &a,point &b)//返回兩點之間距離的平方{return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}double across(point &a,point &b,point &c)//求a b and a c 的X積{return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);}int cmp(const void *a,const void *b){return across(po[0],*(point*)a,*(point*)b) > 1e-8 ? -1 : 1;}int select(){int i,j,k=1;for(i=2;i<n;i++){if(zero(across(po[0],po[k],po[i]))){if(dis(po[0],po[k]) < dis(po[0],po[i]))po[k]=po[i];}elsepo[++k]=po[i];}return k+1;}int graham(int num){double ans=0;int i,j,k=2;po[num]=po[0];//fangbian num++;for(i=3;i<num;i++){while(across(po[k-1],po[k],po[i]) < -eps){k--;}po[++k]=po[i];//就這個迴圈結束,不需要了!}for(i=1;i<=k;i++){ans+=dis(po[i-1],po[i]);}printf("%.2lf\n",ans);return 0;}int main(){int i,j,k;point my_temp;while(scanf("%d",&n),n){scanf("%lf%lf",&po[0].x,&po[0].y);temp=po[0];pos=0;for(i=1;i<n;i++){scanf("%lf%lf",&po[i].x,&po[i].y);if(po[i].y < temp.y)temp=po[i],pos=i;}if(n==2){printf("%.2lf\n",dis(po[0],po[1]));continue;}my_temp=po[0];po[0]=po[pos];po[pos]=my_temp;qsort(po+1,n-1,sizeof(po[0]),cmp);graham(select());}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.