poj 3608 Bridge Across Islands (计算几何)

来源:互联网 发布:软件开发部门ios 编辑:程序博客网 时间:2024/06/06 04:49

Bridge Across Islands
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10122 Accepted: 2975 Special Judge

Description

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 NM. (3 ≤ NM ≤ 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 40.00000 0.000000.00000 1.000001.00000 1.000001.00000 0.000002.00000 0.000002.00000 1.000003.00000 1.000003.00000 0.000000 0

Sample Output

1.00000

Source

POJ Founder Monthly Contest – 2008.06.29, Lei Tao

[Submit]   [Go Back]   [Status]   [Discuss]



#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<cstdlib>#define N 50003#define eps 1e-8using 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[N],q[N];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[n]=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));}}


0 0
原创粉丝点击