题目链接 POJ-3608 Bridge Across Islands

题意

依次按逆时针方向给出凸包,在两个凸包小岛之间造桥,求最小距离。

题解

旋转卡壳的应用之一:求两凸包的最近距离。



找到凸包 p 的 y 值最小点 yminP 和 q 的 y 值最大点ymaxQ,然后分别做切线如图。

那么\(AC\times AD> AC\times AB\)则说明B还不是离AC最近的点,所以++ymaxQ。

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

代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#define sqr(x) (x)*(x)
#define N 50001
#define EPS (1e-8)
#define PI acos(-1.0)
#define INF (1e99)
using namespace std;
int sgn(double x) {
if(fabs(x) < EPS)return 0;
return (x < 0)?-1:1;
}
struct Point {
double x,y;
Point(double _x=0,double _y=0):x(_x), y(_y){}
Point operator -(const Point &b)const {
return Point(x - b.x,y - b.y);
}
Point operator +(const Point &b)const {
return Point(x + b.x,y + b.y);
}
double operator ^(const Point &b)const {
return x*b.y - y*b.x;
}
double operator *(const Point &b)const {
return x*b.x + y*b.y;
}
void in(){
scanf("%lf%lf",&x,&y);
}
};
double dis2(Point a,Point b){
return sqr(a-b);
}
double dist(Point a,Point b){
return sqrt(dis2(a,b));
}
struct Line {
Point s,e;
Line(){}
Line(Point _s,Point _e):s(_s),e(_e) {}
};
double xmult(Point a,Point b,Point o){
return (a-o)^(b-o);
}
double mult(Point a, Point b, Point o){
return (a-o)*(b-o);
}
double disToSeg(Point P,Line L){
if(!sgn(dis2(L.s,L.e)))
return dist(L.s,P);
if(sgn(mult(P,L.e,L.s))<0)return dist(L.s,P);
if(sgn(mult(P,L.s,L.e))<0)return dist(L.e,P);
return fabs(xmult(P,L.s,L.e))/dist(L.s,L.e);
}
double segToSeg(Line l1,Line l2){
return min(min(disToSeg(l1.s,l2),disToSeg(l1.e,l2)),min(disToSeg(l2.s,l1),disToSeg(l2.e,l1)));
}
Point p[N],q[N];
int n,m;
double qiake(){
int yminp=0,ymaxq=0;
for(int i=1;i<n;++i)
if(p[i].y<p[yminp].y)
yminp=i;
for(int i=1;i<m;++i)
if(q[i].y>q[ymaxq].y)
ymaxq=i;
p[n]=p[0];
q[m]=q[0];
double tmp,ans=INF;
for(int i=0;i<n;++i){
while(tmp=sgn(xmult(p[yminp+1],q[ymaxq+1],p[yminp])
-xmult(p[yminp+1],q[ymaxq],p[yminp]))>0)
ymaxq=(ymaxq+1)%m;
ans=min(ans,segToSeg(Line(p[yminp],p[yminp+1]),Line(q[ymaxq],q[ymaxq+1])));
yminp=(yminp+1)%n;
}
return ans;
}
int main(){
while(~scanf("%d%d",&n,&m)&&n&&m){
for(int i=0;i<n;++i)
p[i].in();
for(int i=0;i<m;++i)
q[i].in();
printf("%f\n",qiake());
}
return 0;
}

「POJ-3608」Bridge Across Islands (旋转卡壳--求两凸包距离)的更多相关文章

  1. POJ3608(旋转卡壳--求两凸包的最近点对距离)

    题目:Bridge Across Islands 分析:以下内容来自:http://blog.csdn.net/acmaker/article/details/3178696 考虑如下的算法, 算法的 ...

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

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

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

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

  4. poj 3608 旋转卡壳求不相交凸包最近距离;

    题目链接:http://poj.org/problem?id=3608 #include<cstdio> #include<cstring> #include<cmath ...

  5. poj 3608(旋转卡壳求解两凸包之间的最短距离)

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

  6. 旋转卡壳求两个凸包最近距离poj3608

    #include <iostream> #include <cmath> #include <vector> #include <string.h> # ...

  7. POJ2187 旋转卡壳 求最长直径

    给定平面上的一些散点集,求最远两点距离的平方值. 题解: 旋转卡壳求出凸包,然后根据单调性,求出最远两点的最大距离 #pragma GCC optimize(2) #pragma G++ optimi ...

  8. 「POJ 3666」Making the Grade 题解(两种做法)

    0前言 感谢yxy童鞋的dp及暴力做法! 1 算法标签 优先队列.dp动态规划+滚动数组优化 2 题目难度 提高/提高+ CF rating:2300 3 题面 「POJ 3666」Making th ...

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

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

随机推荐

  1. Day1 Numerical simulation of optical wave propagation之标量衍射理论基本原理(一)

    <Numerical simulation of optical  wave propagation>内容 1. 介绍光波传输的基础理论.离散采样方法.基于MATLAB平台的编码实例以及具 ...

  2. Jmeter之发送请求入参必须使用编码格式、Jmeter之发送Delete请求可能入参需要使用编码格式

    这里的其中一个属性值必须要先编码再传参才可以,具体可以通过抓包分析观察:

  3. babel (二) update to v7

    一.rootmode In 7.1, we've introduced a rootMode option for further lookup if necessary. 二.Remove prop ...

  4. HDU 5782 Cycle —— KMP

    题目:Cycle 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5782 题意:给出两个字符串,判断两个字符串的每一个前缀是否循环相等(比如abc 和 ca ...

  5. eclipse 编码

    单个修改 右击 选择properties  

  6. 集合之TreeSet(含JDK1.8源码分析)

    一.前言 前面分析了Set接口下的hashSet和linkedHashSet,下面接着来看treeSet,treeSet的底层实现是基于treeMap的. 四个关注点在treeSet上的答案 二.tr ...

  7. 阿里云服务器晚上运行定时任务报Too many connections

    1. 相关查询连接数的命令 mysql>show variables like '%max_connections%'; +-------------------------+--------- ...

  8. Delphi调用MSSQL存储过程返回的多个数据集的方法

    varaintf:_Recordset;RecordsAffected:OleVariant; begin ADOStoredProc1.Close;ADOStoredProc1.Open;aintf ...

  9. ArrayList 初学小结!

    package good.com; import java.util.ArrayList;//导入 ArrayList 包 调用动态数组! public class ArrayListList { / ...

  10. python绝对路径和相对路径

    转自https://blog.csdn.net/databatman/article/details/49453953 下面的路径介绍针对windows,其他平台的暂时不是很了解. 在编写的py文件中 ...