最小圆覆盖

#include<bits/stdc++.h> 
#define N 5010
using namespace std;
struct node
{
    double x,y,z;
} b[N],a[N];
node O;
int n;
double R, ans1, ans2, ans3;
double sqr(double x) { return x * x; }
double dis(node x, node y)
{
    return sqrt(sqr(x.x - y.x) + sqr(x.y - y.y));
}
bool incircle(node x)
{
    if (dis(O, x) <= R)
        return true;
    return false;
}
node solve(double a, double b, double c, double d, double e, double f)
{
    double y = (f * a - c * d) / (b * d - e * a);
    double x = (f * b - c * e) / (a * e - b * d);
    return (node){x, y};    
}
double Solve1()
{
    for (int i = 1; i <= n; i++)
        b[i].x = a[i].x, b[i].y = a[i].y;
    random_shuffle(b + 1, b + n + 1);
    R = 0;
    for (int i = 1; i <= n; i++)
        if (!incircle(b[i]))
        {
            O.x = b[i].x;
            O.y = b[i].y;
            R = 0;
            for (int j = 1; j < i; j++)
                if (!incircle(b[j]))
                {
                    O.x = (b[i].x + b[j].x) / 2;
                    O.y = (b[i].y + b[j].y) / 2;
                    R = dis(O, b[i]);
                    for (int k = 1; k < j; k++)
                        if (!incircle(b[k]))
                        {
                            O = solve(
                                b[i].x - b[j].x, b[i].y - b[j].y, (sqr(b[j].x) + sqr(b[j].y) - sqr(b[i].x) - sqr(b[i].y)) / 2,
                                b[i].x - b[k].x, b[i].y - b[k].y, (sqr(b[k].x) + sqr(b[k].y) - sqr(b[i].x) - sqr(b[i].y)) / 2);
                            R = dis(b[i], O);
                        }
                }
        }
    return R;
}
double Solve2()
{
    for (int i = 1; i <= n; i++)
        b[i].x = a[i].x, b[i].y = a[i].z;
    random_shuffle(b + 1, b + n + 1);
    R = 0;
    for (int i = 1; i <= n; i++)
        if (!incircle(b[i]))
        {
            O.x = b[i].x;
            O.y = b[i].y;
            R = 0;
            for (int j = 1; j < i; j++)
                if (!incircle(b[j]))
                {
                    O.x = (b[i].x + b[j].x) / 2;
                    O.y = (b[i].y + b[j].y) / 2;
                    R = dis(O, b[i]);
                    for (int k = 1; k < j; k++)
                        if (!incircle(b[k]))
                        {
                            O = solve(
                                b[i].x - b[j].x, b[i].y - b[j].y, (sqr(b[j].x) + sqr(b[j].y) - sqr(b[i].x) - sqr(b[i].y)) / 2,
                                b[i].x - b[k].x, b[i].y - b[k].y, (sqr(b[k].x) + sqr(b[k].y) - sqr(b[i].x) - sqr(b[i].y)) / 2);
                            R = dis(b[i], O);
                        }
                }
        }
    return R;
}
double Solve3()
{
    for (int i = 1; i <= n; i++)
        b[i].x = a[i].y, b[i].y = a[i].z;
    random_shuffle(b + 1, b + n + 1);
    R = 0;
    for (int i = 1; i <= n; i++)
        if (!incircle(b[i]))
        {
            O.x = b[i].x;
            O.y = b[i].y;
            R = 0;
            for (int j = 1; j < i; j++)
                if (!incircle(b[j]))
                {
                    O.x = (b[i].x + b[j].x) / 2;
                    O.y = (b[i].y + b[j].y) / 2;
                    R = dis(O, b[i]);
                    for (int k = 1; k < j; k++)
                        if (!incircle(b[k]))
                        {
                            O = solve(
                                b[i].x - b[j].x, b[i].y - b[j].y, (sqr(b[j].x) + sqr(b[j].y) - sqr(b[i].x) - sqr(b[i].y)) / 2,
                                b[i].x - b[k].x, b[i].y - b[k].y, (sqr(b[k].x) + sqr(b[k].y) - sqr(b[i].x) - sqr(b[i].y)) / 2);
                            R = dis(b[i], O);
                        }
                }
        }
    return R;
}
int main(void)
{
    scanf("%d", &n);
    int i, j, k;
    for (i = 1; i <= n; i++)
        scanf("%lf%lf%lf", &a[i].x, &a[i].y, &a[i].z);
    ans1 = Solve1();
    ans2 = Solve2();
    ans3 = Solve3();
    printf("%.10lf\n",2*min(ans1,min(ans2,ans3)));
}
全部评论

相关推荐

10-31 11:57
门头沟学院 Java
点赞 评论 收藏
分享
10-11 17:45
门头沟学院 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务