滴水穿石,慢慢积累,总会有成果

随手记录,持之以恒

HDOJ 1392 Surround the Trees (凸包)

Surround the Trees

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


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



普通的凸包,只不过在n=2的时候有一个特判,因为不超过3的时候不存在首尾相连的情况,即不会存在一个封闭的图形,如果按照普通凸包来算就会将那一条线段重复计算,所以特判。。


ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define fab(a) (a)>0?(a):(-a)
#define LL long long
#define MAXN 55000
#define mem(x) memset(x,0,sizeof(x))
#define INF 0xfffffff 
#define PI acos(-1.0)
using namespace std;
struct s
{
	double x,y;
}list[MAXN];
int stack[MAXN],top;
double dis(s aa,s bb)
{
	return sqrt((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y));
}
int cross(s p0,s p1,s p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cmp(s aa,s bb)
{
	int num=cross(list[0],aa,bb);
	if(num>0) return true;
	else if(num==0&&dis(list[0],aa)<dis(list[0],bb)) return true;
	else return false;
}
void graham(int n)
{
	int i;
	if(n==1) {top=0;stack[0]=0;}
	if(n==2) {top=1;stack[0]=0;stack[1]=1;}
	if(n>2)
	{
		for(i=0;i<2;i++)
		stack[i]=i;
		top=1;
		for(i=2;i<n;i++)
        {
            while(top>0&&cross(list[stack[top-1]],list[stack[top]],list[i])<=0) top--;
            top++;
            stack[top]=i;
        }  
	}
}
int main()
{
	int n,i;
	while(scanf("%d",&n)!=EOF,n)
	{
		
		s low;
		scanf("%lf%lf",&list[0].x,&list[0].y);
		low.x=list[0].x,low.y=list[0].y;
		int k=0;
		for(i=1;i<n;i++)
		{
			scanf("%lf%lf",&list[i].x,&list[i].y);
			if(((low.y==list[i].y)&&(low.x>list[i].x))||low.y>list[i].y)
			{
				low.x=list[i].x;
				low.y=list[i].y;
				k=i;
			}
		}
		if(n==2)
		{
			printf("%.2lf\n",dis(list[0],list[1]));
			continue;
		}
		list[k]=list[0];
		list[0]=low;
		sort(list+1,list+n,cmp);
		graham(n);
		double ans=0;
		for(i=0;i<top;i++)
		{
			ans+=dis(list[stack[i]],list[stack[i+1]]);
		}
		ans+=dis(list[stack[0]],list[stack[top]]);
		//printf("%lf\n",ans);
		printf("%.2lf\n",ans);
	}
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/helloiamclh/article/details/49932969
个人分类: 凸包
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

HDOJ 1392 Surround the Trees (凸包)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