Title: Portal
Classic title
Most of the code reference: kuangbin
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <map> #include <vector> #include <set> #include <string> #include <math.h> #define LL long long #define mem(i, j) memset(i, j, sizeof(i)) #define rep(i, j, k) for(int i = j; i <= k; i++) #define dep(i, j, k) for(int i = k; i >= j; i--) #define pb push_back #definethe make_pair the make #define INF INT_MAX #define INF LLONG_MAX #define the PI ACOS (-1) the using namespace STD; const int N = 5E5 + . 5 ; struct Point { Double X, Y; Point ( Double X = 0 , Double Y = 0 ) : X (X), Y (Y) {} /// constructor }; typedef point the vector; /// vector + vector = vector + vector = vector point the vector operator + (the vector A, the vector B) { return the vector (+ Ax of Bx, Ay + By);} /// point - point = vector of the Vector operator - (Point A, Point B) { return the Vector (Ax of - Bx, Ay - By);} /// vector * vector number = the Vector operator * (the Vector A, Double P) { return the vector (Ax of P *, Ay * P);} /// vector / vector number = the vector operator / (the vector A, Double P) { return the vector (Ax of / P, Ay / P);} const Double EPS = 1E- . 8 ; int dCMP ( Double X) { IF (FABS (X) <EPS) return 0; else return x < 0 ? -1 : 1; } bool operator < (const Point& a, const Point& b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } bool operator == (const Point& a, const Point &b) { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; } double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } // dot product Double the Length (the Vector A) { return sqrt ( 1.0 * Dot (A, A));} /// calculated vector length Double Angle (the Vector A, the Vector B) { return ACOS (Dot (A, B ) / the Length (A) / the Length (B));} /// vector A, B angle Double Cross (the vector A, the vector B) { return Ax of * By - Ay * Bx;} /// cross product int , ConvexHull (Point * P, int n-, Point * CH) { Sort (P, P + n-); int m = 0 ; REP (I, 0 , n-- . 1 ) { while(m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } int k = m; dep(i, 0, n - 2) { 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--; return m; } Double the PTS (Point p, Point A, Point B) { /// find the point p to the line segment AB shortest distance IF (A == B) return the Length (p - A); Point V1 = B - A , V2 = P - A, V3 = P - B; IF (dCMP (Dot (V1, V2)) < 0 ) return the Length (V2); the else IF (dCMP (Dot (V1, V3))> 0 ) return the Length (V3); the else return FABS (Cross (V1, V2)) / the Length (V1); } / * distance parallel to the line segment a1a2 b1b2 and * / Double DS(Point a1, Point a2, Point b1, Point b2) { return min(PTS(b1, a1, a2), min(PTS(b2, a1, a2), min(PTS(a1, b1, b2), PTS(a2, b1, b2)))); } /* 得到向量 a1a2 和 b1b2 的位置关系 */ double Get_angle(Point a1, Point a2, Point b1, Point b2) { Point t = b1 - (b2 - a1); return Cross(a2 - a1, t - a1); } double Rotating_calipers(Point P[], int n, Point Q[], int m) { int posp = 0, posq = 0; rep(i, 0, n - 1) if(dcmp(P[i].y - P[posp].y) < 0) posp = i; rep(i, 0, m - 1) if(dcmp(Q[i].y - Q[posq].y) > 0) posq = i; double tmp, ans = 1e99; rep(i, 0, n - 1) { while(dcmp(tmp = Get_angle(P[posp], P[(posp + 1) % n], Q[posq], Q[(posq + 1) % m])) < 0) posq = (posq + 1) % m; if(dcmp(tmp) == 0) ans = min(ans, DS(P[posp], P[(posp + 1) % n], Q[posq], Q[(posq + 1) % m])); else ans = min(ans, PTS(Q[posq], P[posp], P[(posp + 1) % n])); posp = (posp + 1) % n; } return ans; } Point P[N], Q[N], p[N], q[N]; int main() { int n, m; while(scanf("%d %d", &n, &m) && n + m) { rep(i, 0, n - 1) scanf("%lf %lf", &p[i].x, &p[i].y); rep(i, 0, m - 1) scanf("%lf %lf", &q[i].x, &q[i].y); n = ConvexHull(p, n, P); m = ConvexHull(q, m, Q); double ans = min(Rotating_calipers(P, n, Q, m), Rotating_calipers(Q, m, P, n)); printf("%.5f\n", ans); } return 0; }