hdu 1348(凸包)

Wall

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4903    Accepted Submission(s): 1419


Problem 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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
 

 

Sample Input
1 9 100 200 400 300 400 300 300 400 300 400 400 500 400 500 200 350 200 200 200
 

 

Sample Output
1628
 

 

Source
 
题意:国王要在城堡外修一个外墙,这一个外墙要隔城堡至少m英尺,求修这个外墙需要的最小费用
分析:我们先要找到刚好囊括整个城堡的一个外墙,这里要利用凸包算法,然后加上一个圆的周长,这里利用的是Graham算法。下面是算法流程:
令p0为Q中Y-X(not X-Y)坐标排序下最小的点
 设<p1,p2,...pm>为对其余点按以p0为中心的极角逆时针排序所得的点集(如果有多个点有相同的极角,除了距p0最远的点外全部移除
 压p0进栈S
 压p1进栈S
 压p2进栈S
  for i ← 3 to m
  do while 由S的栈顶元素的下一个元素、S的栈顶元素以及pi构成的折线段不拐向左侧
      对S弹栈
      压pi进栈S
  return S;
 
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 1005;
const double pi = atan(1.0)*4;
const double eps = 1e-8;
struct Point
{
    double x,y;
} p[N];

double cross(Point a,Point b,Point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
double dis(Point a,Point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int n,m;
Point Stack[N];
int cmp(Point a,Point b){
    if(cross(a,b,p[0])>0)
        return 1;
    if(cross(a,b,p[0])==0&&dis(b,p[0])-dis(a,p[0])>eps)
        return 1;
    return 0;
}
int Graham()
{
    int k = 0;
    for(int i=0; i<n; i++)
    {
        if(p[k].y>p[i].y||((p[k].y==p[i].y)&&(p[k].x>p[i].x))) k=i;
    }
    swap(p[0],p[k]);
    int top=2;
    sort(p+1,p+n,cmp);
    Stack[0]=p[0];
    Stack[1]=p[1];
    Stack[2]=p[2];
    for(int i=3; i<n; i++)
    {
        while(top>=1&&cross(p[i],Stack[top],Stack[top-1])>=0)
        {
            top--;
        }
        Stack[++top]=p[i];
    }
    return top;
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        double sum=0;
        int top = Graham();
        for(int i=1; i<=top; i++)
        {
            sum+=sqrt(dis(Stack[i],Stack[i-1]));
        }
        ///处理最后一个点和P0
        sum+=sqrt(dis(Stack[0],Stack[top]));
        ///加上圆
        sum+=2*pi*m;
        printf("%.0lf\n",sum);
        if(tcase) printf("\n");
    }
    return 0;
}

 

posted @ 2016-04-24 20:19  樱花庄的龙之介大人  阅读(477)  评论(0编辑  收藏  举报