HDU 3007 Buried memory (最小圆覆盖 凸包解法)

77 篇文章 8 订阅
22 篇文章 0 订阅

Buried memory

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3135    Accepted Submission(s): 1709


Problem Description
Each person had do something foolish along with his or her growth.But,when he or she did this that time,they could not predict that this thing is a mistake and they will want this thing would rather not happened.
The world king Sconbin is not the exception.One day,Sconbin was sleeping,then swakened by one nightmare.It turned out that his love letters to Dufein were made public in his dream.These foolish letters might ruin his throne.Sconbin decided to destroy the letters by the military exercises's opportunity.The missile is the best weapon.Considered the execution of the missile,Sconbin chose to use one missile with the minimum destruction.
Sconbin had writen N letters to Dufein, she buried these letters on different places.Sconbin got the places by difficult,he wants to know where is the best place launch the missile,and the smallest radius of the burst area. Let's help Sconbin to get the award.
 
Input
There are many test cases.Each case consists of a positive integer N(N<500,^V^,our great king might be a considerate lover) on a line followed by N lines giving the coordinates of N letters.Each coordinates have two numbers,x coordinate and y coordinate.N=0 is the end of the input file.
 
Output
For each case,there should be a single line in the output,containing three numbers,the first and second are x and y coordinates of the missile to launch,the third is the smallest radius the missile need to destroy all N letters.All output numbers are rounded to the second digit after the decimal point.
 
Sample Input
  
  
3 1.00 1.00 2.00 2.00 3.00 3.00 0
 
Sample Output
  
  
2.00 2.00 1.41
 
Source
2009 Multi-University Training Contest 11 - Host by HRBEU

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007

题目大意:求n个点的最小圆覆盖

题目分析:先求凸包,然后枚举凸包上的三点求包含这个三角形的最小圆,注意特判n为1和2的情况
三角形外接圆圆心坐标公式(有点恐怖):三角形外接圆圆心公式
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int const MAX = 505;
struct POINT {
    double x, y;
}p[MAX], stk[MAX], base, ansp;
int n, top;
double ans;

double getDist(POINT p1, POINT p2) {
    return sqrt(1.0 * (p2.x - p1.x) * (p2.x - p1.x) + 1.0 * (p2.y - p1.y) * (p2.y - p1.y));
}

double getCross(POINT p0, POINT p1, POINT p2) {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

bool cmp(POINT p1, POINT p2) {
    if (getCross(base, p1, p2) == 0) {
        return getDist(base, p1) < getDist(base, p2);
    }
    if (getCross(base, p1, p2) > 0) {
        return true;
    } 
    return false;
}

void getBase() {
    scanf("%lf %lf", &p[0].x, &p[0].y);
    base.x = p[0].x;
    base.y = p[0].y;
    int pos = 0;
    for (int i = 1; i < n; i ++) {
        scanf("%lf %lf", &p[i].x, &p[i].y);
        if (p[i].y < base.y || (p[i].y == base.y && p[i].x < base.x)) {
            base.x = p[i].x;
            base.y = p[i].y;
            pos = i; 
        }
    }
    swap(p[0], p[pos]);
}

void getConvex() {
    stk[0] = p[0];
    stk[1] = p[1];
    top = 1;
    for (int i = 2; i < n; i ++) {
        while (top > 0 && getCross(stk[top - 1], stk[top], p[i]) <= 0) {
            top --;
        }
        stk[++ top] = p[i];
    }
}

POINT getCircumcenter(POINT a, POINT b, POINT c) {
    POINT res;   
    double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1 * a1 + b1 * b1) / 2.0;  
    double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2 * a2 + b2 * b2) / 2.0;  
    double d = a1 * b2 - a2 * b1;  
    res.x = a.x + (c1 * b2 - c2 * b1) / d;  
    res.y = a.y + (a1 * c2 - a2 * c1) / d;  
    return res;   
} 

bool judge(double a, double b, double c){
    return a * a + b * b < c * c;
}

void calR(POINT p0, POINT p1, POINT p2) {
    double p0p1 = getDist(p0, p1);
    double p0p2 = getDist(p0, p2);
    double p1p2 = getDist(p1, p2);
    if (judge(p0p1, p0p2, p1p2) || judge(p0p1, p1p2, p0p2) || judge(p0p2, p1p2, p0p1)) {
        double maxLen = max(p0p1, max(p0p2, p1p2));
        if (maxLen / 2.0 > ans) {
            ans = max(p0p1, max(p0p2, p1p2)) / 2.0;
            if (maxLen == p0p1) {
                ansp.x = (p0.x + p1.x) / 2.0;
                ansp.y = (p0.y + p1.y) / 2.0;
            }
            if (maxLen == p1p2) {
                ansp.x = (p1.x + p2.x) / 2.0;
                ansp.y = (p1.y + p2.y) / 2.0;
            }
            if (maxLen == p0p2) {
                ansp.x = (p0.x + p2.x) / 2.0;
                ansp.y = (p0.y + p2.y) / 2.0;
            }
        }
    }
    else {
        POINT tmpP = getCircumcenter(p0, p1, p2);
        double tmpR = getDist(p2, tmpP);
        if (tmpR > ans) {
            ans = tmpR;
            ansp = tmpP;
        }
    }
}

int main() {
    while(scanf("%d", &n) && n) {
        getBase();
        if (n == 1) {
            printf("%.2f %.2f 0.00\n", p[0].x, p[0].y);
            continue;
        }
        sort(p + 1, p + n, cmp);
        getConvex();
        if (top == 1) {
            ansp.x = (stk[0].x + stk[1].x) / 2.0;
            ansp.y = (stk[0].y + stk[1].y) / 2.0;
            ans = getDist(stk[0], stk[1]) / 2.0;
            printf("%.2f %.2f %.2f\n", ansp.x, ansp.y, ans);
            continue;
        }
        ans = 0;
        for (int i = 0; i <= top; i ++) {
            for (int j = i + 1; j <= top; j ++) {
                for (int k = j + 1; k <= top; k ++) {
                    calR(stk[i], stk[j], stk[k]);
                }
            }
        }
        printf("%.2f %.2f %.2f\n", ansp.x, ansp.y, ans);
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值