POJ 3608 Bridge Across Islands (凸包+旋转卡壳)

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.


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


Solution

引用ACMaker博客的部分内容:http://blog.csdn.net/acmaker?viewmode=list

这里写图片描述

这里写图片描述


然后就是凸包(题目已保证是凸多边形了)和旋转卡壳*的应用了。代码有详细解释。


Code(AC)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#define Eps 1e-10
#define N 10010

using namespace std;

int n, m;
struct Data{
    double x, y;
    Data() {}
    Data(double _x, double _y):x(_x), y(_y) {}
    friend Data operator + (Data A, Data B){return Data(A.x + B.x, A.y + B.y);}
    friend Data operator - (Data A, Data B){return Data(A.x - B.x, A.y - B.y);}
    friend Data operator * (double A, Data B){return Data(A * B.x, A * B.y);}
    friend Data operator / (Data A, double B){return Data(A.x / B, A.y / B);}
}c1[N], c2[N];
double dist;
double Dot(Data A, Data B){return A.x * B.x + A.y * B.y;}
double Det(Data A, Data B){return A.x * B.y - A.y * B.x;}

double Getdist(Data A, Data B){
    return sqrt(Dot(B - A, B - A));
}

//计算P点到线段AB的最短距离
double pldist(Data P, Data A, Data B){
    if(Getdist(A, B) < Eps)  return Getdist(P, A);//特判重点情况,否则会除以0!!(虽说去掉也能过)
    if(Dot(B - A, P - A) < -Eps)  return Getdist(P, A);//点对点 
    if(Dot(A - B, P - B) < -Eps)  return Getdist(P, B);//d点对点
    else  return fabs(Det(P - A, P - B)) / Getdist(A, B);//点对边(相对位置不一定哦)
//万一AB距离非常短将出错!(甚至重点) 
//  因为是线段,所以不一定点对边最短 
}

//求一条线段的两端点到另外一条线段的距离,反过来一样,共4种情况
double lldist(Data A, Data B, Data C, Data D){
    return min(min(pldist(A, C, D), pldist(B, C, D)), min(pldist(C, A, B), pldist(D, A, B)));
}

void Sol(){
    dist = 0x7fffffff;
    int u = 1, d = 1, cmp;
    c1[n+1] = c1[1];
    c2[m+1] = c2[1];

    for(int i = 1; i <= n; i++)  if(c1[i].y < c1[d].y)  d = i;
    for(int i = 1; i <= m; i++)  if(c2[i].y > c2[u].y)  u = i;
    for(int i = 1; i <= n; i++){
      while(cmp = Det(c2[u+1]-c1[d+1], c1[d]-c1[d+1]) - Det(c2[u]-c1[d+1], c1[d]-c1[d+1]) > Eps)  u = u % m + 1; //注意写法 
      //找对踵点对,注意是顺时针,与逆时针写法相反 
      if(cmp < -Eps)  dist = min(dist, pldist(c2[u], c1[d], c1[d+1]));非平行线段情况,求点到线段的最短距离 
      else  dist = min(dist, lldist(c1[d], c1[d+1], c2[u], c2[u+1]));
      //两平行线段间的距离 ,特判平行情况!!(对蹱点对相当于4对)仍然判断4次点到线段距离 
      d = d % n + 1;
    } 
}


int main(){

    while(~ scanf("%d%d", &n, &m) && (n || m)){
      for(int i = 1; i <= n; i++)  scanf("%lf%lf", &c1[i].x, &c1[i].y);
      for(int i = 1; i <= m; i++)  scanf("%lf%lf", &c2[i].x ,&c2[i].y);
      //不用求凸包直接做
      Sol();//只用做一遍 ,两次卡壳对蹱点对一样。。 
      printf("%.5lf\n", dist);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值