poj 1113(凸包)

来源:互联网 发布:三星支持网络打印 编辑:程序博客网 时间:2024/05/15 02:17

#include<cstdio>
#include<cmath>
#include <algorithm>
using namespace std;
struct point
{
 double x, y;
};

bool mult(point sp, point ep, point op){
 return (sp.x-op.x) * (ep.y-op.y)
  >=(ep.x-op.x) * (sp.y-op.y);
}
bool operator<(const point &l, const point &r){
 return l.y<r.y || (l.y==r.y && l.x<r.x);
}
int graham(point pnt[], int n, point res[]){
 int i,len,k=0,top=1;
 sort(pnt,pnt+n);
 if(n==0)
  return 0;
 res[0]=pnt[0];
 if(n==1)
  return 1;
 res[1]=pnt[1];
 if(n==2)
  return 2;
 res[2]=pnt[2];
 for(i=2;i<n;i++)
 {
  while(top && mult(pnt[i],res[top],res[top-1]))
   top--;
  res[++top]=pnt[i];
 }
 len=top;
 res[++top]=pnt[n-2];
 
 for(i=n-3;i>=0;i--)
 {
  while(top!=len && mult(pnt[i],res[top],res[top-1]))
   top--;
  res[++top]=pnt[i];
 }
 return top;
}
int main()
{
 int n,l,top,i;
 struct point pnt[1010],res[1010];
 double ans;
 scanf("%d%d",&n,&l);
 
 for(i=0;i<=n-1;i++)
 {
        scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
    }
 
    top=graham(pnt,n,res);
    ans=0;
    for(i=0;i<=top-2;i++)
 {
  ans+=sqrt((res[i].x-res[i+1].x)*(res[i].x-res[i+1].x)+(res[i].y-res[i+1].y)*(res[i].y-res[i+1].y));
    }
    ans+=sqrt((res[i].x-res[0].x)*(res[i].x-res[0].x)+(res[i].y-res[0].y)*(res[i].y-res[0].y));
 ans+=2*3.141592653*l;
    printf("%.0f\n",ans);
 return 0;
}


 

原创粉丝点击