您的位置:首页 > 产品设计 > UI/UE

bzoj1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 计算几何 凸包

2015-02-25 11:39 309 查看
裸凸包,上个模板。
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 10000
struct node
{
double x,y;
}p[maxn];
int s[maxn],top,n;
double ans;
double sqr(double x)
{
return x*x;
}
double dis(node a,node b)
{
return sqr(a.x-b.x)+sqr(a.y-b.y);
}
inline node operator-(node a,node b)
{
node t;t.x=a.x-b.x;t.y=a.y-b.y;return t;
}
inline double operator*(node a,node b)
{
return a.x*b.y-a.y*b.x;
}
bool cmp(node a,node b)
{
double t=(a-p[1])*(b-p[1]);
if(t==0)return dis(p[1],a)<dis(p[1],b);
return t>0;
}
void graham()
{
int t=1;
for(int i=2;i<=n;i++)
if(p[i].y<p[t].y||(p[i].y==p[t].y&&p[i].x<p[t].x))t=i;
swap(p[1],p[t]);
sort(p+2,p+n+1,cmp);
top=2;
s[1]=1;s[2]=2;
for(int i=3;i<=n;i++)
{
for(;(p[s[top]]-p[s[top-1]])*(p[i]-p[s[top-1]])<=0;top--);
top++;s[top]=i;
}
s[top+1]=1;
for(int i=1;i<=top;i++)
ans+=sqrt(dis(p[s[i]],p[s[i+1]]));
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
graham();
printf("%.2lf\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: