最小圆覆盖 gym-102006 I

Posted cjlhy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了最小圆覆盖 gym-102006 I相关的知识,希望对你有一定的参考价值。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std;

const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1.1e-4;
const  double PI = acos(-1);

struct Point {
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) { }
};
typedef Point Vector;

int dcmp(double x) {
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}

Point operator + (Vector A, Vector B) {return Point(A.x + B.x, A.y + B.y);}
Point operator - (Vector A, Vector B) {return Point(A.x - B.x, A.y - B.y);}
Point operator * (Vector A, double p) {return Point(A.x * p, A.y * p);}
Point operator / (Vector A, double p) {return Point(A.x / p, A.y / p);}
bool operator < (const Vector &A, const Vector &B) {return A.y < B.y || (A.y == B.y && A.x < B.x);}
bool operator == (const Vector &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(Dot(A, A));}
double Angle(Vector A, Vector B) {return acos(Dot(A, B) / Length(A) / Length(B));}
double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;}
double Area2(Point A, Point B, Point C) {return Cross(B - A, C - A);}

Vector Rotate(Vector A, double rad) {
    return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}

double dist(const Point& a, const Point &b) {
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}

int n;
double r, R;
Point p[N];

Point GetCircleCenter(Point A, Point B, Point C) {
    Point o;
    double a1 = B.x-A.x, b1 = B.y-A.y, c1 = (a1*a1+b1*b1)/2;
    double a2 = C.x-A.x, b2 = C.y-A.y, c2 = (a2*a2+b2*b2)/2;
    double d = a1*b2-a2*b1;
    o.x=A.x+(c1*b2-c2*b1)/d;
    o.y=A.y+(a1*c2-a2*c1)/d;
    return o;
}

void MinPointCoverByCircle(Point *p, int n, Point &o, double &r) {
    random_shuffle(p, p+n);
    o = p[0], r = 0;
    for(int i = 1; i < n; i++) {
        if(dist(p[i], o) > r + eps) {
            o = p[i]; r = 0;
            for(int j = 0; j < i; j++) {
                if(dist(p[j], o) > r + eps) {
                    o.x = (p[i].x+p[j].x)/2;
                    o.y = (p[i].y+p[j].y)/2;
                    r = dist(p[j], o);
                    for(int k = 0; k < j; k++) {
                        if(dist(p[k], o) > r + eps) {
                            o = GetCircleCenter(p[i], p[j], p[k]);
                            r = dist(p[i], o);
                        }
                    }
                }
            }
        }
    }
}

int main() {
//    freopen("robots.in", "r", stdin);
    int T; scanf("%d", &T);
    while(T--) {
        scanf("%d%lf%lf", &n, &R, &r); n++;
        p[0] = Point(0, 0);
        for(int i = 1; i < n; i++) {
            double x, y; scanf("%lf%lf", &x, &y);
            p[i] = p[i-1] + Point(x, y);
        }
        sort(p, p+n);
        n = unique(p, p+n)-p;
        Point o;
        MinPointCoverByCircle(p, n, o, r);
        printf("%.12f %.12f
", -o.x, -o.y);
    }
    return 0;
}
/*
*/

 

以上是关于最小圆覆盖 gym-102006 I的主要内容,如果未能解决你的问题,请参考以下文章

bzoj 1337 最小圆覆盖

Java案例:最小覆盖圆问题

Java案例:最小覆盖圆问题

Java案例:最小覆盖圆问题

[matlab] 10.最小覆盖圆

最小覆盖圆的神奇算法及例题