poj3608 Bridge Across Islands

125 篇文章 0 订阅

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 N, M. (3 ≤ N, M ≤ 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.

旋转卡壳,最短距离可能是点到点、点到边。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps=1e-10,pi=acos(-1),oo=1e15;
int cmp(double x)
{
    if (x>eps) return 1;
    if (fabs(x)<eps) return 0;
    return -1;
}
struct vector
{
    double x,y;
    void rd()
    {
        scanf("%lf%lf",&x,&y);
    }
    bool operator < (const vector &v) const
    {
        return cmp(y-v.y)==-1||(cmp(y-v.y)==0&&cmp(x-v.x)==-1);
    }
    vector operator + (const vector &v) const
    {
        return (vector){x+v.x,y+v.y};
    }
    vector operator - (const vector &v) const
    {
        return (vector){x-v.x,y-v.y};
    }
    vector operator * (const double &k) const
    {
        return (vector){x*k,y*k};
    }
    vector operator / (const double &k) const
    {
        return (vector){x/k,y/k};
    }
}a[10010],b[10010],t[10010];
typedef vector point;
double angle(vector v)
{
    double ret=atan2(v.y,v.x);
    if (cmp(ret)==-1) ret+=2*pi;
    return ret;
}
double dot(vector v1,vector v2)
{
    return v1.x*v2.x+v1.y*v2.y;
}
double cross(vector v1,vector v2)
{
    return v1.x*v2.y-v2.x*v1.y;
}
double len(vector v)
{
    return sqrt(dot(v,v));
}
double dis(point p1,point p2)
{
    return len(p2-p1);
}
struct line
{
    point p;
    vector v;
};
double dis(point p,line l)
{
    return fabs(cross(p-l.p,l.v))/len(l.v);
}
struct seg
{
    point a,b;
};
double dis(point p,seg s)
{
    if (cmp(dot(p-s.a,s.b-s.a))==1&&cmp(dot(p-s.b,s.a-s.b))==1)
        return dis(p,(line){s.a,s.b-s.a});
    return min(dis(p,s.a),dis(p,s.b));
}
void solve(point *f,int n)
{
    int m=0,mm;
    for (int i=1;i<=n;i++)
    {
        while (m>=2&&cmp(cross(f[m-1]-f[m-2],t[i]-f[m-2]))==-1) m--;
        f[m++]=t[i];
    }
    mm=m;
    for (int i=n-1;i;i--)
    {
        while (m>mm&&cmp(cross(f[m-1]-f[m-2],t[i]-f[m-2]))==-1) m--;
        f[m++]=t[i];
    }
}
int n,m;
double ans;
void init()
{
    for (int i=1;i<=n;i++) t[i].rd();
    solve(a,n);
    for (int i=1;i<=m;i++) t[i].rd();
    solve(b,m);
}
void renew(double x)
{
    ans=min(ans,x);
}
void solve()
{
    int top,ci,cj,cnti,cntj;
    double x,y,x1,y1;
    for (int i=0;i<n;i++)
        if (cmp(a[(i+1)%n].y-a[i].y)==-1)
        {
            top=i;
            break;
        }
    x=pi,y=0;
    cnti=cntj=0;
    ans=oo;
    renew(dis(a[top],b[0]));
    for (int i=top,j=0;cnti<=n||cntj<=m;)
    {
        x1=angle(a[(i+1)%n]-a[i])-x;
        if (cmp(x1)==-1) x1+=2*pi;
        y1=angle(b[(j+1)%m]-b[j])-y;
        if (cmp(y1)==-1) y1+=2*pi;
        x+=min(x1,y1);
        if (cmp(x-2*pi)>=0) x-=2*pi;
        y+=min(x1,y1);
        if (cmp(y-2*pi)>=0) y-=2*pi;
        ci=cmp(x-angle(a[(i+1)%n]-a[i]));
        cj=cmp(y-angle(b[(j+1)%m]-b[j]));
        if (!ci)
        {
            renew(dis(a[(i+1)%n],b[j]));
            renew(dis(b[j],(seg){a[i],a[(i+1)%n]}));
        }
        if (!cj)
        {
            renew(dis(a[i],b[(j+1)%m]));
            renew(dis(a[i],(seg){b[j],b[(j+1)%m]}));
        }
        if (!ci&&!cj)
        {
            renew(dis(a[(i+1)%n],b[(j+1)%m]));
            renew(dis(a[(i+1)%n],(seg){b[j],b[(j+1)%m]}));
            renew(dis(b[(j+1)%m],(seg){a[i],a[(i+1)%n]}));
        }
        if (!ci) i=(i+1)%n,cnti++;
        if (!cj) j=(j+1)%m,cntj++;
    }
    printf("%.5f\n",ans);
}
int main()
{
    while (scanf("%d%d",&n,&m)&&n)
    {
        init();
        solve();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值