poj 3608 旋转卡壳求不相交凸包最近距离;
题目链接: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 旋转卡壳求不相交凸包最近距离;的更多相关文章
- Bridge Across Islands POJ - 3608 旋转卡壳求凸包最近距离
\(\color{#0066ff}{题目描述}\) 几千年前,有一个小王国位于太平洋的中部.王国的领土由两个分离的岛屿组成.由于洋流的冲击,两个岛屿的形状都变成了凸多边形.王国的国王想建立一座桥来连接 ...
- poj 3608(旋转卡壳求解两凸包之间的最短距离)
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9768 Accepted: ...
- 「POJ-3608」Bridge Across Islands (旋转卡壳--求两凸包距离)
题目链接 POJ-3608 Bridge Across Islands 题意 依次按逆时针方向给出凸包,在两个凸包小岛之间造桥,求最小距离. 题解 旋转卡壳的应用之一:求两凸包的最近距离. 找到凸包 ...
- POJ 3608 旋转卡壳
思路: 旋转卡壳应用 注意点&边 边&边 点&点 三种情况 //By SiriusRen #include <cmath> #include <cstdi ...
- POJ3608(旋转卡壳--求两凸包的最近点对距离)
题目:Bridge Across Islands 分析:以下内容来自:http://blog.csdn.net/acmaker/article/details/3178696 考虑如下的算法, 算法的 ...
- POJ2187 旋转卡壳 求最长直径
给定平面上的一些散点集,求最远两点距离的平方值. 题解: 旋转卡壳求出凸包,然后根据单调性,求出最远两点的最大距离 #pragma GCC optimize(2) #pragma G++ optimi ...
- 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 , 旋转卡壳求凸包的直径的平方
旋转卡壳求凸包的直径的平方 板子题 #include<cstdio> #include<vector> #include<cmath> #include<al ...
- UVa 1453 - Squares 旋转卡壳求凸包直径
旋转卡壳求凸包直径. 参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <cstdio> ...
随机推荐
- Java中JIN机制及System.loadLibrary() 的执行过程
Android平台Native开发与JNI机制详解 http://mysuperbaby.iteye.com/blog/915425 个人认为下面这篇转载的文章写的很清晰很不错. 注意Android平 ...
- ASP。net中如何在一个按钮click事件中调用另一个按钮的click事件
方法一: 直接指定 事件<asp:Button ID="btn1" runat="server" Text="按钮1" onclick ...
- ui线程和后台线程异步
private void Button_Click(object sender, RoutedEventArgs e) { CreateElementOnSeperateThread(() => ...
- 深入了解java集群技术
原文源自:http://blog.csdn.net/happyangelling/article/details/6413584 序言 越来越多的关键应用运行在J2EE(Java 2, Enterpr ...
- JDK常用类_util
集合 Collection:集合顶层接口 AbstractCollection:集合抽象类 关联数组 Map:顶层接口 AbstractMap:抽象类实现,提供了子类的通用操作 HashMap:哈希表 ...
- SGU 148.B-Station
时间限制:0.25s 空间限制:4M 题目 在离著名的国家Berland不远的地方,有一个水下工作站.这个工作站有N层.已知:是第层装有Wi的水,最多可以容纳Li的水,恐怖分子炸毁第i的代价是Pi. ...
- [翻译]jQuery十周年-John Resig
10th Anniversary of jQuery Today marks the 10th anniversary of the release of jQuery...[原文] 今天是jQuer ...
- css学习--inline-block详解及dispaly:inline inline-block block 三者区别精要概括
*知识储备: 内联元素:是不可以控制宽和高.margin等:并且在同一行显示,不换行. 块级元素:是可以控制宽和高.margin等,并且会换行. 1.inline-block 详解 (1)一句话就是在 ...
- JDK环境变量配置贺Tomcat环境搭建
一.安装JDK JDK (Java Develpmet kit) Java开发环境(工具包和运行环境jre) 是Java开发的核心,包括:编译程序的命令 javac 运行程序java命令 he jav ...
- python cx_Oracle install
Ubuntu install python-cx_Oracle 1: install alien tools . sudo apt-get install alien 2: dowload fol ...