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 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 0Sample Output
1.00000Source
POJ Founder Monthly Contest – 2008.06.29, Lei Tao
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
旋轉(zhuǎn)卡殼~
(被%lf卡精度調(diào)了三個小時是怎樣的一種體驗?)
輸出要用%f,不能用%lf……玄學(xué)問題n號……
因為題中已經(jīng)給出了凸包,所以是不用再自己寫的。直接用旋轉(zhuǎn)卡殼,枚舉每條邊到另一個凸包的距離,更新答案。
注意要分別用兩個凸包卡對方一次!
(最好不要用bool Operator,容易WA……)
詳見:http://blog.csdn.net/acmaker/article/details/3178696~
#include<cstdio>#include<iostream>#include<cmath>using namespace std;#define eps 1e-8const 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;}
新聞熱點
疑難解答