最小圆覆盖 [模板] BZOJ 1337&1336

题目描述

给出N个点,让你画一个最小的包含所有点的圆。

输入输出格式

输入格式:

先给出点的个数N,2<=N<=100000,再给出坐标Xi,Yi.(-10000.0<=xi,yi<=10000.0)

输出格式:

输出圆的半径,及圆心的坐标,保留10位小数

输入输出样例

输入样例#1: 复制
6
8.0 9.0
4.0 7.5
1.0 2.0
5.1 8.7
9.0 2.0
4.5 1.0
输出样例#1: 复制
5.0000000000
5.0000000000 5.0000000000

说明

5.00 5.00 5.0

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 400005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-11
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
    int x = 0;
    char c = getchar();
    bool f = false;
    while (!isdigit(c)) {
        if (c == '-') f = true;
        c = getchar();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f ? -x : x;
}


ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (!b) {
        x = 1; y = 0; return a;
    }
    ans = exgcd(b, a%b, x, y);
    ll t = x; x = y; y = t - a / b * y;
    return ans;
}
*/


struct node {
    double x, y;
}pt[maxn];

node o;
int n;
double r;

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 dt(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);
    r = dis(o, p1);
}

int main() {
//	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    rdint(n);
    for (int i = 1; i <= n; i++) {
        rdlf(pt[i].x); rdlf(pt[i].y);
    }
    random_shuffle(pt + 1, pt + 1 + n);
    o = pt[1]; r = 0;
    for (int i = 2; i <= n; i++) {
        if (dis(pt[i], o) > r + eps) {
            o = pt[i]; r = 0;
            for (int j = 1; j <= i - 1; j++) {
                if (dis(o, pt[j]) > r + eps) {
                    o.x = (pt[i].x + pt[j].x) / 2.0;
                    o.y = (pt[i].y + pt[j].y) / 2.0;
                    r = dis(o, pt[j]);
                    for (int k = 1; k <= j - 1; k++) {
                        if (dis(o, pt[k]) > r + eps) {
                            dt(pt[i], pt[j], pt[k]);
                        }
                    }
                }
            }
        }
    }
    printf("%.10lf\n%.10lf %.10lf", 1.0*r, o.x, o.y);
    return 0;
}

 

posted @ 2019-01-28 15:39  NKDEWSM  阅读(239)  评论(0编辑  收藏  举报