• [模板]最小圆覆盖


    #include <bits/stdc++.h>
    using namespace std;
    
    const double EPS = 1e-8;
    const int N = 1e5+10;
    
    struct Point{
        double x, y;
    };
    
    int n;
    Point p[N];
    
    bool equals(double a, double b){
        return fabs(a-b) < EPS;
    }
    
    bool greater_than(double a, double b){
        return a-b > EPS;
    }
    
    double dis(Point A, Point B){
        return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
    }
    
    // 求三角形的外心:中垂线的交点
    Point circum_center(Point A, Point B, Point C){
        Point ret;
        double a1 = B.x - A.x;
        double b1 = B.y - A.y;
        double c1 = (a1*a1 + b1*b1)/2;
    
        double a2 = C.x - A.x;
        double b2 = C.y - A.y;
        double c2 = (a2*a2 + b2*b2)/2;
    
        double d = a1*b2 - a2*b1;
    
        ret.x = A.x + (c1*b2-c2*b1)/d;
        ret.y = A.y + (a1*c2-a2*c1)/d;
    
        return ret;
    }
    
    // 
    void min_cover_circle(Point& c, double& r){
        random_shuffle(p, p+n);
        c = p[0];
        r = 0;
        for(int i = 1; i < n; ++i){
            if(greater_than(dis(c, p[i]), r)){ // the first point
                c = p[i];
                r = 0;
                for(int j = 0; j < i; ++j){ // the second point
                    if(greater_than(dis(c, p[j]), r)){
                        c.x = (p[i].x + p[j].x) / 2;
                        c.y = (p[i].y + p[j].y) / 2;
                        r = dis(c, p[j]);
                        for(int k = 0; k < j; ++k){ // the third point
                            if(greater_than(dis(c, p[k]), r)){
                                c = circum_center(p[i], p[j], p[k]);
                                r = dis(p[i], c);
                            }
                        }
                    }
                }
            }
        }
    }
    
    int main(){
        scanf("%d", &n);
        for(int i = 0; i < n; ++i){
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        Point c;
        double r;
        min_cover_circle(c, r);
        printf("%.8f", r);
    
        // system("pause");
        return 0;
    }
    
    
    ---- suffer now and live the rest of your life as a champion ----
  • 相关阅读:
    关于树论【动态树问题(LCT)】
    caioj1462: 【EXKMP】回文串
    Node.js 文件上传 cli tools
    VuePress & Markdown Slot
    npm config set registry
    Front End Frameworks Trending 2021
    如何使用 VuePress 搭建一个 element-ui 风格的文档网站
    Semantic Pull Requests All In One
    [POJ2559]Largest Rectangle in a Histogram
    [POJ3253]Fence Repair
  • 原文地址:https://www.cnblogs.com/popodynasty/p/14505544.html
Copyright © 2020-2023  润新知