/*
求出多边形最窄的地段长度
枚举边,求出所有点中到边的距离最大的值
这些值中最小的就是答案
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
const int MAXN = 109;
const double eps = 1e-4;
struct point{
double x,y;
}p[MAXN],h[MAXN];
inline double distance(const point &p1,const point &p2){
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}// 求两点之间的距离
inline double multiply(const point &sp,const point &ep,const point &op){
return ((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
}//判断sp,ep,op是否满足左转
int cmp(const void *a,const void *b){//按极角排序
point *p1 = (point *)a;
point *p2 = (point *)b;
double t = multiply(*p2,*p1,p[0]);
if(t>eps) return 1;
else if(fabs(t)<=eps)
{
if(distance(p[0],*p1)>distance(p[0],*p2)) return 1;
else return -1;
}
else return -1;
}
void anglesort(point p[],int n){//找到最左下方的点
int i,k=0;
point temp;
for(i=1;i<n;i++)
if(p[i].x<p[k].x ||( fabs(p[i].x-p[k].x)<eps && (p[i].y<p[k].y)))
k=i;
temp=p[0],p[0]=p[k],p[k]=temp;
qsort(p+1,n-1,sizeof(point),cmp);
}
void Graham_scan(point p[],point ch[],int n,int &len){//建立凸包
int i,top=2;
anglesort(p,n);
if(n<3){
for(i=0,len=n;i<n;i++) ch[i]=p[i];
return;
}
ch[0]=p[0],ch[1]=p[1],ch[2]=p[2];
p[n]=p[0];
for(i=3;i<n;i++){
while(multiply(p[i],ch[top],ch[top-1])>=eps) top--;
ch[++top]=p[i];
}
len=top+1;
}
double judge(point _x,point _y,int len)
{
double res=0;
for(int i=0;i<len;i++)
{
double tmp=fabs(multiply(h[i],_x,_y))/distance(_x,_y);//面积法求出距离
if(tmp>res)
res=tmp;
}
return res;
}
double solve(int len)
{
h[len]=h[0];
double ans=1<<30;
for(int i=0;i<len;i++)
{
double tmp=judge(h[i],h[i+1],len);//枚举边
if(tmp<ans)
ans=tmp;
}
return ans;
}
int main(){
int n,len;
int d=0;
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++) scanf("%lf %lf",&p[i].x,&p[i].y);
Graham_scan(p,h,n,len);
printf("Case %d: %.2lf\n",++d,solve(len)+0.005);
}
return 0;
}
hdu 3847 Trash Removal 2011WorldFinal 凸包
最新推荐文章于 2019-02-26 13:29:16 发布