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

HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】

时间:2016-05-12 15:32:50      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

Surround the Trees

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


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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct ss
{
    double x, y;
}po[105], s[105];
double xmulti(ss a, ss b, ss c)//向量叉积
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
double dis(ss a, ss b)//求距离
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp1(ss a, ss b)
{
    if(a.y == b.y) return a.x < b.x;
    else return a.y < b.y;
}
bool cmp(ss a, ss b)
{
    if(xmulti(po[0], a, b) > 0) return true;
    else if(xmulti(po[0], a, b) == 0)
    {
        if(dis(po[0], a) <= dis(po[0], b)) return true;
        else return false;
    }
    else return false;
}
int N, k;
double ans;
int main()
{
    while(scanf("%d", &N) != EOF && N)
    {
        k = 1;
        ans = 0;
        for(int i = 0;i < N; ++i)
        {
            scanf("%lf %lf", &po[i].x, &po[i].y);
        }
        if(N == 1)
        {
            printf("0.00\n");
            continue;
        }
        else if(N == 2)
        {
            printf("%.2f\n",  dis(po[0], po[1]));
            continue;
        }
        sort(po, po+N, cmp1);//找到最左下角的点
        sort(po+1, po+N, cmp);//按与x轴正方向的夹角排序
        memset(s, 0, sizeof(s));
        s[0] = po[0];
        s[1] = po[1];
        for(int i =  2;i < N; ++i)//做凸包
        {
            while(xmulti(s[k-1], s[k], po[i]) < 0 && k > 0) k--;
            s[++k] = po[i];
        }
        for(int i = 1;i <= k; ++i)//求距离和
        {
            ans += dis(s[i], s[i-1]);
        }
        ans += dis(s[0], s[k]);
        printf("%.2f\n", ans);
    }
    return 0;
}




HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】

标签:

原文地址:http://blog.csdn.net/a995549572/article/details/51365864

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