zoukankan      html  css  js  c++  java
  • [模板]最小圆覆盖

    #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 ----
  • 相关阅读:
    Redhat 7使用CentOS 7的Yum网络源
    指定YUM安装包的体系结构或版本
    CURL常用命令
    VIM技巧之去除代码行号并缩进代码
    VIM 中鼠标选择不选中行号
    linux服务器性能优化
    阻塞,非阻塞,同步,异步
    WEB三层架构与MVC
    mvc与三层结构
    Centos环境下Tomcat启动缓慢
  • 原文地址:https://www.cnblogs.com/popodynasty/p/14505544.html
Copyright © 2011-2022 走看看