Bridge Across Islands
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11539   Accepted: 3395   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

 
图示:

思路:找到凸包 p 的 y 值最小点 yminP 和 q 的 y 值最大点ymaxQ,然后分别做切如图。那么AC×AD>AC×AB则说明B还不是离AC最近的点,所以++ymaxQ。

否则用 AC和 BD 两个线段的距离更新最近距离,并且++yminP,即考察P的下一条边。

具体实现时,同时旋转体现为逐步选取逆时针方向上的下一个顶点作为C或D、A或B,其实选择卡壳就是只要找到"当前向量面积不小于下一个向量面积"即可,再求两条线段间的最短距离。

代码:

 //#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i,n) for(int i=0;i<n;i++)
const int n = 1e4 + ;
const int mod = 1e9 + ;
const int mOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
const int inf=0x3f3f3f3f;
using namespace std;
struct P
{
db x, y;
P() {}
P(db x, db y) : x(x), y(y) {}
P operator + (const P& p){ return P(x + p.x, y + p.y); }
P operator - (const P& p){ return P(x - p.x, y - p.y); }
P operator * (const db& d){ return P(x * d, y * d); }
bool operator < (const P& a) const
{
if (x != a.x) return x < a.x;
else return y < a.y;
}
db dot(const P& p) { return x * p.x + y * p.y; }
db det(const P& p) { return x * p.y - y * p.x; }
};
P p[n], q[n];
// 向量AB 与 AC 的叉积 如果叉积大于0,那么C在向量AB的逆时针方向,叉积小于0则在AB的顺时针方向。如果叉积等于0,则ABC共线。
db cross(P A, P B, P C) {return (B - A).det(C - A); }
// 向量AB 与 AC 的点积 如果点积的结果为0,那么这两个向量互相垂直
db mul(P A, P B, P C) {return (B - A).dot(C - A); }
// 两点距离
db dis(P A, P B){return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); } // 逆时针排序
void csort(P* p, int n)
{
for (int i = ; i < n - ; ++i)
{
db tmp = cross(p[i], p[i + ], p[i + ]);
if (tmp > eps) return;
else if (tmp < -eps)
{
reverse(p, p + n);
return;
}
}
} //计算C点到线段AB的最短距离
db ptl(P A, P B, P C)
{
if (dis(A, B) < eps) return dis(B, C);
if (mul(A, B, C) < -eps) return dis(A, C);
if (mul(B, A, C) < -eps) return dis(B, C);
return fabs(cross(A, B, C) / dis(A, B));
}
//求一条线段的两端点到另外一条线段的距离,反过来一样,共4种情况
db ltl(P A, P B, P C, P D)
{
return min(min(ptl(A, B, C), ptl(A, B, D)), min(ptl(C, D, A), ptl(C, D, B)));
}
db solve(P* p, P* q, int n, int m)
{
int pmi = , qmx = ;
for (int i = ; i < n; ++i) if (p[i].y < p[pmi].y) pmi = i; // P上y坐标最小的顶点
for (int i = ; i < m; ++i) if (q[i].y > q[qmx].y) qmx = i; // Q上y坐标最大的顶点
p[n] = p[]; // 为了方便,避免求余
q[m] = q[];
db tmp, ans = inf;
for (int i = ; i < n; ++i)
{
while (tmp = cross(p[pmi],p[pmi + ], q[qmx + ]) - cross(p[pmi],p[pmi + ], q[qmx]) > eps) qmx = (qmx + ) % m;
ans = min(ans, ltl(p[pmi], p[pmi + ], q[qmx], q[qmx + ]));
pmi = (pmi + ) % n;
}
return ans;
}
int main()
{
int n, m;
while (~scanf("%d%d", &n, &m) && n + m)
{
for (int i = ; i < n; ++i) cd(p[i].x),cd(p[i].y);
for (int i = ; i < m; ++i) cd(q[i].x),cd(q[i].y);
csort(p, n);
csort(q, m);
printf("%.5lf\n", solve(p, q, n, m));
}
return ;
}

