三分的模板套一套就行了没啥好说的。。
#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
#define ft first
#define sd second
#define all(c) ((c).begin()), ((c).end())
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
const int maxn = 1e5+5;
typedef long long ll;
const ll mod = 1e9+7;
int Case = 1;
int n, m;
struct node{
double x, y, z;
}cc[maxn];
double cal(double x, double y, double z) {
double rt = 0;
for(int i = 1; i <= n; i++) {
double dx = x-cc[i].x, dy = y-cc[i].y, dz = z-cc[i].z;
rt = max(rt, sqrt(dx*dx+dy*dy+dz*dz));
}
return rt;
}
double C2(double x, double y) {
double res = 1e18;
double rz = 100000, lz = -100000;
for(int i = 1; i<= 50; i++) {
double l = lz+(rz-lz)/3, r = lz+(rz-lz)/3*2;
double ls = cal(x, y, l), rs = cal(x, y, r);
res = min(res, min(ls, rs));
if(ls > rs) lz = l;
else rz = r;
}
return res;
}
double C1(double x) {
double res = 1e18;
double ry = 100000, ly = -100000;
for(int i = 1; i<= 50; i++) {
double l = ly+(ry-ly)/3, r = ly+(ry-ly)/3*2;
double ls = C2(x, l), rs = C2(x, r);
res = min(res, min(ls, rs));
if(ls > rs) ly = l;
else ry = r;
}
return res;
}
void solve() {
double res = 1e18;
for(int i = 1; i <= n; i++) {
scanf("%lf%lf%lf", &cc[i].x, &cc[i].y, &cc[i].z);
}
double rx = 100000, lx = -100000;
for(int i = 1; i<= 50; i++) {
double l = lx+(rx-lx)/3, r = lx+(rx-lx)/3*2;
double ls = C1(l), rs = C1(r);
res = min(res, min(ls, rs));
if(ls > rs) lx = l;
else rx = r;
}
printf("%.5lf\n", res);
return;
}
int main() {
//ios::sync_with_stdio(false);cin.tie(0);std::cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
//scanf("%d", &Case);
while(scanf("%d", &n) == 1 && n){
solve();
}
return 0;
}
模拟退火也是板子。。。一发就过了。。
#pragma GCC optimize(2)
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define ft first
#define sd second
#define all(c) ((c).begin()), ((c).end())
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
const int maxn = 1e5+5;
typedef long long ll;
const ll mod = 1e9+7;
int Case = 1;
int n, m;
struct node{
double x, y, z;
}cc[maxn];
double res, t;
const double delta = 0.993;
double resx, resy, resz;
double cal(double X, double Y, double Z) {
double rt = 0;
for(int i = 1; i <= n; i++) {
double deltax = X-cc[i].x, deltay = Y-cc[i].y, deltaz = Z-cc[i].z;
rt = max(rt, sqrt(deltax*deltax+deltay*deltay+deltaz*deltaz));
}
return rt;
}
void simulate_anneal() {
t = 2000;
double x = resx, y = resy, z = resz;
while(t > 1e-14) {
double X = x + ((rand()<<1)-RAND_MAX)*t;
double Y = y + ((rand()<<1)-RAND_MAX)*t;
double Z = z + ((rand()<<1)-RAND_MAX)*t;
double temp = cal(X, Y, Z);
if(temp < res) {
res = temp;
resx = X;resy = Y;resz = Z;
x = X;y = Y;z = Z;
}
else if(exp((res-temp)/t)*RAND_MAX > rand())x = X, y = Y, z = Z;
t *= delta;
}
}
void solve() {
res = 1e18;
resx = 0; resy = 0; resz = 0;
for(int i = 1; i <= n; i++)
{cin>>cc[i].x>>cc[i].y>>cc[i].z; resx += cc[i].x; resy += cc[i].y;resz += cc[i].z;}
resx /= n;resy /= n;resz /= n;
simulate_anneal();
simulate_anneal();
simulate_anneal();
printf("%.5f\n", res);
return;
}
int main() {
srand(19260817);
ios::sync_with_stdio(false);cin.tie(0);std::cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
//scanf("%d", &Case);
while(cin>>n){
solve();
}
return 0;
}