题目不难,主要是转化,大致题意为:
国王要在多边形的城堡外建立一个围墙,要求围墙距离城堡的最小距离为L,给定城堡多边形的各个顶点的坐标,和L,要求用最少的长度建立此围墙。
题目读懂后,其实就是求凸包外围长度之和+半径为L的圆周长。与poj2187几乎一样的代码。
下面是程序: 216K+32MS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <iomanip>
#define Max 1010
#define Inf 1000000010
#define PI 3.141592654
using namespace std;
typedef struct Point{
int x;
int y;
}point;
point node[Max];
int n,L;
double Sum;
int det(int x1,int y1,int x2,int y2){
return x1*y2-x2*y1;
}
int cross(point A,point B,point C,point D){
return det(B.x-A.x,B.y-A.y,D.x-C.x,D.y-C.y);
}
int dis(point A,point B){
return (B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y);
}
/*int cmp(const void *s,const void *t){
point a=(*(point*)s);
point b=(*(point*)t);
int temp=cross(node[0],a,node[0],b);
if(temp>0)
return -1;
else if(temp==0)
return dis(node[0],a)-dis(node[0],b);
else
return 1;
}*/
bool cmp(const point s,const point t){
//point a=(*(point*)s);
//point b=(*(point*)t);
int temp=cross(node[0],s,node[0],t);
if(temp>0)
return true;
else if(temp==0)
return dis(node[0],s)<dis(node[0],t);//dis(node[0],a)-dis(node[0],b);
else
return false;
}
int main(){
scanf("%d%d",&n,&L);
int i,k,index=0;
node[0].x=Inf,node[0].y=Inf;
for(i=1;i<=n;i++){
scanf("%d%d",&node[i].x,&node[i].y);
if(node[i].x<node[index].x)
index=i;
else if(node[i].x==node[index].x){
if(node[i].y<node[index].y)
index=i;
}
}
node[0]=node[index];
//qsort(node+1,n,sizeof(point),cmp);
sort(node+1,node+1+n,cmp);
node[n+1]=node[0];
k=3;
for(i=4;i<=n+1;i++){
while(cross(node[k-1],node[k],node[k],node[i])<0)
k--;
node[++k]=node[i];
}
/*int max_dis=-Inf;
for(i=1;i<k;i++)
for(int j=i+1;j<k;j++){
int temp=dis(node[i],node[j]);
if(temp>max_dis)
max_dis=temp;
}
printf("%d\n",max_dis);*/
Sum=0.0;
for(i=1;i<k;i++)
Sum+=sqrt(double(dis(node[i],node[i+1])));
Sum+=2*PI*L;
//Sum+=0.5;
/*__int64 temp=Sum;
//printf("%I64d\n",__int64(Sum));
//printf("%I64d\n",temp);
printf("%I64d\n",temp));*/
cout << fixed << setprecision(0) << Sum << endl;
return 0;
}