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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值