/*
题目:Super Star
题目来源:POJ 2069
题目难度:中等偏难
题目内容或思路:
模拟退火
一开始一直徘徊在WA和TLE之间,提交了有四十多次,步长和点数改多了就TLE,
改少了就WA。无奈之下只得网上看别人的代码。发现别人代码中的点不是随机
移动的,而是朝最远的点移动,这时有些明白了,朝最远的点移动应该会更好
一些,但是还是WA。再仔细看了看,发现我是先判移动后的地方是不是更优,
再移动,如果不是更优就不会移动,这样最远的点不变,那个点又不动,这就
造成了很多次无效的循环,这样反而不好。于是去掉了判断更优的条件。
看来模拟退火也不是那么容易的,其中的技巧还是要好好参透一翻啊。
做题日期:2011.3.8
*/
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <bitset>
#include <cmath>
#include <set>
#include <utility>
#include <ctime>
#define sqr(x) ((x)*(x))
using namespace std;
const int N = 33, nt = 1, L = 30;
const double inf = 1e100;
const double eps = 1e-8;
const double pi = acos(-1.0);
int n;
double delta, maxv, minv;
struct vpoint{
double x, y, z, d;
int id;
}vp[N], test[N];
double dis(vpoint a, vpoint b) {
return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y) + sqr(a.z - b.z));
}
void calc(vpoint &p) {
p.d = 0;
for (int i = 0; i < n; ++i) {
double t = dis(p, vp[i]);
if (p.d < t) {
p.d = t;
p.id = i;
}
}
}
void updata(int id) {
double d = test[id].d;
test[id].x += (vp[test[id].id].x - test[id].x) / d * delta;
test[id].y += (vp[test[id].id].y - test[id].y) / d * delta;
test[id].z += (vp[test[id].id].z - test[id].z) / d * delta;
calc(test[id]);
}
void solve() {
maxv = -inf; minv = inf;
for (int i = 0; i < n; ++i) {
scanf("%lf%lf%lf", &vp[i].x, &vp[i].y, &vp[i].z);
maxv = max(maxv, vp[i].x);
maxv = max(maxv, vp[i].y);
minv = min(minv, vp[i].x);
minv = min(minv, vp[i].y);
}
for (int i = 0; i < nt; ++i) {
test[i].x = minv + (rand() % (int(maxv - minv + 1) * 1000)) / 1000.0;
test[i].y = minv + (rand() % (int(maxv - minv + 1) * 1000)) / 1000.0;
test[i].z = minv + (rand() % (int(maxv - minv + 1) * 1000)) / 1000.0;
calc(test[i]);
}
double r = 0.98;
for (delta = 100; delta > eps; delta *= r) {
for (int i = 0; i < nt; ++i)
for (int j = 0; j < L; ++j)
updata(i);
}
double res = inf;
for (int i = 0; i < nt; ++i)
res = min(res, test[i].d);
printf("%lf\n", res);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("D:\\in.txt", "r", stdin);
#endif
while (scanf("%d", &n), n) {
solve();
}
return 0;
}
相关文章: