POJ 3608 凸包间最短距离(旋转卡壳)
| 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 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
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 凸包间最短距离(旋转卡壳)的更多相关文章
- poj 3608 凸包间的最小距离
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7632 Accepted: ...
- POJ 3608 Bridge Across Islands [旋转卡壳]
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10455 Accepted: ...
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- 【POJ 2187】Beauty Contest(凸包直径、旋转卡壳)
给定点集的最远两点的距离. 先用graham求凸包.旋(xuán)转(zhuàn)卡(qiǎ)壳(ké)求凸包直径. ps:旋转卡壳算法的典型运用 http://blog.csdn.net/hanch ...
- [Bzoj1069][Scoi2007]最大土地面积(凸包)(旋转卡壳)
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3629 Solved: 1432[Submit][Sta ...
- [模板] 计算几何2: 自适应Simpson/凸包/半平面交/旋转卡壳/闵可夫斯基和
一些基本的定义在这里: [模板] 计算几何1(基础): 点/向量/线/圆/多边形/其他运算 自适应Simpson Simpson's Rule: \[ \int ^b_a f(x)dx\approx ...
- POJ - 2079:Triangle (旋转卡壳,求最大三角形)
Given n distinct points on a plane, your task is to find the triangle that have the maximum area, wh ...
- POJ - 3608 Bridge Across Islands【旋转卡壳】及一些有趣现象
给两个凸包,求这两个凸包间最短距离 旋转卡壳的基础题 因为是初学旋转卡壳,所以找了别人的代码进行观摩..然而发现很有意思的现象 比如说这个代码(只截取了关键部分) double solve(Point ...
- poj 2187 凸包加旋转卡壳算法
题目链接:http://poj.org/problem?id=2187 旋转卡壳算法:http://www.cppblog.com/staryjy/archive/2009/11/19/101412. ...
随机推荐
- c#真正判断文件类型
//真正判断文件类型的关键函数 public static bool IsAllowedExtension2(FileUpload hifile) { if (hifile != null) { Sy ...
- ZR国庆Round2解题报告
心路历程 预计得分:100 + 10 - 20 + 10 = 120 实际得分:100 + 0 + 10 = 110 感觉这场打的挺稳的.开场秒掉A题,写+调差不多1h 然后刚T3暴力,刚完还有2h左 ...
- 在 CentOS7 上安装 swftools
1.从官网下载 swftools,这里下载的是 0.9.2 版本: wget http://www.swftools.org/swftools-0.9.2.tar.gz 2.下载后得到 swftool ...
- CSS实现多重边框和内凹圆角
CSS实现多重边框 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&q ...
- Azure进阶攻略 | 该如何唤醒你?因内核超时而沉睡的Linux虚拟机!
周五下午,当你收拾好东西准备下班,奔赴 Happy Hour 时,突然接到开发团队的电话: 对方:伙计救命啊,我在搭建开发环境,但 Azure 上的 Linux 虚拟机无法启动! 你(心里想着:你要加 ...
- 科大讯飞语音转文字以及中文分词的Java测试代码
我录了一段音存储在这个test.m4a文件里,语音内容为"测试一下Netweaver对于并发请求的响应性能". 使用如下Java代码进行测试: package com.iflyte ...
- POJ-2377 Bad Cowtractors---最大生成树
题目链接: https://vjudge.net/problem/POJ-2377 题目大意: 给一个图,求最大生成树权值,如果不连通输出-1 思路: kruskal算法变形,sort按边从大到小排序 ...
- HDU(1166),线段树模板,单点更新,区间总和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 第一次做线段树,帆哥的一句话,我记下来了,其实,线段树就是一种处理数据查询和更新的手段. 然后, ...
- next_permutation暴力搜索,POJ(3187)
题目链接:http://poj.org/problem?id=3187 解题报告: #include <stdio.h> #include <iostream> #includ ...
- 创建 XXXXXXXX 的配置节处理程序时出错: 请求失败
今天碰到这个错误,之前的程序在测试的时候都没有问题,同样的程序打包通过QQ传给其他人,在XP下测试也没有问题,我的Win7系统从QQ信箱下载压缩包,解压之后执行程序就会出问题,本来还是考虑自己程序是不 ...