POJ 3608 Bridge Across Islands (计算几何+三分)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 7395 | Accepted: 2183 | 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.
![](http://poj.org/images/3608_1.gif)
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
#include <iostream> #include <cstdio> #include <vector> #include <cmath> #include <algorithm> #include <cstdlib> using namespace std; const int maxn=1000; const double eps=1e-10; struct point{ double x,y; point(double x0=0,double y0=0){ x=x0; y=y0; } friend bool operator < (point a,point b){ if(abs(a.y-b.y) > eps) return a.y<b.y; else return a.x<b.x; } double getdis(point p){ return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); } }; int m,n; vector <point> vm,vn,p; double xchen(point a,point b,point c){//ab 叉乘 ac return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); } double dchen(point p0,point p1,point p2){//p0p1 点乘 p0p2; return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y); } bool cmp(point a,point b){ return xchen(p[0],a,b)>0; } double getldis(point a,point b,point p){//点到线段的最小距离 if(dchen(a,p,b)<0 || dchen(b,p,a)<0) return min(p.getdis(a),p.getdis(b)); else{ double a0,b0,c0; a0=a.y-b.y; b0=b.x-a.x; c0=a.x*b.y-b.x*a.y; return fabs(a0*p.x+b0*p.y+c0)/sqrt(a0*a0+b0*b0); } } void qiutubao(){ p.resize(m); for(int i=0;i<m;i++){ scanf("%lf%lf",&p[i].x,&p[i].y); } sort(p.begin(),p.end()); sort(p.begin()+1,p.end(),cmp); vm=p; vm.push_back(p[0]); p.resize(n); for(int i=0;i<n;i++){ scanf("%lf%lf",&p[i].x,&p[i].y); } sort(p.begin(),p.end()); sort(p.begin()+1,p.end(),cmp); vn=p; vn.push_back(p[0]); } void computing(){ qiutubao(); double mindis=1e12; for(int i=0;i<m;i++){ int l=0,r=n-1; while(l<r){ //cout<<l<<" "<<r<<endl; //system("pause"); int lmd=(2*l+r)/3; int rmd=(l+2*r)/3; double dislmd=getldis(vm[i],vm[i+1],vn[lmd]); double disrmd=getldis(vm[i],vm[i+1],vn[rmd]); if(dislmd<=disrmd) r=rmd; else l=lmd+1; } mindis=min(mindis,getldis(vm[i],vm[i+1],vn[r])); } for(int i=0;i<n;i++){ int l=0,r=m-1; while(l<r){ int lmd=(2*l+r)/3; int rmd=(l+2*r)/3; double dislmd=getldis(vn[i],vn[i+1],vm[lmd]); double disrmd=getldis(vn[i],vn[i+1],vm[rmd]); if(dislmd<=disrmd) r=rmd; else l=lmd+1; } mindis=min(mindis,getldis(vn[i],vn[i+1],vm[r])); } printf("%.5lf\n",mindis); } int main(){ while(scanf("%d%d",&m,&n)!=EOF && (m||n)){ computing(); } return 0; }