1. 题目描述
有n个点,求能覆盖这n个点的半径最小的圆的圆心及半径。

2. 基本思路
算法模板http://soft.cs.tsinghua.edu.cn/blog/?q=node/1066定义Di表示相对于P[1]和P[i]组成的最小覆盖圆,如果P[2..i-1]都在这个圆内,那么当前的圆心和半径即为最优解。
如果P[j]不在这个圆内,那么P[j]一定在新的最小覆盖圆的边界上即P[1]、P[j]、P[i]组成的圆。
因为三点可以确定一个圆,因此只需要不断的找到不满足的P[j],进而更新最优解即可。
其实就是三层循环,不断更新最优解。然而,这个算法的期望复杂度是O(n)。这个比较难以理解。

3. 代码

 /* 3007 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
double x, y;
} Point; const double eps = 1e-;
const int maxn = ;
Point P[maxn];
Point o;
double r;
int n; double Length(Point a, Point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double Cross(Point a, Point b, Point c) {
return (c.x-a.x)*(b.y-a.y) - (c.y-a.y)*(b.x-a.x);
} Point Intersect(Point a, Point b, Point c, Point d) {
Point ret = a;
double t = ((a.x - c.x) * (c.y - d.y) - (a.y - c.y) * (c.x - d.x)) /
((a.x - b.x) * (c.y - d.y) - (a.y - b.y) * (c.x - d.x));
ret.x += (b.x - a.x) * t;
ret.y += (b.y - a.y) * t;
return ret;
} Point circumcenter(Point a, Point b, Point c) {
Point ua, ub, va, vb; ua.x = (a.x + b.x) / 2.0;
ua.y = (a.y + b.y) / 2.0;
ub.x = ua.x - a.y + b.y;
ub.y = ua.y + a.x - b.x; va.x = (a.x + c.x) / 2.0;
va.y = (a.y + c.y) / 2.0;
vb.x = va.x - a.y + c.y;
vb.y = va.y + a.x - c.x;
return Intersect(ua, ub, va, vb);
} void min_center() {
o = P[];
r = ; rep(i, , n) {
if (Length(P[i], o)-r > eps) {
o = P[i];
r = ;
rep(j, , i) {
if (Length(P[j], o)-r > eps) {
o.x = (P[i].x + P[j].x) / ;
o.y = (P[i].y + P[j].y) / ;
r = Length(o, P[j]); rep(k, , j) {
if (Length(P[k], o)-r > eps) {
o = circumcenter(P[i], P[j], P[k]);
r = Length(o, P[k]);
}
}
}
}
}
}
} void solve() {
min_center();
printf("%.2lf %.2lf %.2lf\n", o.x, o.y, r);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d", &n)!=EOF && n) {
rep(i, , n)
scanf("%lf%lf", &P[i].x, &P[i].y);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
04-27 01:28