Bridge Communication SS Islands
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:8071 |
|
Accepted:2364 |
|
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 integersN,M. (3 ≤N,M≤ 10000)
Each of the nextNLines contains a pair of coordinates, which describes the position of a vertex in one convex polygon.
Each of the nextMLines contains a pair of coordinates, which describes the position of a vertex in the other convex polygon.
A lineN=M= 0 indicates the end of input.
The coordinates are within the range [-10000,100 00].
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 0
Sample Output
1.00000
Typical questions
Code:
/* ***********************************************Author :_rabbitCreated Time :2014/5/10 16:26:51File Name :20.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include
#include
#include #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3f#define eps 1e-5#define pi acos(-1.0)typedef long long ll;int dcmp(double x){if(fabs(x)
0?1:-1;}struct Point{double x,y;Point(double _x=0,double _y=0){x=_x;y=_y;}};Point operator + (const Point &a,const Point &b){ return Point(a.x+b.x,a.y+b.y); } Point operator - (const Point &a,const Point &b){ return Point(a.x-b.x,a.y-b.y); } Point operator * (const Point &a,const double &p){ return Point(a.x*p,a.y*p); } Point operator / (const Point &a,const double &p){ return Point(a.x/p,a.y/p); } bool operator < (const Point &a,const Point &b){ return a.x
0) return Length(v3); else return fabs(Cross(v1, v2)) / Length(v1); } double dis_pair_seg(Point p1, Point p2, Point p3, Point p4) { return min(min(DistanceToSegment(p1, p3, p4), DistanceToSegment(p2, p3, p4)), min(DistanceToSegment(p3, p1, p2), DistanceToSegment(p4, p1, p2))); } vector
CH(vector
p){sort(p.begin(),p.end());p.erase(unique(p.begin(),p.end()),p.end());int n=p.size();int m=0;vector
ch(n+1);for(int i=0;i
1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-1])<=0)m--;ch[m++]=p[i];}int k=m;for(int i=n-2;i>=0;i--){while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;ch[m++]=p[i];}if(n>1)m--;ch.resize(m);return ch;}double RC_Distance(vector
ch1,vector
ch2) { int q=0, p=0,n=ch1.size(),m=ch2.size(); for(int i=0;i
eps) q=i; ch1.push_back(ch1[0]);ch2.push_back(ch2[0]); double tmp, ans=1e100; for(int i=0;i
eps) q=(q+1)%m; if(tmp < -eps) ans = min(ans,DistanceToSegment(ch2[q],ch1[p],ch1[p+1])); else ans = min(ans,dis_pair_seg(ch1[p],ch1[p+1],ch2[q],ch2[q+1])); p=(p+1)%n; } return ans; } int main(){ //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int n,m; while(cin>>n>>m){ if(n==0&&m==0)break; vector
ch1,ch2; Point p; while(n--)scanf("%lf%lf",&p.x,&p.y),ch1.push_back(p); while(m--)scanf("%lf%lf",&p.x,&p.y),ch2.push_back(p); ch1=CH(ch1);ch2=CH(ch2); printf("%.5lf\n",RC_Distance(ch1,ch2)); } return 0;}