最小圆覆盖(模板)------------------------------------模板

题目描述
Given are N points (xi,yi) in a two-dimensional plane.
Find the minimum radius of a circle such that all the points are inside or on it.

Constraints
·2≤N≤50
·0≤xi≤1000
·0≤yi≤1000
··The given N points are all different.
The values in input are all integers.
输入
Input is given from Standard Input in the following format:

N
x1 y1
:
xN yN
输出
Print the minimum radius of a circle such that all the N points are inside or on it.
Your output will be considered correct if the absolute or relative error from our answer is at most 10−6.
样例输入 Copy
【样例1】
2
0 0
1 0
【样例2】
3
0 0
0 1
1 0
【样例3】
10
10 9
5 9
2 0
0 0
2 7
3 3
2 5
10 0
3 7
1 9
样例输出 Copy
【样例1】
0.500000000000000000
【样例2】
0.707106781186497524
【样例3】
6.726812023536805158
提示
样例1解释
Both points are contained in the circle centered at (0.5,0) with a radius of 0.5.
样例3解释
If the absolute or relative error from our answer is at most 10−6, the output will be considered orrect.

最小圆覆盖模板

#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
 
#include<bits/stdc++.h>//O(n),最小圆覆盖
 
#define ll long long
#define met(a, x) memset(a,x,sizeof(a))
#define inf 0x3f3f3f3f
#define ull unsigned long long
using namespace std;
const double eps = 1e-12;
struct node {
    double x, y;
} s[500005];
node o;//圆心坐标
double ri;//半径
 
double dis(node a, node b) {
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
 
void getr(node p1, node p2, node p3) {//三个点求三角形圆心坐标和半径
    double a, b, c, d, e, f;
    a = p2.y - p1.y;
    b = p3.y - p1.y;
    c = p2.x - p1.x;
    d = p3.x - p1.x;
    f = p3.x * p3.x + p3.y * p3.y - p1.x * p1.x - p1.y * p1.y;
    e = p2.x * p2.x + p2.y * p2.y - p1.x * p1.x - p1.y * p1.y;
    o.x = (a * f - b * e) / (2 * a * d - 2 * b * c);
    o.y = (d * e - c * f) / (2 * a * d - 2 * b * c);
    ri = dis(o, p1);
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> s[i].x >> s[i].y;
    }
    random_shuffle(s + 1, s + n + 1);
    o = s[1];
    ri = 0;
    for (int i = 2; i <= n; i++) {
        if (dis(s[i], o) > ri + eps) {
            o = s[i];
            ri = 0;//第一个点为圆心
            for (int j = 1; j < i; j++) {
                if (dis(o, s[j]) > ri + eps) {
                    o.x = (s[i].x + s[j].x) / 2;
                    o.y = (s[i].y + s[j].y) / 2;
                    ri = dis(o, s[j]);//第一个点和第二个点中点为圆心,距离为直径
                    for (int k = 1; k < j; k++) {
                        if (dis(o, s[k]) > ri + eps) {
                            getr(s[i], s[j], s[k]);//三点定圆
                        }
                    }
                }
            }
        }
    }
    cout << fixed << setprecision(10) << ri << endl;
   // cout << fixed << setprecision(10) << o.x << ' ' << fixed << setprecision(10) << o.y << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值