题目链接 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. UnderWater+SDN论文之五

    Underwater Sensor Networks with Mobile Agents: Experience from the Field Source: LNICST 2013 论文是来自两个 ...

  2. IOS-43-导航栏标题navigationItem.title不能改变颜色的两种解决方法

    IOS-43-导航栏标题navigationItem.title不能改变颜色的两种解决方法 IOS-43-导航栏标题navigationItem.title不能改变颜色的两种解决方法 两种方法只是形式 ...

  3. Python_架构、同一台电脑上两个py文件通信、两台电脑如何通信、几十台电脑如何通信、更多电脑之间的通信、库、端口号

    1.架构 C/S架构(鼻祖) C:client  客户端 S:server  服务器 早期使用的一种架构,目前的各种app使用的就是这种架构,它的表现形式就是拥有专门的app. B/S架构(隶属于C/ ...

  4. MySQL中关于数据类型指定宽度之后的情况

    概述 MySQL有很多种数据类型,最常用的就是int,char,varchar,这些类型在创建表的时候都可以指定该字段的宽度,方法是在类型后面加一个括号,括号中写宽度就可以了. 但是,在指定宽度之后, ...

  5. #Leetcode# 524. Longest Word in Dictionary through Deleting

    https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...

  6. Zabbix appliance manual

    https://www.zabbix.com/documentation/4.0/manual/appliance If the appliance fails to start up in Hype ...

  7. centos7 network eno16777736

    Network service not running - eno16777736 not activated - CentOShttps://www.centos.org/forums/viewto ...

  8. telnet总结

    telnet是经常使用的客户端链接工具,总结一下常用的telnet的使用方法 1) 连接 telnet //链接swoole 2)退出当前连接 ctrl + ] 回车 3)查看常用的一些命令 ? 回车 ...

  9. 对B+树,B树,红黑树的理解

    出处:https://www.jianshu.com/p/86a1fd2d7406 写在前面,好像不同的教材对b树,b-树的定义不一样.我就不纠结这个到底是叫b-树还是b-树了. 如图所示,区别有以下 ...

  10. MyBatis全局配置文件的各项标签3

    mapper 将sql映射注册到全局配置中,这个我们在上一章已经使用过了, resource 这个属性是用来引用类路径下的sql映射文件 url 这个属性是用来引用网络路径或磁盘路径下的sql映射文件 ...