hdu 2215(凸包+最小圆覆盖)

/*

 凸包+最小圆覆盖

 枚举任意3点找其最小覆盖圆

(当为钝角三角形时不是外接圆,而是以其最长边为直径的圆)。

 当为外接圆时,半径公式为r=abc/4s;(推导为如下:

 由正弦定理,a/sinA=b/sinB=c/sinC=2R,得sinA=a/(2R),

 又三角形面积公式S=(bcsinA)/2,所以S=(abc)/(4R),故R=(abc)/(4S).

*/



#include <cmath>

#include <cstdio>

#include <cstdlib>

#include <iostream>



using namespace std;



const int N = 105;

const double eps = 1e-8;



struct point {

    int x;

    int y;

}p[N], stack[N];



bool isZero(double x) {

    return (x > 0 ? x : -x) < 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));

}



double crossProd(point A, point B, point C) {

    return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x);

}



int cmp(const void *a, const void *b) {

    point *c = (point *)a;

    point *d = (point *)b;

    double k = crossProd(p[0], *c, *d);

    if (k<eps || (isZero(k)&&dis(p[0], *c)>dis(p[0], *d))) return 1;

    return -1;

}



int Graham(int n) {

    int x = p[0].x;

    int y = p[0].y;

    int mi = 0;

    for (int i=1; i<n; ++i) {

        if (p[i].x<x || (p[i].x==x&&p[i].y<y)) {

            x = p[i].x;

            y = p[i].y;

            mi = i;

        }

    }

    point tmp = p[mi];

    p[mi] = p[0];

    p[0] = tmp;

    qsort(p+1, n-1, sizeof(point), cmp);

    p[n] = p[0];

    for (int i=0; i<3; ++i) stack[i] = p[i];

    int top = 2;

    for (int i=3; i<n; ++i) {

        if (crossProd(stack[top-1], stack[top], p[i])<=eps && top>=2) --top;

        stack[++top] = p[i];

    }

    return top;

}



double max(double a, double b) {

    return a > b ? a : b;

}



int main() {

    int n;

    while (scanf("%d", &n), n) {

        for (int i=0; i<n; ++i) scanf ("%d%d", &p[i].x, &p[i].y);

        if (n == 1) printf ("0.50\n");

        else if (n == 2) printf ("%.2lf\n", dis(p[0], p[1])*0.50+0.50);

        else {

            int top = Graham(n);

            double maxr = -1;

            double a, b, c, r, s;

            for (int i=0; i<top; ++i) {//枚举凸包上的点 

                for (int j=i+1; j<top; ++j) {

                    for (int k=j+1; k<=top; ++k) {

                        a = dis(stack[i], stack[j]);

                        b = dis(stack[i], stack[k]);

                        c = dis(stack[j], stack[k]);

                        if (a*a+b*b<c*c || a*a+c*c<b*b || b*b+c*c<a*a) r = max(max(a, b), c) / 2.0;//钝角 

                        else {

                            s = fabs(crossProd(stack[i], stack[j], stack[k])) / 2.0;

                            r = a * b * c / (s * 4.0);//三角形外接圆公式 

                        }                

                        if (maxr < r) maxr = r;

                    }

                }

            }

            printf ("%.2lf\n", maxr+0.5);

        }

    }

    return 0;

}

 

你可能感兴趣的:(HDU)