HDU3847 Trash Removal(凸包)

来源:互联网 发布:哈萨克歌软件 编辑:程序博客网 时间:2024/06/05 00:25

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3847


Trash Removal

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 635    Accepted Submission(s): 221
Special Judge


Problem Description
Allied Chute Manufacturers is a company that builds trash chutes. A trash chute is a hollow tube installed in buildings so that trash dropped in at the top will fall down and be collected in the basement. Designing trash chutes is actually highly nontrivial. Depending on what kind of trash people are expected to drop into them, the trash chute needs to have an appropriate size. And since the cost of manufacturing a trash chute is proportional to its size, the company always would like to build a chute that is as small as possible. Choosing the right size can be tough though.

We will consider a 2-dimensional simplification of the chute design problem. A trash chute points straight down and has a constant width. Objects that will be dropped into the trash chute are modeled as polygons. Before an object is dropped into the chute it can be rotated so as to provide an optimal fit. Once dropped, it will travel on a straight path downwards and will not rotate in flight. The following figure shows how an object is first rotated so it fits into the trash chute.

Your task is to compute the smallest chute width that will allow a given polygon to pass through.
 

Input
The input contains several test cases. Each test case starts with a line containing an integer n (3 <= n <= 100), the number of points in the polygon that models the trash item.

The next n lines then contain pairs of integers xi and yi (0 <= xi, yi <= 10^4), giving the coordinates of the polygon vertices in order. All points in one test case are guaranteed to be mutually distinct and the polygon sides will never intersect. (Technically, there is one inevitable exception of two neighboring sides sharing their common vertex. Of course, this is not considered an intersection.)

The last test case is followed by a line containing a single zero.
 

Output
For each test case, display its case number followed by the width of the smallest trash chute through which it can be dropped. Display the minimum width with exactly two digits to the right of the decimal point, rounding up to the nearest multiple of 1/100. Answers within 1/100 of the correct rounded answer will be accepted.
 

Sample Input
30 03 00 440 1010 020 1010 200
 

Sample Output
Case 1: 2.40Case 2: 14.15
 


题意:有一个多边形的垃圾和一个通道,可以任意旋转多边形,问通道的直径最小需要多少才能让多边形通过。


多边形的形状是没有价值的,所以直接求出这些顶点的凸包。

然后对于凸包的每条边,求出每个点到这条边的最大距离,再求出这些最大距离中的最小距离。


最后注意输出格式,2位小数,向上进位。


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8struct point{       double x,y;       point(){}       point(double _x,double _y)       {                    x=_x;y=_y;       }       point operator - (const point &b) const       {             return point(x-b.x,y-b.y);       }       bool operator < (const point &b) const       {            return x<b.x||x==b.x&&y<b.y;       }       double len()       {              return sqrt(x*x+y*y);       }}res[105],p[105];int n;int dcmp(double x){    return (x>eps)-(x<-eps);}double dis(point a,point b){       return (a-b).len();}double cross(point a,point b){       return a.x*b.y-b.x*a.y;}double ptoline(point p,point a,point b)//点到直线距离{       return (fabs(cross(p-a,b-a))/dis(a,b));}int andrew()//求出凸包{    sort(p,p+n);    int m=0;    for (int i=0;i<n;i++)    {        while (m>1&&cross(res[m-1]-res[m-2],p[i]-res[m-2])<0) --m;        res[m++]=p[i];    }    int k=m;    for (int i=n-2;i>=0;--i)    {        while (m>k&&cross(res[m-1]-res[m-2],p[i]-res[m-2])<0) --m;        res[m++]=p[i];    }    if (m>1) --m;    return m;}int main(){    int ca=0;    while (scanf("%d",&n)!=EOF&&n!=0)    {          for (int i=0;i<n;i++)          {              scanf("%lf%lf",&p[i].x,&p[i].y);          }          int m=andrew();          res[m]=res[0];          double mi=INF;          for (int i=0;i<m;i++)          {              double ma=-1;              for (int j=0;j<n;j++)//对每条边求出最大距离              {                  double tmp=ptoline(p[j],res[i],res[i+1]);                  //if (dcmp(tmp)==0) continue;                  if (dcmp(tmp-ma)>0) ma=tmp;              }              if (dcmp(ma-mi)<0) mi=ma;//求出最大距离中的最小值          }          mi=ceil(mi*100)/100.0;//控制输出          printf("Case %d: %.2lf\n",++ca,mi);    }    return 0;}


0 0
原创粉丝点击