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 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
旋转卡壳~
(被%lf卡精度调了三个小时是怎样的一种体验?)
输出要用%f,不能用%lf……玄学问题n号……
因为题中已经给出了凸包,所以是不用再自己写的。直接用旋转卡壳,枚举每条边到另一个凸包的距离,更新答案。
注意要分别用两个凸包卡对方一次!
(最好不要用bool operator,容易WA……)
详见:http://blog.csdn.net/acmaker/article/details/3178696~
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
#define eps 1e-8
const double inf=1e99;
int n,m;
struct node{
double x,y;
}a[10002],c[10002];
double dian(node u,node v)
{
return u.x*v.x+u.y*v.y;
}
double cha(node u,node v)
{
return u.x*v.y-u.y*v.x;
}
double dis(node u)
{
return sqrt(dian(u,u));
}
node operator -(node u,node v)
{
node p;
p.x=u.x-v.x;p.y=u.y-v.y;
return p;
}
int zer(double u)
{
if(abs(u)<eps) return 0;
return u<0 ? -1:1;
}
bool operator ==(node u,node v)
{
return !zer(u.x-v.x) && !zer(u.y-v.y);
}
double cal(node p,node u,node v)
{
if(u==v) return dis(p-u);
node x1=p-u,x2=p-v,x3=v-u;
if(zer(dian(x2,x3))>0) return dis(x2);
if(zer(dian(x1,x3))<0) return dis(x1);
return abs(cha(x1,x3)/dis(x3));
}
double findd(node u[],int n,node v[],int m)
{
int j=1,k=1,kkz;double ans=inf;
for(int i=2;i<=n;i++) if(u[i].y<u[j].y) j=i;
for(int i=2;i<=m;i++) if(v[i].y>v[k].y) k=i;
for(int i=1;i<=n;i++)
{
while((kkz=zer(cha((u[j+1]-u[j]),(v[k]-v[k+1]))))<0) k=k%m+1;
if(!kkz)
{
ans=min(ans,min(cal(u[j],v[k],v[k+1]),cal(u[j+1],v[k],v[k+1])));
ans=min(ans,min(cal(v[k],u[j],u[j+1]),cal(v[k+1],u[j],u[j+1])));
}
else ans=min(ans,cal(v[k],u[j],u[j+1]));
j=j%n+1;
}
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)==2 && n)
{
for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);a[n+1]=a[1];
for(int i=1;i<=m;i++) scanf("%lf%lf",&c[i].x,&c[i].y);c[m+1]=c[1];
double ans=min(findd(a,n,c,m),findd(c,m,a,n));
printf("%.5f\n",ans);
}
return 0;
}