Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 11382 | Accepted: 3346 | 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 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
旋转卡壳求两凸包最短距离
信仰调试了一晚上
最后还是没A掉
然后尝试粘别人blog的代码 很多也没A掉。。。。
有可能数据搞shit了 但我也不想再写凸包了。。。
认为我A了吧。。。
UPD: 2018/3/1 在我想起来这个挂了很久的题以后 我写了 %f 并且A掉了它.
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef double db;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=50100,inf=0X3f3f3f3f;
const db eps=1e-8;
struct node
{
db x,y;
friend db dot(const node &x,const node &y,const node &bas)
{
db x1=x.x-bas.x, x2=y.x-bas.x;
db y1=x.y-bas.y, y2=y.y-bas.y;
return x1*x2+y1*y2;
}
friend db cross(const node &x,const node &y,const node &bas)
{
db x1=x.x-bas.x, x2=y.x-bas.x;
db y1=x.y-bas.y, y2=y.y-bas.y;
return x1*y2-x2*y1;
}
friend db dis(const node &x,const node &y)
{return sqrt( (x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y) );}
friend db node_seg(const node &x,const node &a,const node &b)
{
if(dis(a,b)<eps) return dis(x,a);
if(dot(x,b,a)<-eps) return dis(x,a);
if(dot(x,a,b)<-eps) return dis(x,b);
return abs(cross(a,b,x)/dis(a,b));
}
friend db seg(const node &x,const node &y,const node &a,const node &b)
{
db tmp1=min( node_seg(x,a,b) , node_seg(y,a,b) );
db tmp2=min( node_seg(a,x,y) , node_seg(b,x,y) );
return min(tmp1,tmp2);
}
}p[2][N];
int n,m;
db RC()
{
register int i,ymn=1,ymx=1;
for(i=1;i<=n;++i)
if(p[0][i].y<p[0][ymn].y)
ymn=i;
for(i=1;i<=m;++i)
if(p[1][i].y>p[1][ymx].y)
ymx=i;
db ans=dis(p[0][ymn],p[1][ymx]),tmp;
p[0][++n]=p[0][1];p[1][++m]=p[1][1];
for(i=1;i<n;++i)
{
while( (tmp=cross(p[0][ymn+1],p[1][ymx],p[0][ymn])-cross(p[0][ymn+1],p[1][ymx+1],p[0][ymn]))<-eps )
{
ymx++;
if(ymx==m)ymx=1;
}
if(tmp>eps)
ans=min(ans,node_seg(p[1][ymx],p[0][ymn],p[0][ymn+1]));
else
ans=min(ans,seg(p[0][ymn],p[0][ymn+1],p[1][ymx],p[1][ymx+1]));
ymn++;
if(ymn==n)ymn=1;
}
n--;m--;
swap(n,m);
swap(p[0],p[1]);
return ans;
}
int main()
{
register int i;
while(1)
{
n=read();m=read();
if(!n&&!m)break;
for(i=1;i<=n;++i)
scanf("%lf%lf",&p[0][i].x,&p[0][i].y);
for(i=1;i<=m;++i)
scanf("%lf%lf",&p[1][i].x,&p[1][i].y);
printf("%.5f\n",min(RC(),RC()));
}
return 0;
}
/*
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
1.00000
*/