HDU1392 Surround the Trees(凸包)

来源:互联网 发布:客户关系维护软件 编辑:程序博客网 时间:2024/06/07 01:49

Surround the Trees

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


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
Asia 1997, Shanghai (Mainland China)
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1115 1086 2108 2150 1348 
 



题意:求最短多长的绳子可以把所有树围起来

思路:求凸包周长

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<cstring>#include<string>#include<algorithm>using namespace std;#define ll long long#define ms(a,b)memset(a,b,sizeof(a))#define eps 1e-10#define inf 1e8double a[4][4] = { {0,0,0,0},{0,-1,0,0},{0,0,0,-1},{0,-1,0,-1} } ;int n;double add(double a,double b){    if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;    return a+b;}struct P{    double x,y;    int number;    P(){}    P(double x,double y): x(x),y(y){}    P operator + (P p)    {        return P(add(x,p.x),add(y,p.y));    }    P operator - (P p)    {        return P(add(x,-p.x),add(y,-p.y));    }    P operator *(double d)    {        return P(x*d,y*d);    }    double dot (P p)    {        return add(x*p.x,y*p.y);    }    double det(P p)    {        return add(x*p.y,-y*p.x);    }}ps[2000];bool on_seg(P p1,P p2,P q){    return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;}P intersection(P p1,P p2,P q1,P q2){    return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));}bool cmp_x(const P& p,const P & q){    if(p.x!=q.x) return p.x<q.x;    return p.y<q.y;}vector<P> convex_hull(P *pos,int n){    sort(ps,ps+n,cmp_x);    int k=0;    vector<P> qs(n*2);    for(int i=0;i<n;i++)    {        while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;        qs[k++]=ps[i];    }    for(int i=n-2,t=k;i>=0;i--)    {        while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;        qs[k++]=ps[i];    }    qs.resize(k-1);    return qs;}double dist(P p,P q){    return sqrt((p-q).dot(p-q));}int main(){    int n;    while(~scanf("%d",&n))    {        if(n==0) break;        for(int i=0;i<n;i++)            scanf("%lf%lf",&ps[i].x,&ps[i].y);        double res=0.0;        if(n==2)        {            printf("%.2lf\n",dist(ps[0],ps[1]));            continue;        }        vector<P> qs=convex_hull(ps,n);        int qs_size=qs.size();        for(int i=0;i<qs.size();i++)        {            res+=dist(qs[i],qs[(i+1)%qs_size]);        }        printf("%.2lf\n",res);    }    return 0 ;}


原创粉丝点击