码迷,mamicode.com
首页 > 其他好文 > 详细

计算几何(凸包模板):HDU 1392

时间:2016-10-25 16:20:33      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:splay   img   this   round   image   app   opera   ram   images   

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
  这道题就是凸包模板。
  http://www.cnblogs.com/jbelial/archive/2011/08/05/2128625.html
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cmath>
 6 using namespace std;
 7 const double eps=1e-10;
 8 const int N=110;
 9 struct Point{
10     double x,y;
11     Point(double x_=0,double y_=0){x=x_;y=y_;}
12     friend Point operator-(Point a,Point b){
13         return Point(a.x-b.x,a.y-b.y);
14     }
15 }p[N],st[N];
16 double sqr(double x){return x*x;}
17 double dis(Point a,Point b){return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}
18 double cross(Point a,Point b){return a.x*b.y-a.y*b.x;}
19 bool cmp(Point a,Point b){
20     double s=cross(a-p[0],b-p[0]);
21     if(fabs(s)>=eps)return s>=eps;
22     return dis(a,p[0])<dis(b,p[0]);
23 }
24 int n,t,top;
25 double ans;
26 int main(){
27     while(scanf("%d",&n)!=EOF&&n){
28         for(int i=0;i<n;i++)
29             scanf("%lf%lf",&p[i].x,&p[i].y);
30         if(n==1){
31             puts("0.00");
32             continue;
33         }
34         if(n==2){
35             printf("%.2f\n",dis(p[0],p[1]));
36             continue;
37         }
38         t=0;
39         for(int i=1;i<n;i++)if(p[i].y<p[t].y||p[i].y==p[t].y&&p[i].x<p[t].x)t=i;
40         if(t)swap(p[0],p[t]);
41         sort(p+1,p+n,cmp);
42         p[n]=p[0];top=0;
43         st[++top]=p[0];
44         st[++top]=p[1];
45         st[++top]=p[2];
46         for(int i=3;i<=n;i++){
47             while(top>=2&&cross(st[top]-p[i],st[top-1]-p[i])>=0)top--;
48             st[++top]=p[i];
49         }
50         ans=0;
51         for(int i=2;i<=top;i++)ans+=dis(st[i],st[i-1]);
52         printf("%.2f\n",ans);
53     }
54     return 0;
55 }

 

计算几何(凸包模板):HDU 1392

标签:splay   img   this   round   image   app   opera   ram   images   

原文地址:http://www.cnblogs.com/TenderRun/p/5997016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!