您的位置:首页 > 其它

poj1113——Wall(凸包)

2017-02-26 16:16 323 查看
Description

Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King’s castle. The King was so greedy, that he would not listen to his Architect’s proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall.



Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King’s requirements.

The task is somewhat simplified by the fact, that the King’s castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle’s vertices in feet.

Input

The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King’s castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle.

Next N lines describe coordinates of castle’s vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

Output

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King’s requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

Sample Input

9 100

200 400

300 400

300 300

400 300

400 400

500 400

500 200

350 200

200 200

Sample Output

1628

Hint

结果四舍五入就可以了

要围绕城堡造一圈围墙,要求围墙至少要与城堡保持一定距离并且费用最少。例图就是一个示意图,城堡的顶点附近是圆周的一部分,所有顶点附近的圆周正好构成一个完整的圆,多画几个多边形就能明白。剩下的直线部分就是一个凸包的长度。

我曾纠结为什么图中凹下去的部分,围墙为什么不会跟着凹下去,仔细读题才发现要求费用最少,也就是说图中的方式是最经济的

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 50005
#define Mod 10001
using namespace std;
double PI=acos(-1.0);
struct point
{
int x,y;
};
point list[MAXN];
int stack[MAXN],top;
int cross(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(point p1,point p2)
{
return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(point p1,point p2)
{
int tmp=cross(list[0],p1,p2);
if(tmp>0)
return true;
else if(tmp==0&&dis(list[0],p1)<dis(list[0],p2))
return true;
else
return false;
}
void graham(int n)
{
if(n==1)
{
top=0;
stack[0]=0;
}
if(n==2)
{
top=1;
stack[0]=0;
stack[1]=1;
}
if(n>2)
{
for(int i=0;i<=1;++i)
stack[i]=i;
top=1;
for(int 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,l;
while(~scanf("%d%d",&n,&l))
{
point p0;
scanf("%d%d",&list[0].x,&list[0].y);
p0.x=list[0].x;
p0.y=list[0].y;
int k=0;
for(int i=1;i<n;++i)
{
scanf("%d%d",&list[i].x,&list[i].y);
if((p0.y>list[i].y)||((p0.y==list[i].y)&&(p0.x>list[i].x)))
{
p0.x=list[i].x;
p0.y=list[i].y;
k=i;
}
}
list[k]=list[0];
list[0]=p0;
sort(list+1,list+n,cmp);
graham(n);
double ans=0;
for(int i=0;i<top;++i)
ans+=dis(list[stack[i]],list[stack[i+1]]);
ans+=dis(list[stack[0]],list[stack[top]]);
ans+=2*PI*l;
printf("%d\n",(int)(ans+0.5));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: