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): 8414    Accepted Submission(s): 3222


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
题目分析:
因为要将所有树都围起来绳的最短长度,可以假设出现凹包,情况,那么将凹角出现的点去掉,直接相连另两个点,因为三角形两边之和大于第三边,所以围成凸包的周长就是最小的绳的长度
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define MAX 107

using namespace std;

int n;

struct Node
{
    int x,y;
}e[MAX],res[MAX];

int cmp ( Node a , Node b )
{
    if ( a.x == b.x ) return a.y < b.y;
    return a.x < b.x;
}

int cross ( Node a , Node b , Node c )
{
    return (a.x-c.x)*(b.y-c.y) - (b.x-c.x)*(a.y-c.y);
}

int convex ( int n )
{
    sort ( e , e+n , cmp );
    int m = 0 , i , j , k;
    for ( int i = 0 ; i < n ; i++ )
    {
        while ( m > 1 && cross ( res[m-1] , e[i] , res[m-2] ) <=0 ) m--;
        res[m++] = e[i];
    }
    k = m;
    for ( int i = n-2 ; i >= 0 ; i-- )
    {
        while ( m > k && cross ( res[m-1] , e[i] , res[m-2] ) <= 0 ) m--;
        res[m++] = e[i];
    }
    if ( n > 1 ) m--;
    return m;
}

double dis ( Node p1 , Node p2 )
{
    return sqrt (pow ( (p1.x - p2.x)*1.0 , 2 ) + pow ( (p1.y-p2.y)*1.0 , 2 ) );
}

double perimeter ( int n )
{
    double ret = 0;
    if ( n < 2 ) return ret;
    if ( n == 2 ) return dis ( res[0] , res[1] );
    Node p1 = res[0],p2,p3 = res[0];
    for ( int i = 1 ; i < n ; i++ )
    {
        p2 = res[i];
        ret += dis ( p1 , p2 );
        p1 = p2;  
    }
    ret += dis ( p1 , p3 );
    return ret;
}

int main ( )
{
    while ( ~scanf ( "%d" , &n ) , n )
    {
        for ( int i = 0 ; i < n ; i++ )
            scanf ( "%d%d" , &e[i].x , &e[i].y );
        printf ( "%.2lf\n" , perimeter ( convex ( n ) ) );
    } 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值