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. ...
随机推荐
- Redis入门--(二)Redis的概述
1.Redis的由来 创始人觉得Mysql不好用,就自己写了: 国内使用Redis的网站有新浪微博,知乎: 国外GitHub: VMWare也支持redis的开发 2.Redis的概述 官方提供的测试 ...
- 非关系型数据库(NOSQL)-Redis
整理一波Redis 简介,与memcached比较 官网:http://redis.io Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括 ...
- 切片操作:MATLAB VS Python
切片操作:MATLAB VS Python 一.MATLAB 矩阵的拆分 1.冒号表达式: t = e1:e2:e3 e1表示初始值,e2为步长,e3为终止值(包括e3),产生一个从e1到e3,步长为 ...
- 转:ACCESS数据库转ORACLE数据库分享
来源: 作者:zz 网上有很多文章介绍access转oracle数据库的方法,本人都尝试了,不是很成功,列举一下,后来人不必盲目试了,基本不成功: 1.Access-->excel-->P ...
- SpringCloud的学习记录(6)
这一章节讲fegin的使用. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息, 这 ...
- 笨办法学Python(八)
习题 8: 打印,打印 formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) print formatter % (&q ...
- C语言中头文件怎么写?(本文来源网络,由黑乌鸦进一步完善)
c语言头文件怎么写?我一直有这样的疑问,但是也一直没去问问到底咋回事:所以今天一定要把它弄明白! 其实学会写头文件之后可以为我们省去不少事情,可以避免书写大量的重复代码.有利于整理思路.使代码脉络 ...
- axure 动态面板实现图片轮播效果(淘宝)
淘宝中经常可以看到店铺中的图片轮播效果,本经验将通过axure7.0实现 工具/原料 axure7.0 方法/步骤 下载需要轮播的图片 将图片引入至axure中,将引入的第一张图片转为 ...
- 2017.9.25 JSP内置对象的概述
1.JSP的定义: 在JSP中是为了便于数据信息的存储.传递.获取,专门设置了九个内置对象, jsp内置对象是指他们是预先设定的,不需创建,每个对象都有自己的属性和方法. 2.JSP内置对象 对象名称 ...
- Win7多用户同时登陆
软件提供下载: http://pan.baidu.com/s/1o6FQv70