Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10208 Accepted Submission(s): 3941
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.
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.
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
题目大意:不超过100棵树 求将这些树全包围的最小绳子的长度。
求凸包计算周长 排序的时候借鉴的百度百科。。。
然后补充一点 叉积
若 a x b > 0表示a在b的顺时针方向上
若 a x b < 0表示a在b的逆时针方向上
若 a x b == 0表示a在b共线,但不确定方向是否相同
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<algorithm>
#define maxn 110
using namespace std;
double x[maxn],y[maxn];
int n,s[maxn];
struct Node
{
double x,y;
}node[maxn];
double mul(Node a,Node b,Node c)
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
double dis(Node a,Node b)
{
return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
bool cmp(Node a,Node b)
{
double tem=mul(node[0],a,b);
if(tem>0||(tem==0&&dis(node[0],a)<dis(node[0],b)))
return true;
return false;
}
void solve()
{
int top;
if(n==1)
{
top=0;
s[0]=0;
}
else if(n==2)
{
s[0]=0;
s[1]=1;
top=1;
}
else
{
for(int i=0;i<=1;++i)
s[i]=i;
top=1;
for(int i = 2; i <=n; i++)
{
while(top > 0 && (mul(node[s[top]],node[s[top-1]],node[i])) >= 0)
top--;
top++;
s[top] = i;
}
}
double ans=0;
while(top>=0)
{
int a=-1,b=-1;
a=s[top];
top--;
if(top>=0)
b=s[top];
if(a>=0&&b>=0)
ans+=dis(node[a],node[b]);
}
printf("%.2lf\n",ans);
}
int main()
{
while(scanf("%d",&n),n)
{
for(int i=0;i<n;++i)
scanf("%lf%lf",&node[i].x,&node[i].y);
int p=0;
for(int i=1;i<n;++i)
{
if(node[i].x<node[p].x||(node[i].x==node[p].x&&node[i].y<node[p].y))
p=i;
}
Node tem=node[0];
node[0]=node[p];
node[p]=tem;
node[n]=node[0];
sort(node+1,node+n,cmp);
// for(int i=0;i<=n;++i)
// {
// printf("%lf %lf\n",node[i].x,node[i].y);
// }
solve();
}
return 0;
}