zoukankan      html  css  js  c++  java
  • hdu 1392 Surround the Trees 凸包模板

    Surround the Trees

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


    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
     
    Source
     
     
     
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 using namespace std;
     8 
     9 struct node
    10 {
    11     int x,y;
    12 };
    13 node a[102],stack1[102];
    14 
    15 double dis(node n1,node n2)//求距离
    16 {
    17     return (double)sqrt( (n1.x-n2.x)*(n1.x-n2.x)*1.0 + (n1.y-n2.y)*(n1.y-n2.y)*1.0 );
    18 }
    19 double cross(node a,node n1,node n2)// 为正时 "左转"
    20 {
    21     return (n1.x-a.x)*(n2.y-a.y) - (n1.y-a.y)*(n2.x-a.x);
    22 }
    23 bool cmp(node n1,node n2)// 叉乘越小的,越靠前
    24 {
    25     double k = cross(a[0],n1,n2);
    26     if( k>0) return true;
    27     else if( k==0 && dis(a[0],n1)<dis(a[0],n2))
    28         return true;
    29     else return false;
    30 }
    31 void Graham(int n)
    32 {
    33     int i,head;
    34     double r=0;
    35     for(i=1;i<n;i++)
    36         if(a[i].x<a[0].x ||(a[i].x==a[0].x&&a[i].y<a[0].y ) )
    37             swap(a[0],a[i]);
    38     sort(a+1,a+n,cmp); //排序
    39     a[n]=a[0]; //为了对最后一点的检验是否为满足凸包。cross(stack1[head-1],stack1[head],stack[i]);
    40     stack1[0]=a[0];
    41     stack1[1]=a[1];
    42     stack1[2]=a[2];// 放入3个先
    43     head=2;
    44     for(i=3;i<=n;i++)
    45     {
    46         while( head>=2 && cross(stack1[head-1],stack1[head],a[i])<=0 )head--; 
    47         // == 包含了重点和共线的情况。此题求周长,并没有关系。所以不加==,也是可以的。
    48         stack1[++head]=a[i];
    49     }
    50     for(i=0;i<head;i++) //不是<=.  因为 a[0]在 0 和 head 两个位置都出现了。
    51     {
    52         r=r+dis(stack1[i],stack1[i+1]);
    53     }
    54     printf("%.2lf
    ",r);
    55 }
    56 int main()
    57 {
    58     int i,n;
    59     while(scanf("%d",&n)>0)
    60     {
    61         if(n==0)break;
    62         for(i=0;i<n;i++)
    63             scanf("%d%d",&a[i].x,&a[i].y);//end input
    64         if(n==1)//特判 
    65         {
    66             printf("0.00
    ");
    67             continue;
    68         }
    69         if(n==2)//此题的特判
    70         {
    71             printf("%.2lf
    ",dis(a[0],a[1]));
    72             continue;
    73         }
    74         Graham(n);
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    ADO.NET 数据连接查询
    弹出消息对话框类
    C# 火星文转化 算法 dictionary 的使用案例
    微软官方的SqlHelper
    jsHelper
    【VC++积累】之六、MFC简要剖析
    C# 统计文章中字符的种类和个数 哈希表和字典的使用
    C# NetHelper
    C# XML操作类 XmlHelper
    【VS++积累】之七、黑客编程之匿名管道
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3577254.html
Copyright © 2011-2022 走看看