终于搞下这题了,手抖率真心太高了,点到线段最短距离搞了好久,擦。。。
旋转卡壳求两凸包的最近距离,用比较面积的方法来求果然碉堡了,在内测的时候有向面积变为负,所以对踵点一样地找。
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <memory>
#include <complex>
#include <numeric>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>
using namespace std;
#pragma pack(4)
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x7f7f7f7f;
#define loop(a,n) \
for(int i=0;n>i;i++) \
cout<<a[i]<<(i!=n-1?' ':'\n')
#define loop2(a,n,m) \
for(int i=0;n>i;i++) \
for(int j=0;m>j;j++) \
cout<<a[i][j]<<(j!=m-1?' ':'\n')
#define at(a,i) ((a)&(1<<(i)))
#define nt(a,i) ((a)^(1<<(i)))
#define set1(a,i) ((a)|(1<<(i)))
#define set0(a,i) ((a)&(~(1<<(i))))
#define cmp(a,b) (fabs((a)-(b))<eps?0:(((a)-(b))>eps?+1:-1))
#define lmax(a,b) ((a)>(b)?(a):(b))
#define lmin(a,b) ((a)<(b)?(a):(b))
#define fmax(a,b) (cmp(a,b)>0?(a):(b))
#define fmin(a,b) (cmp(a,b)<0?(a):(b))
const int MAXV = 10002;
typedef struct Point
{
double o[2];
double & operator [] (int i)
{
return o[i];
}
friend ostream & operator << (ostream & cout,Point argu)
{
return cout<<"("<<argu[0]<<","<<argu[1]<<")";
}
Point(double x=0,double y=0)
{
o[0]=x;
o[1]=y;
}
Point operator + (Point argu)
{
return Point(o[0]+argu[0],o[1]+argu[1]);
}
Point operator - (Point argu)
{
return Point(o[0]-argu[0],o[1]-argu[1]);
}
Point operator + (double argu)
{
return (*this)*(1+argu/length());
}
Point operator - (double argu)
{
return (*this)*(1-argu/length());
}
Point operator * (double argu)
{
return Point(o[0]*argu,o[1]*argu);
}
Point operator / (double argu)
{
return Point(o[0]/argu,o[1]/argu);
}
bool operator > (Point argu) const
{
if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])>0;
else return cmp(o[1],argu[1])>0;
}
bool operator >= (Point argu) const
{
if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])>=0;
else return cmp(o[1],argu[1])>=0;
}
bool operator < (Point argu) const
{
if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])<0;
else return cmp(o[1],argu[1])<0;
}
bool operator <= (Point argu) const
{
if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])<=0;
else return cmp(o[1],argu[1])<=0;
}
bool operator != (Point argu) const
{
return cmp(o[0],argu[0])!=0||cmp(o[1],argu[1])!=0;
}
bool operator == (Point argu) const
{
return cmp(o[0],argu[0])==0&&cmp(o[1],argu[1])==0;
}
double length(void) const
{
return sqrt(o[0]*o[0]+o[1]*o[1]);
}
double angle(void) const
{
return fmod(atan2(o[1],o[0])+2*pi,2*pi);
}
}Vector;
double dot(Vector a,Vector b)
{
return a[0]*b[0]+a[1]*b[1];
}
double dot(Point o,Point a,Point b)
{
return dot(a-o,b-o);
}
int ward(Point p,Point s,Point e)
{
return cmp(dot(s,e,p),0);
}
double cross(Vector a,Vector b)
{
return a[0]*b[1]-a[1]*b[0];
}
double cross(Point o,Point a,Point b)
{
return cross(a-o,b-o);
}
int side(Point p,Point s,Point e)
{
return cmp(cross(s,e,p),0);
}
double angle(Vector a,Vector b)
{
return (b-a).length();
}
double angle(Point o,Point a,Point b)
{
return angle(a-o,b-o);
}
double length(Point s,Point e)
{
return (s-e).length();
}
double length(Point p,Point s,Point e)
{
if(ward(p,s,e)*ward(p,e,s)>0) return abs(cross(p,s,e)/length(s,e));
else return min(length(p,s),length(p,e));
}
double length(Point s1,Point e1,Point s2,Point e2)
{
return min(min(length(s1,s2,e2),length(e1,s2,e2)),min(length(s2,s1,e1),length(e2,s1,e1)));
}
double area(Point poly[],int n)
{
double ans=0;
for(int i=1;n>i+1;i++)
{
ans+=cross(poly[0],poly[i],poly[i+1]);
}
return abs(ans)/2;
}
int boundingBox(double x,double a,double b)
{
return cmp(a,x)*cmp(x,b);
}
int boundingBox(Point p,Point s,Point e)
{
if(boundingBox(p[0],s[0],e[0])>0&&boundingBox(p[1],s[1],e[1])>0) return 1;
else if(boundingBox(p[0],s[0],e[0])>=0&&boundingBox(p[1],s[1],e[1])>=0) return 0;
else return -1;
}
Point solve(double a,double b,double c,double d,double e,double f)
{
Point A(a,b);
Point B(c,d);
Point R(e,f);
return Point(cross(B,R),cross(R,A))/cross(A,B);
}
int locate(Point p,Point s,Point e)
{
if(p==s||p==e) return 0;
return side(p,s,e)==0&&boundingBox(p,s,e)>=0?1:-1;
}
int locate(Point p,Point poly[],int n)
{
int cnt[3]={0};
poly[n]=poly[0];
for(int i=0;n>i;i++)
{
cnt[side(p,poly[i],poly[i+1])+1]=true;
if(cnt[1]) return 0;
if(cnt[2]&&cnt[0]) return -1;
}
return 1;
}
int intersection(Point s1,Point e1,Point s2,Point e2)
{
if(side(s1,s2,e2)*side(e1,s2,e2)<0&&side(s2,s1,e1)*side(e2,s1,e1)<0) return 1;
else
{
if(locate(s1,s2,e2)>=0) return 0;
if(locate(e1,s2,e2)>=0) return 0;
if(locate(s2,s1,e1)>=0) return 0;
if(locate(e2,s1,e1)>=0) return 0;
return -1;
}
}
int convexHull(Point poly[],int n,Point convex[])
{
int cnt=0,ans=0;
sort(poly,poly+n);
for(int i=0;n>i;i++)
{
while(cnt>ans+1&&side(poly[i],convex[cnt-2],convex[cnt-1])<=0)
cnt--;
convex[cnt++]=poly[i];
}
ans=cnt-1;
for(int i=n-2;i>=0;i--)
{
while(cnt>ans+1&&side(poly[i],convex[cnt-2],convex[cnt-1])<=0)
cnt--;
convex[cnt++]=poly[i];
}
return cnt-1;
}
double rotateCliper(Point p1[],Point p2[],int n,int m)
{
double ans=inf,temp;
int f1=min_element(p1,p1+n)-p1;
int f2=max_element(p2,p2+m)-p2;
p1[n]=p1[0];
p2[m]=p2[0];
for(int i=0;n>i;i++)
{
while(temp=cmp(cross(p1[f1],p1[f1+1],p2[f2+1]),cross(p1[f1],p1[f1+1],p2[f2]))>0)
f2=(f2+1)%m;
if(cmp(temp,0)!=0) ans=min(ans,length(p2[f2],p1[f1],p1[f1+1]));
else ans=min(ans,length(p2[f2],p2[f2+1],p1[f1],p1[f1+1]));
f1=(f1+1)%n;
}
return ans;
}
int n,m;
double x,y;
Point p1[MAXV],p2[MAXV],c1[MAXV],c2[MAXV];
int main()
{
#ifndef ONLINE_JUDGE
freopen("Bridge Across Islands.txt","r",stdin);
#else
#endif
while(scanf("%d %d",&n,&m),n||m)
{
for(int i=0;n>i;i++)
{
scanf("%lf %lf",&x,&y);
p1[i]=Point(x,y);
}
for(int i=0;m>i;i++)
{
scanf("%lf %lf",&x,&y);
p2[i]=Point(x,y);
}
n=convexHull(p1,n,c1);
m=convexHull(p2,m,c2);
printf("%.4f\n",min(rotateCliper(c1,c2,n,m),rotateCliper(c2,c1,m,n)));
}
return 0;
}