poj 3608 Bridge Across Islands (计算几何)
2016-08-15 16:16
429 查看
Bridge Across Islands
Thousands of thousands years ago there was a small kingdom located in the middle of the Pacific Ocean. The territory of the kingdom consists two separated islands. Due to the impact of the ocean current, the shapes of both the islands became convex polygons. The king of the kingdom wanted to establish a bridge to connect the two islands. To minimize the cost, the king asked you, the bishop, to find the minimal distance between the boundaries of the two islands. Input The input consists of several test cases. Each test case begins with two integers N, M. (3 ≤ N, M ≤ 10000) Each of the next N lines contains a pair of coordinates, which describes the position of a vertex in one convex polygon. Each of the next M lines contains a pair of coordinates, which describes the position of a vertex in the other convex polygon. A line with N = M = 0 indicates the end of input. The coordinates are within the range [-10000, 10000]. Output For each test case output the minimal distance. An error within 0.001 is acceptable. Sample Input 4 4 0.00000 0.00000 0.00000 1.00000 1.00000 1.00000 1.00000 0.00000 2.00000 0.00000 2.00000 1.00000 3.00000 1.00000 3.00000 0.00000 0 0 Sample Output 1.00000 Source POJ Founder Monthly Contest – 2008.06.29, Lei Tao |
[Discuss]
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#define N 50003
#define eps 1e-8
using namespace std;
struct vector
{
double x,y;
vector (double X=0,double Y=0)
{
x=X,y=Y;
}
bool operator ==(const vector &a) const
{
return x==a.x&&y==a.y;
}
};
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 b){
return vector (a.x*b,a.y*b);
}
vector operator /(vector a,double b){
return vector(a.x/b,a.y/b);
}
typedef vector point;
int n,m;
point p
,q
;
int dcmp(double x)
{
if (fabs(x)<eps) return 0;
return x<0?-1:1;
}
double dot(vector a,vector b)
{
return a.x*b.x+a.y*b.y;
}
double cross(vector a,vector b)
{
return a.x*b.y-a.y*b.x;
}
double len(vector a)
{
return sqrt(dot(a,a));
}
double distl(point p,point a,point b)
{
vector v=b-a,u=p-a;
return fabs(cross(v,u))/len(v);
}
double dists(point p,point a,point b)//点到线段的距离,需要判断从点到线段做垂线,垂足是否在线段上,如果在直线上,那么距离求解的方式同上,否则距离为到线段较近端点的向量的模长
{
if (a==b) return len(p-a);
vector v1,v2,v3;
v1=b-a; v2=p-a; v3=p-b;
if (dcmp(dot(v1,v2))<0) return len(v2);
else if (dcmp(dot(v1,v3))>0) return len(v3);
else return fabs(cross(v1,v2))/len(v1);
}
double rotating_calipers(point* p,point* q,int n,int m)//利用旋转卡壳求两个凸包上最近点的距离,一个凸包上的平行线逆时针旋转,另一个凸包上的最远点也单调逆时针旋转,所以这个算法要正反进行两遍
{
int x=0; int y=0,i;
double ans=1e10,t;
for (int i=0;i<n;i++)
x=(p[i].y<p[x].y)?i:x;
for (int i=0;i<m;i++)
y=(p[i].y>p[y].y)?i:y;
p
=p[0]; q[m]=q[0];
for (int i=0;i<n;i++)
{
while((t=dcmp(cross(p[x]-p[x+1],q[y+1]-q[y])))<0)//判断凸壳上下一条边的旋转方向
y=(y+1)%m;
if (t==0)//平行的时候四个点之间更新答案
{
ans=min(ans,dists(p[x],q[y+1],q[y]));
ans=min(ans,dists(p[x+1],q[y+1],q[y]));
ans=min(ans,dists(q[y],p[x],p[x+1]));
ans=min(ans,dists(q[y+1],p[x],p[x+1]));
}
else
ans=min(ans,dists(q[y],p[x],p[x+1]));//否则只用最远点更新答案
x=(x+1)%n;
}
return ans;
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
if (n==0||m==0) break;
for (int i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for (int i=0;i<m;i++)
scanf("%lf%lf",&q[i].x,&q[i].y);
double ans1=rotating_calipers(p,q,n,m);
double ans2=rotating_calipers(q,p,m,n);
printf("%0.5lf\n",min(ans1,ans2));
}
}
相关文章推荐
- POJ 3608 Bridge Across Islands (计算几何+三分)
- POJ 3608 Bridge Across Islands (计算几何+三分)
- POJ 3608 Bridge Across Islands(两个凸包最近距离,旋转卡壳)
- poj 3608 Bridge Across Islands(旋转卡壳)
- 【POJ 3608】Bridge Across Islands
- POJ 3608 Bridge Across Islands(求两凸包间的最小距离)
- POJ 3608 Bridge Across Islands(旋转卡壳求凸多边形最短距离)
- poj 3608 Bridge Across Islands(两个凸包的最短距离)
- poj 3608 Bridge Across Islands 旋转卡壳(两个凸包的最近距离)
- poj 3608 Bridge Across Islands(旋转卡壳求俩凸包间最小间距)
- poj 3608 Bridge Across Islands, 旋转卡壳求凸多边形间最小距离
- POJ 3608 Bridge Across Islands(旋转卡壳)
- POJ 3608 Bridge Across Islands(旋转卡壳,两凸包最短距离)
- poj 3608 Bridge Across Islands(卡壳旋转求两个多边形的最近点对)
- POJ 3608 Bridge Across Islands
- Bridge Across Islands----POJ_3608----求两凸包最近距离----旋转卡壳
- POJ 3608 Bridge Across Islands(旋转卡壳 求不相交凸包之间的最短距离)
- poj 3608 Bridge Across Islands
- poj 3608 Bridge Across Islands
- poj 3608:Bridge Across Islands