poj 1113 wall 简单的凸包

来源:互联网 发布:linux c sleep 头文件 编辑:程序博客网 时间:2024/05/19 11:45

 

                                                                                                                                                  Wall
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 23448 Accepted: 7746

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 100200 400300 400300 300400 300400 400500 400500 200350 200200 200

Sample Output

1628

Hint

结果四舍五入就可以了
 
解题
算法:  这道题就是求包含所有点的凸包的长度,加上圆的周长,这样才能距离每个点的位置是定值;  用卷包裹法求凸包
下面贴上我的解题代码:
 
自己敲了一边模板
重点注意在自己定义的cmp函数中应当注意用1e-6来判断两数相减的大小。会有误差。
 
#include<stdio.h>#include<iostream>#include<algorithm>#include<math.h>using namespace std;struct node{            double  x,y,alps,dis;           } point[1100];const double pi=3.14159265358;  //确定pi的值          double dis( node a , node b )        //求距离的函数{    return sqrt( ( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) ) );          }bool cmp( node a , node b )         //排序先按角度排序,再按距离排序{              if(a.alps-b.alps<1e-6 && a.alps-b.alps>-1e-6)return a.dis-b.dis<0; //注意这里,这里改成了1e-6才通过 wa了好多次         return a.alps-b.alps<0;   }bool  cross( node a, node b, node c )  //已知三个点求叉积{        int x1,y1,x2,y2;        x1=b.x-a.x;        y1=b.y-a.y;        x2=c.x-b.x;        y2=c.y-b.y;    if(x1*y2-x2*y1>=0)return false;    else return true;            }int s[1100]; int main(){    int n,i;    double length;    while(scanf("%d %lf",&n,&length)!=EOF)    {          int j=1;          for( i=1 ; i<=n ; i++ )              {                 scanf("%lf %lf",&point[i].x,&point[i].y);                 if( ( point[j].x>point[i].x )|| ( (point[j].x==point[i].x)&& (point[j].y>point[i].x) ) ) //求出最左边的点                       j=i;              }          point[0]=point[j];          point[j]=point[n];                        for( i=1 ; i<n  ; i++ )              {                 point[i].dis=dis( point[0] , point[i] );  //求出每个点的距离                 point[i].alps= ( point[i].y-point[0].y )/point[i].dis; //求出每个点的sin角度的值              }          sort(point+1,point+n,cmp);//按sin角度的值从小到大(以第一顺位)按dis以第二顺位排序也是从小到大                              s[1]=0; //排好序的0,1点一定是在凸包上的          s[2]=1;          int m=2;          for(i=2;i<n;i++)  // 求凸包的上的点的坐标,用s数组储存是第几个点在凸包上          {              while(cross( point[s[m-1]] ,point[s[m]] ,point[i]) ) m--;              s[++m]=i;             }                              double ans=0;          ans+=dis(point[s[1]] , point[s[m]]); //千万别忘了第一个点和最后一个点也要算距离的          for(i=2;i<=m;i++)              ans+=dis(point[ s[i-1]],point[s[i]] ); //加上中间所有点的距离  现在ans是凸包的周长            double s;          s=pi*2*length;      //加上圆的周长          printf("%d\n",int(ans+s+0.5 ));        }    }

 
 
原创粉丝点击