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


题意:给出两个凸多边形,求他们之间的最近距离


经典:http://blog.csdn.net/acmaker/article/details/3178696


用一个凸包的边去卡另一个凸包的点
先找到一个凸包p的最低点和另一个凸包q的最高点,这样就有了两条线j,j+1和k,k+1来卡
枚举j,移动k使得两条线差不多平行(用叉积判断;可以发现也是单调的)
然后更新ans,如果平行就是平行线段之间最短距离,否则用点k和线段j,j+1的距离
 
注意要用两次,因为分别用两个凸包去卡对方
 
吐槽:哪里说要5位小数了!!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=5e4+;
const double INF=1e99;
const double eps=1e-; inline int sgn(double x){
if(abs(x)<eps) return ;
else return x<?-:;
} struct Vector{
double x,y;
Vector(double a=,double b=):x(a),y(b){}
bool operator <(const Vector &a)const{
return sgn(x-a.x)<||(sgn(x-a.x)==&&sgn(y-a.y)<);
}
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==&&sgn(a.y-b.y)==;}
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;} double Len(Vector a){return sqrt(Dot(a,a));}
double Len2(Vector a){return Dot(a,a);}
double DisTL(Point p,Point a,Point b){
Vector v1=p-a,v2=b-a;
return abs(Cross(v1,v2)/Len(v2));
}
double DisTS(Point p,Point a,Point b){
if(a==b) return Len(p-a);
Vector v1=p-a,v2=p-b,v3=b-a;
if(sgn(Dot(v2,v3))>) return Len(v2);
else if(sgn(Dot(v1,v3))<) return Len(v1);
else return abs(Cross(v1,v3)/Len(v3));
} int n,m;
Point p[N],q[N];
double RotatingCalipers(Point p[],int n,Point q[],int m){
double ans=INF;
p[n+]=p[];
q[m+]=q[];
int j=,k=,t;
for(int i=;i<=n;i++) if(p[i].y<p[j].y) j=i;
for(int i=;i<=m;i++) if(q[i].y>q[k].y) k=i;
for(int i=;i<=n;i++){
while((t=sgn(Cross(p[j+]-p[j],q[k]-q[k+])))<) k=k%m+;
if(t==){
ans=min(ans,min(DisTS(q[k],p[j],p[j+]),DisTS(q[k+],p[j],p[j+])));
ans=min(ans,min(DisTS(p[j],q[k],q[k+]),DisTS(p[j+],q[k],q[k+])));
}else ans=min(ans,DisTS(q[k],p[j],p[j+]));
j=j%n+;
}
return ans;
} int main(int argc, const char * argv[]) {
while(true){
scanf("%d%d",&n,&m);if(n==&&m==) break;
for(int i=;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
for(int i=;i<=m;i++) scanf("%lf%lf",&q[i].x,&q[i].y);
double ans=min(RotatingCalipers(p,n,q,m),RotatingCalipers(q,m,p,n));
printf("%.5f\n",ans);
}
}
 

POJ 3608 Bridge Across Islands [旋转卡壳]的更多相关文章

  1. POJ 3608 Bridge Across Islands(旋转卡壳,两凸包最短距离)

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

  2. POJ 3608 Bridge Across Islands(计算几何の旋转卡壳)

    Description Thousands of thousands years ago there was a small kingdom located in the middle of the ...

  3. POJ 3608 Bridge Across Islands (旋转卡壳)

    [题目链接] http://poj.org/problem?id=3608 [题目大意] 求出两个凸包之间的最短距离 [题解] 我们先找到一个凸包的上顶点和一个凸包的下定点,以这两个点为起点向下一个点 ...

  4. POJ 3608 Bridge Across Islands --凸包间距离,旋转卡壳

    题意: 给你两个凸包,求其最短距离. 解法: POJ 我真的是弄不懂了,也不说一声点就是按顺时针给出的,不用调整点顺序. 还是说数据水了,没出乱给点或给逆时针点的数据呢..我直接默认顺时针给的点居然A ...

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

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

  6. ●POJ 3608 Bridge Across Islands

    题链: http://poj.org/problem?id=3608 题解: 计算几何,求两个凸包间的最小距离,旋转卡壳 两个凸包间的距离,无非下面三种情况: 所以可以基于旋转卡壳的思想,去求最小距离 ...

  7. poj 3608 Bridge Across Islands

    题目:计算两个不相交凸多边形间的最小距离. 分析:计算几何.凸包.旋转卡壳.分别求出凸包,利用旋转卡壳求出对踵点对,枚举距离即可. 注意:1.利用向量法判断旋转,而不是计算角度:避免精度问题和TLE. ...

  8. poj 3608 Bridge Across Islands 两凸包间最近距离

    /** 旋转卡壳,, **/ #include <iostream> #include <algorithm> #include <cmath> #include ...

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

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

随机推荐

  1. UE4 多人FPS VR游戏制作笔记

    1, 2,服务器游戏流程 服务器负责驱动游戏流程.服务器的职责是在游戏开始/结束以及 actor 复制更新等情况下通知客户端转移到新地图. 主要架构部分 大多在本文的讨论范围之外,但我们可以在遇到特定 ...

  2. YUI 阻止动态css加载

    skinnable动态加载 在YUI Module中,经常采用skinnable参数来动态加载css,如: YUI().use('w-paginator', function(Y) { }, requ ...

  3. Spark学习笔记2(spark所需环境配置

    Spark学习笔记2 配置spark所需环境 1.首先先把本地的maven的压缩包解压到本地文件夹中,安装好本地的maven客户端程序,版本没有什么要求 不需要最新版的maven客户端. 解压完成之后 ...

  4. 本地如何使用phpstudy环境搭建多站点

    http://jingyan.baidu.com/article/e52e36154227ef40c70c5147.html 平时在开发项目的时候, 多个项目同时开发的时候会遇到都得放到根目录才能正常 ...

  5. ecshop_标签大全

    admin 后台功能 -------templates后台模板 data 上传文件.SQL备份文件.配置项 ------sqldata 数据库备份文件 ------config.php配置文件 inc ...

  6. Git学习(2)-使用Git 代码将本地文件提交到 GitHub

    上次随笔写到git的安装和运用命令窗口创建本地版本库,这次主要讲一下用git代码将本地文件提交到GitHub上. 前提是有一个GitHub账号. 1.创建一个新的版本库,进入到你本地项目的根目录下(我 ...

  7. git分支小问题

    参考网址:http://hbiao68.iteye.com/blog/2055493 1.查看分支 git branch 或者 git branch -v 2.创建一个新的分支 git branch ...

  8. Javascript学习--时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  9. 微信跳一跳的mini辅助设计

    前一段考试没时间写东西,就迟到补发一波,继电器触发触屏,arduino处理数据就行了,B站很多人做,我也来一个,个人测试数据增益为2.1左右,即  延时=距离X2.1x10 void setup() ...

  10. Linux指令--diff

    diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...