POJ 3608 凸包间最短距离(旋转卡壳)的更多相关文章

  1. poj 3608 凸包间的最小距离

    Bridge Across Islands Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7632   Accepted:  ...

  2. POJ 3608 Bridge Across Islands [旋转卡壳]

    Bridge Across Islands Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10455   Accepted: ...

  3. POJ 2187 Beauty Contest【旋转卡壳求凸包直径】

    链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  4. 【POJ 2187】Beauty Contest(凸包直径、旋转卡壳)

    给定点集的最远两点的距离. 先用graham求凸包.旋(xuán)转(zhuàn)卡(qiǎ)壳(ké)求凸包直径. ps:旋转卡壳算法的典型运用 http://blog.csdn.net/hanch ...

  5. [Bzoj1069][Scoi2007]最大土地面积(凸包)(旋转卡壳)

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 3629  Solved: 1432[Submit][Sta ...

  6. [模板] 计算几何2: 自适应Simpson/凸包/半平面交/旋转卡壳/闵可夫斯基和

    一些基本的定义在这里: [模板] 计算几何1(基础): 点/向量/线/圆/多边形/其他运算 自适应Simpson Simpson's Rule: \[ \int ^b_a f(x)dx\approx ...

  7. POJ - 2079:Triangle (旋转卡壳,求最大三角形)

    Given n distinct points on a plane, your task is to find the triangle that have the maximum area, wh ...

  8. POJ - 3608 Bridge Across Islands【旋转卡壳】及一些有趣现象

    给两个凸包,求这两个凸包间最短距离 旋转卡壳的基础题 因为是初学旋转卡壳,所以找了别人的代码进行观摩..然而发现很有意思的现象 比如说这个代码(只截取了关键部分) double solve(Point ...

  9. poj 2187 凸包加旋转卡壳算法

    题目链接:http://poj.org/problem?id=2187 旋转卡壳算法:http://www.cppblog.com/staryjy/archive/2009/11/19/101412. ...

随机推荐

  1. HihoCoder#1513 : 小Hi的烦恼(五维数点 bitset 分块)

    题意 题目链接 Sol 五位数点问题,写个cdq分治套cdq分治套cdq分治套cdq分析就完了 可以用bitset搞 对于每一科开\(n\)个bitset,其中\(b[i]\)表示的排名为\(1 - ...

  2. .Net CIL

    MachineCode->Assembly->CIL(.Net) or SpecialMachineCode(JVM)->Pogramming code CIL Instructio ...

  3. ArcSDE空间数据库中SDE用户使用探讨(转)

    ArcSDE作为空间数据库解决方案,应用非常广泛,本短文将尝试描述SDE的工作机制,简要说明空间数据 库中SDE用户的使用方法. ArcSDE如何工作 ArcSDE属于中间件技术,其本身并不能够存储空 ...

  4. 理解python yield

    python源代码中经常会有使用yield,带有yield的函数是generator(生成器),它返回是一个迭代值,下面我们分析yield是什么原理,有什么好处? 首先,我们写一个简单的斐波那契数列前 ...

  5. [转]Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)

    今天学习了Spinner组件,使用Spinner相当于从下拉列表中选择项目,下面演示一下Spinner的使用(分别使用ArrayAdapter和自定义Adapter实现) (一):使用ArrayAda ...

  6. jQuery-prepend、append、before、after的区别

    举例说明,原始html代码如下: <ol> <li>List item 1</li> <li>List item 2</li> <li ...

  7. 如何用代码填充S/4HANA销售订单行项目的数量字段

    我的任务是用代码生成S/4HANA销售订单(Sales Order)的行项目,并且填充对应的quantity(数量)值. 最开始我用了下面的代码,把quantity的值写入item字段target_q ...

  8. framework7中一行的字如果过多就省略号显示的CSS写法

    .order-info-title { text-overflow: ellipsis !important; white-space: nowrap !important; overflow: hi ...

  9. 砍树,POJ(2665)

    题目链接:http://poj.org/problem?id=2665 解题报告: 这里的区域没有重复,若有重复的话,模拟即可. #include <cstdio> #include &l ...

  10. react里面Fragments的使用

    关于react Fragments,React 中一个常见模式是为一个组件返回多个元素.Fragments 可以让你聚合一个子元素列表,并且不在DOM中增加额外节点. render() { retur ...