poj 3608 Bridge Across Islands(卡壳旋转求两个多边形的最近点对)

Bridge Across Islands
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7672 Accepted: 2275 Special Judge

Description

Thousands of thousands years ago there was a small kingdom located in the middle of the Pacific Ocean. The territory of the kingdom consists two separated islands. Due to the impact of the ocean current, the shapes of both the islands became convex polygons. The king of the kingdom wanted to establish a bridge to connect the two islands. To minimize the cost, the king asked you, the bishop, to find the minimal distance between the boundaries of the two islands.

Input

The input consists of several test cases.
Each test case begins with two integers NM. (3 ≤ NM ≤ 10000)
Each of the next N lines contains a pair of coordinates, which describes the position of a vertex in one convex polygon.
Each of the next M lines contains a pair of coordinates, which describes the position of a vertex in the other convex polygon.
A line with N = M = 0 indicates the end of input.
The coordinates are within the range [-10000, 10000].

Output

For each test case output the minimal distance. An error within 0.001 is acceptable.

Sample Input

4 4
0.00000 0.00000
0.00000 1.00000
1.00000 1.00000
1.00000 0.00000
2.00000 0.00000
2.00000 1.00000
3.00000 1.00000
3.00000 0.00000
0 0

Sample Output

1.00000

Source


题意:求2个不相交的凸多边形的最近距离,题目已经按顺序给出凸多边形

题解:用卡壳旋转来求最近点对,枚举一个多边形的边到另一个多边形的点最短距离

在处理点的单峰函数时候,有2种情况:

1. 该点是最近另一个多边形被枚举的边的点,就直接求点到直线的距离,注意点判断点到直线的垂线的垂足是否在直线上,我是用内积判断的

2.该点和下一个点形成的直线平行于被枚举的边,那么别枚举4个点到对应的边的距离,就是调用4次情况1的函数就可以了


#include<cstdio>
#include<cmath>
#include<algorithm>
#define eps 1e-8
using namespace std;
struct point{
    double x,y;
    point(){}
    point(double x_,double y_):x(x_),y(y_){}
}np[10008],mp[10008];
int n,m;
double MIN(double x,double y){ return x<y?x:y; }
double cross(struct point p1,struct point p2,struct point p3)
{
    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
double dot(struct point p1,struct point p2)
{
    return p1.x*p2.x+p1.y*p2.y;
}
double dis(struct point p1,struct point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
void make_clockwise()
{
    double area=0;

    for(int i=2;i<=n;i++) area+=cross(np[1],np[i],np[i+1]);
    if(area>0) reverse(np,np+n+2);
    area=0;
    for(int i=2;i<=m;i++) area+=cross(mp[1],mp[i],mp[i+1]);
    if(area>0) reverse(mp,mp+m+2);
}
double point_to_segment(struct point p1,struct point p2,struct point p3)
{
    if(dot(point(p1.x-p3.x,p1.y-p3.y),point(p2.x-p3.x,p2.y-p3.y))<0||
       dot(point(p1.x-p2.x,p1.y-p2.y),point(p3.x-p2.x,p3.y-p2.y))<0)
        return MIN(dis(p1,p2),dis(p1,p3));

    double a=p2.y-p3.y;
    double b=p3.x-p2.x;
    double c=p2.x*p3.y-p2.y*p3.x;
    double d=fabs(a*p1.x+b*p1.y+c)/sqrt(a*a+b*b);

    return d;
}
double segment_to_segment(struct point p1,struct point p2,struct point p3,struct point p4)
{
    return MIN(MIN(point_to_segment(p1,p3,p4),point_to_segment(p2,p3,p4)),
               MIN(point_to_segment(p3,p1,p2),point_to_segment(p4,p1,p2)));
}
double solve(struct point *np,struct point *mp,int n,int m)
{
    double ymin,ymax,res,temp;
    int i,idn=1,idm=1;

    for(ymin=np[1].y,i=2;i<=n;i++)
        if(np[i].y<ymin) ymin=np[idn=i].y;
    for(ymax=mp[1].y,i=2;i<=m;i++)
        if(mp[i].y>ymax) ymax=mp[idm=i].y;
    for(res=0xffffff,i=1;i<=n;i++)
    {
        while((temp=cross(np[idn],np[idn+1],mp[idm])-cross(np[idn],np[idn+1],mp[idm+1]))>eps)
            idm=(idm+1)%m;
        if(temp<-eps) res=MIN(res,point_to_segment(mp[idm],np[idn],np[idn+1]));
        else res=MIN(res,segment_to_segment(mp[idm],mp[idm+1],np[idn],np[idn+1]));
        idn=(idn+1)%n;
    }

    return res;
}
int main()
{
    //freopen("t.txt","r",stdin);
    while(scanf("%d%d",&n,&m),n!=0&&m!=0)
    {
        for(int i=1;i<=n;i++) scanf("%lf%lf",&np[i].x,&np[i].y);
        for(int i=1;i<=m;i++) scanf("%lf%lf",&mp[i].x,&mp[i].y);
        make_clockwise();
        np[0]=np[n],np[n+1]=np[1];
        mp[0]=mp[m],mp[m+1]=mp[1];
        printf("%lf\n",MIN(solve(np,mp,n,m),solve(mp,np,m,n)));
    }

    return 0;
}


3 3
0 0
0 1
1 0
1.5 1.5
0 3
3 3
1.41421
3 3
0 0
0 0.5
1 0
1.5 1.5
0 3
3 3
1.56525
3 3
0 0
0 0.5
1 1
1.5 1.5
0 3
3 3
0.70711
3 3
0 0
0 0.5
1 1
1.5 1.5
0 3
3 5
0.70711
3 3
0 0
0 0.5
1 1
1.5 1.2
2 3
4 5
0.53852


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值