• POJ 3608 Bridge Across Islands (两凸包间最短距离 + 旋转卡壳)


    题目:传送门

    经典题

    代码大部分参考了: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
    #define make make_pair
    #define INF INT_MAX
    #define inf LLONG_MAX
    #define PI acos(-1)
    using namespace std;
    
    const int N = 5e5 + 5;
    
    struct Point {
        double x, y;
        Point(double x = 0, double y = 0) : x(x), y(y) { } /// 构造函数
    };
    
    typedef Point Vector;
    /// 向量+向量=向量, 点+向量=向量
    Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
    ///点-点=向量
    Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
    ///向量*数=向量
    Vector operator * (Vector A, double p) { return Vector(A.x * p, A.y * p); }
    ///向量/数=向量
    Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / 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; } /// 点积
    double Length(Vector A) { return sqrt(1.0 * Dot(A, A)); } /// 计算向量长度
    double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
    double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积
    
    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 PTS(Point p, Point A, Point B) { /// 求点 p 到线段 AB 的最短距离
        if(A == B) return Length(p - A);
        Point v1 = B - A, v2 = p - A, v3 = p - B;
        if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
        else if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
        else return fabs(Cross(v1, v2)) / Length(v1);
    }
    /* 平行线段 a1a2 和 b1b2 的距离 */
    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 = Cross(P[(posp + 1) % n] - P[posp], Q[posq] - P[posp]) - Cross(P[(posp + 1) % n] - P[posp], Q[(posq + 1) % m] - P[posp])) < 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
    ", ans);
    
        }
        return 0;
    }
    一步一步,永不停息
  • 相关阅读:
    jquery 建议编辑器
    开发中可能会用到的几个 jQuery 小提示和技巧
    Httpsqs的安装以及安装过程错误的解决方法 转
    ajax加载模块实时刷新的原理
    好用的php类库和方法
    js中masonry与infinitescroll结合 形成瀑布流
    网站架构从无到有
    可扩展Web架构与分布式系统
    JSONP跨域的原理解析
    你写的前端到底用没用到这些
  • 原文地址:https://www.cnblogs.com/Willems/p/12504847.html
Copyright © 2020-2023  润新知