/**
旋转卡壳,,
**/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std; const double eps = 1e-;
struct point {
double x,y;
point(double x=,double y =):x(x),y(y){}
}; int dcmp(double x){
if(fabs(x)<eps)
return ;
else
return x<?-:;
} point p[],p1[];
point q[],q1[];
int maxid,minid;
int N,M;
typedef point Vector; Vector operator -(point a,point b){
return Vector(a.x-b.x,a.y-b.y);
} double cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
} double dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
bool cmp(point a,point b){
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
} bool operator ==(const point &a,const point &b){
return dcmp(a.x-b.x)==&&dcmp(a.y-b.y)==;
} double length(Vector a){
return sqrt(a.x*a.x+a.y*a.y);
} int convexHull(point *p,int n,point *ch){
sort(p,p+n,cmp);
int m =;
for(int i=;i<n;i++){
while(m>&&cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)
m--;
ch[m++] = p[i];
}
int k =m;
for(int i = n-;i>=;i--){
while(m>k&&cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)
m--;
ch[m++] = p[i];
}
if(n>) m--;
return m;
} 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);
} 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)));
} 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;
}
} double solve(point *p,point *q,int minid,int maxid,int N,int M){
double temp,ret = 1e5;
p[N] = p[];
q[M] = q[];
for(int i=;i<N;i++){
while(temp = dcmp(cross(q[maxid]-q[maxid+],p[minid+]-p[minid]))>){
maxid = (maxid+)%M;
}
if(temp<)
ret = min(ret,distanceToSegment(q[maxid],p[minid],p[minid+]));
else
ret = min(ret,distanceBetweenSegment(p[minid],p[minid+],q[maxid],q[maxid+]));
minid = (minid+)%N;
}
return ret;
} int main()
{ while(cin>>N>>M){
if(N==&&M==)
break;
for(int i=;i<N;i++)
cin>>p[i].x>>p[i].y;
for(int i=;i<M;i++)
cin>>q[i].x>>q[i].y;
N = convexHull(p,N,p1);
M = convexHull(q,M,q1);
getMaxAndMin(maxid,minid);
double ans = solve(p1,q1,minid,maxid,N,M);
printf("%.5lf\n",ans);
}
return ;
}

poj 3608 Bridge Across Islands 两凸包间最近距离的更多相关文章

  1. ●POJ 3608 Bridge Across Islands

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

  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(旋转卡壳求解两凸包之间的最短距离)

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

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

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

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

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

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

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

  8. poj 3608 Bridge Across Islands

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

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

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

随机推荐

  1. php 接口示例

    php 接口示例: public function dev(){ $m=new Model('machine_info'); $ip=$_GET['ip']; echo $ip; //$arr=$m- ...

  2. MyEclipse13中修改Servlet.java源代码

    Servlet.java源代码想要修改的步骤,与低版本的不同废话少说,直接来步骤: 1,在myEclipse的安装目录中搜索com.genuitec.eclipse.wizards文件,如图:选择co ...

  3. Mysql文件太大导入失败解决办法总结

    Mysql文件太大导入失败解决办法总结 在使用phpmyadmin导入数据库的时候可能会碰到由于数据库文件太大而无法导入的问题! 英文提示如下:File exceeds the maximum all ...

  4. POJ 3311 Hie with the Pie (BFS+最短路+状态压缩)

    题意:类似于TSP问题,只是每个点可以走多次,求回到起点的最短距离(起点为点0). 分析:状态压缩,先预处理各点之间的最短路,然后sum[i][buff]表示在i点,状态为buff时所耗时...... ...

  5. httpunit使用演示样例

    import java.io.IOException; import java.net.MalformedURLException; import org.xml.sax.SAXException; ...

  6. jsp中的两种跳转方式分别是?有什么区别?

    在JSP中跳转有两种方式 forward跳转:<jsp:forward page ="跳转页面地址"> response跳转:response.sendRedirect ...

  7. CD key 生成

    题目描述如下: 某欧软件需要实现简易的CD-KEY算法,输入3个正整数,以空格隔开,根据者3个正整数生成的cd-key字符串.输出格式:xxxx-xxxx-xxxx-xxyy. 包含16个字符,以短划 ...

  8. Studious Student Problem Analysis

    (http://leetcode.com/2011/01/studious-student-problem-analysis.html) You've been given a list of wor ...

  9. WPF多线程下载文件,有进度条

    //打开对话框选择文件         private void OpenDialogBox_Click(object sender, RoutedEventArgs e)         {     ...

  10. PHP请求第三方接口的函数

    <?php public function HttpGet($url){ $curl = curl_init (); curl_setopt ( $curl, CURLOPT_URL, $url ...