题目链接:http://poj.org/problem?id=3608

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = ;
const int maxe = ;
const int INF = 0x3f3f3f;
const double eps = 1e-;
const double PI = acos(-1.0); struct Point{
double x,y;
Point(double x=, double y=) : x(x),y(y){ } //构造函数
};
typedef Point Vector; 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 p){return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);} bool operator < (const Point& a,const Point& b){
return a.x < b.x ||( a.x == b.x && a.y < b.y);
} int dcmp(double x){
if(fabs(x) < eps) return ;
else return x < ? - : ;
}
bool operator == (const Point& a, const Point& b){
return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
} ///向量(x,y)的极角用atan2(y,x);
double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A,A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A,B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y * B.x; } Vector Rotate(Vector A, double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad)); } ///求点P到线段AB的距离,先看Q点在线段外还是内;利用点积就可以,
double DistanceToSegment(Point P,Point A,Point B){
if(A == B) return Length(P-A);
Vector v1 = B - A,v2 = P - A,v3 = P - B;
if(dcmp(Dot(v1,v2)) < ) return Length(v2);
else if(dcmp(Dot(v1,v3) > )) return Length(v3);
else return fabs(Cross(v1,v2))/Length(v1);
}
///求线段AB与线段CD的距离;
double DistanceBetweenSegment(Point A,Point B,Point C,Point D){
return min(min(DistanceToSegment(A,C,D),DistanceToSegment(B,C,D)),min(DistanceToSegment(C,A,B),DistanceToSegment(D,A,B)));
} //凸包:
/**Andrew算法思路:首先按照先x后y从小到大排序(这个地方没有采用极角逆序排序,所以要进行两次扫描),删除重复的点后得到的序列p1,p2.....,然后把p1和p2放到凸包中。从p3开始,当新的
点在凸包“前进”方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边;**/ //Goal[]数组模拟栈的使用;
int ConvexHull(Point* P,int n,Point* Goal){
sort(P,P+n);
int m = unique(P,P+n) - P; //对点进行去重;
int cnt = ;
for(int i=;i<m;i++){ //求下凸包;
while(cnt> && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
Goal[cnt++] = P[i];
}
int temp = cnt;
for(int i=m-;i>=;i--){ //逆序求上凸包;
while(cnt>temp && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
Goal[cnt++] = P[i];
}
if(cnt > ) cnt--; //减一为了去掉首尾重复的;
return cnt;
} double solve(Point* P1,Point* Q1,int Minid,int Maxid,int N,int M){
double temp,ret = 1e5;
P1[N] = P1[]; Q1[M] = Q1[];
for(int i=;i<N;i++){
while(temp = dcmp(Cross(Q1[Maxid]-Q1[Maxid+],P1[Minid+]-P1[Minid]))> ) //这一步最难理解:要理解怎样才能使Q1[Maxid]Q1[Maxid+1]这条线段最接近线段P1[Minid+1]P1[Minid];
Maxid = (Maxid+)%M; // 先以P1[Minid]为定点,旋转Q1[Maxid];
if(temp < ) ret = min(ret,DistanceToSegment(Q1[Maxid],P1[Minid],P1[Minid+]));
else ret = min(ret,DistanceBetweenSegment(P1[Minid],P1[Minid+],Q1[Maxid],Q1[Maxid+]));\
Minid = (Minid + )%N; //再旋转P1[Minid];
}
return ret;
}
Point read_point(){
Point A;
scanf("%lf %lf",&A.x,&A.y);
return A;
} /*******************************分割线******************************/
Point P[maxn],Q[maxn];
Point P1[maxn],Q1[maxn]; //利用凸包算法使坐标逆时针化后的存储;
int N,M;
int Maxid,Minid; void GetMaxandMin(int& Maxid,int& Minid){
Maxid = ; Minid = ;
for(int i=;i<N;i++){
if(P1[i].y < P1[Minid].y) Minid = i;
}
for(int i=;i<M;i++){
if(Q1[i].y > Q1[Maxid].y) Maxid = i;
}
} int main()
{
//freopen("E:\\acm\\input.txt","r",stdin); while(cin>>N>>M && N+M){
for(int i=;i<N;i++) P[i] = read_point();
N = ConvexHull(P,N,P1); for(int i=;i<M;i++) Q[i] = read_point();
M = ConvexHull(Q,M,Q1); GetMaxandMin(Maxid,Minid); double ans = solve(P1,Q1,Minid,Maxid,N,M);
printf("%.5f\n",ans);
}
}

poj 3608 旋转卡壳求不相交凸包最近距离;的更多相关文章

  1. Bridge Across Islands POJ - 3608 旋转卡壳求凸包最近距离

    \(\color{#0066ff}{题目描述}\) 几千年前,有一个小王国位于太平洋的中部.王国的领土由两个分离的岛屿组成.由于洋流的冲击,两个岛屿的形状都变成了凸多边形.王国的国王想建立一座桥来连接 ...

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

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

  3. 「POJ-3608」Bridge Across Islands (旋转卡壳--求两凸包距离)

    题目链接 POJ-3608 Bridge Across Islands 题意 依次按逆时针方向给出凸包,在两个凸包小岛之间造桥,求最小距离. 题解 旋转卡壳的应用之一:求两凸包的最近距离. 找到凸包 ...

  4. POJ 3608 旋转卡壳

    思路: 旋转卡壳应用 注意点&边  边&边  点&点 三种情况 //By SiriusRen #include <cmath> #include <cstdi ...

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

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

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

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

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

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

  8. poj 2187 Beauty Contest , 旋转卡壳求凸包的直径的平方

    旋转卡壳求凸包的直径的平方 板子题 #include<cstdio> #include<vector> #include<cmath> #include<al ...

  9. UVa 1453 - Squares 旋转卡壳求凸包直径

    旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...

随机推荐

  1. window.showModalDialog()复制内容

    ShowModalDialog 打开的 页面上加入个 <span id="mySpan" name="mySpan" contentEditable=&q ...

  2. UTF-8和GBK有什么区别?

    字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1. 至于UTF-8编码则是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24位(三个字节)来编码.对于英文字符 ...

  3. Source not found for AeceManager$$FastClassByCGLIB$$15dcd49c.invoke(int, Object, Object[]) line: not available 问题解决

    一般出现这个问题,是manager的问题.控制台没有报错.是调试出来的.. 解决办法: 在调用此方法的manager里的方法上加上try  ...catch 重新启动调试, 就可在控台看到问题所在. ...

  4. Ubuntu Server 14.04 下root无法ssh登陆

    今天安装了Ubuntu Server 14.04   在终端配置了root密码后,使用SecureCRT和putty竟然不能ssh登陆,SecureCRT一直提示密码不对,但是可以肯定输入的密码100 ...

  5. C++中 _itoa_s方法简介

    _itoa_s 函数原型如下: _itoa_s ( int value, char *buffer, size_t sizeInCharacters, //存放结果的字符数组长度 int radix ...

  6. Prototype 模式

    Prototype 模式提供了一个通过已存在对象进行新对象创建的接口(Clone) ,Clone()实现和具体的实现语言相关,在 C++中我们将通过拷贝构造函数实现之. /////////////// ...

  7. SGU 152.Making round

    不断向下取直到,忽略的数累计到一个百分比,给当前百分比加1. 这道题要避免处理浮点数,用余数来处理,不然会wa 9 #include <iostream> #include <cma ...

  8. 自己制作精美的App Store 软件截屏

    当用户搜索到App的时候,一般都会先看截图,如果截图效果不好,可能用户就不会下载. 不想自己辛苦写的认为还不错的软件,因为截图的原因,而降低了很多下载量吧. 轻轻松松做出这样高大上的截屏效果来. Sc ...

  9. 小心DriveInfo类IsReady属性的较大延迟问题

    当某些驱动器调用IsReady属性来判断是否准备好时,会有性能问题,会非常慢,特别是网络驱动器断开的时候,这个属性会有30秒左右的延迟,这对程序执行是非常大的开销,请慎重调用

  10. 翻译-让ng的$http服务与jQuerr.ajax()一样易用

    Make AngularJS $http service behave like jQuery.ajax() 让ng的$http服务与jQuerr.ajax()一样易用 作者zeke There is ...