Problem p00810 (C++) — buggy submission, verdict: Time Limit Exceeded

//  Super Star Aizu - 1231
//  status:
//  time:
//  main.cpp
//  Created by livin2 on 18/04/20.
//  Copyright © 2017年 livin2. All rights reserved.
//  Note:#
// #include <cstdio>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
// #include <map>
// #include <fstream>
using namespace std;
const int precision = 100000;
const double DZERO = 1e-9;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
struct pos {
  double x, y, z;
  pos() {}
  pos(double x, double y, double z) : x(x), y(y), z(z) {}
  void moveRand(double step);
};
double len(const pos &a, const pos &b) {
  return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) +
              (a.z - b.z) * (a.z - b.z));
}
double Rand() { return (double)(rand() % precision) / precision; }

struct SuperStar {
  int n;
  // double X, Y;
  double maxX = 100.0;
  double Dstep, ans;
  pos ansp;
  static const int pRandNum = 40;
  static const int stepRandNum = 20;
  const double minDstep = 1e-6;
  const double ratio = 0.98;
  vector<pos> P;
  vector<double> Rs;
  vector<pos> ansPs;
  SuperStar(int n);
  pos posRand();
  double getRadius(const pos &p);
  bool isInRange(const pos &p);
  void solve();
};
int main() {
  double x, y;
  int n;
  while (cin >> n && n) {
    SuperStar exp(n);
    exp.solve();
    cout << fixed << setprecision(5) << exp.ans << endl;
  }
  return 0;
}

SuperStar::SuperStar(int n) : n(n), Rs(pRandNum), ansPs(pRandNum) {
  double xx, yy, zz;
  for (int i = 0; i < n; i++) {
    cin >> xx >> yy >> zz;
    P.push_back(pos(xx, yy, zz));
  }
  Dstep = sqrt(2) * maxX / sqrt(n); // sqrt (2) *...
}
void pos::moveRand(double step) {
  double ang = Rand() * 2 * PI;
  z += step * sin(ang);
  double xy = step * cos(ang);
  ang = Rand() * 2 * PI;
  x += xy * cos(ang);
  y += xy * sin(ang);
}
pos SuperStar::posRand() {
  double x = maxX * Rand();
  double y = maxX * Rand();
  double z = maxX * Rand();
  return pos(x, y, z);
}

double SuperStar::getRadius(const pos &p) {
  double res = 0;
  for (int i = 0; i < P.size(); i++)
    res = max(res, len(P[i], p));
  return res;
}

bool SuperStar::isInRange(const pos &p) {
  if (p.x > DZERO && p.x - maxX <= DZERO && p.y > DZERO &&
      p.y - maxX <= DZERO && p.z > DZERO && p.z - maxX <= DZERO)
    return true;
  return false;
}

void SuperStar::solve() {
  // init
  for (int i = 0; i < pRandNum; i++) {
    ansPs[i] = posRand();
    Rs[i] = getRadius(ansPs[i]);
  }
  // SA
  while (Dstep > minDstep) {
    for (int i = 0; i < pRandNum; i++) {
      pos cur = ansPs[i];
      for (int j = 0; j < stepRandNum; j++) {
        cur.moveRand(Dstep);
        if (isInRange(cur)) {
          double rad = getRadius(cur);
          if (rad < Rs[i]) {
            Rs[i] = rad;
            ansPs[i].x = cur.x;
            ansPs[i].y = cur.y;
            ansPs[i].z = cur.z;
          }
        }
      }
    }
    Dstep *= ratio;
  }
  //
  ans = INF;
  ansp = ansPs[0];
  for (int i = 0; i < pRandNum; i++) {
    if (Rs[i] < ans) {
      ans = Rs[i];
      ansp = ansPs[i];
    }
  }
}


Error: TLE

Fixed submission:

/*#include <iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int Max=110;
const int inf=1000000;
const double eps=1e-8;
int n;
double T;
double posx,posy,posz;
double dis;
double a[Max],b[Max],c[Max];
double getdis(double aa,double bb,double cc,int i)
{
    return sqrt((aa-a[i])*(aa-a[i])+(bb-b[i])*(bb-b[i])+(cc-c[i])*(cc-c[i]));
}
int main()
{
    while(scanf("%d",&n)!=-1)
    {
        if(n==0)break;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf",&a[i],&b[i],&c[i]);
        }
        T=100;
        posx=0;
        posy=0;
        posz=0;
        int flag=1;
        dis=inf;
        while(T>eps)
        {
            for(int i=1;i<=n;i++)
            {
                if(getdis(posx,posy,posz,i)>getdis(posx,posy,posz,flag))
                {
                    flag=i;
                }
            }
            double d=getdis(posx,posy,posz,flag);
            dis=min(dis,d);
            posx=posx+(a[flag]-posx)/d*T;
            posy=posy+(b[flag]-posy)/d*T;
            posz=posz+(c[flag]-posz)/d*T;
            T=T*0.9;
        }
        printf("%.5f\\n",dis);
    }
    return 0;
}*/
#include <bits/stdc++.h>
using namespace std;
const int Max = 40;
const double eps = 1e-8;
double a[Max], b[Max], c[Max];
double x, y, T;
double posx, posy, posz;
double dis;
int n;
int dx[] = {1, 1, 1, 1, 1,  1,  1,  1,  1,  0,  0,  0,  0,
            0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int dy[] = {1, 1,  1,  0,  0, 0, -1, -1, -1, 1, 1,  1,  0,
            0, -1, -1, -1, 1, 1, 1,  0,  0,  0, -1, -1, -1};
int dz[] = {1,  0, -1, 1,  0, -1, 1,  0, -1, 1,  0, -1, 1,
            -1, 1, 0,  -1, 1, 0,  -1, 1, 0,  -1, 1, 0,  -1};
double getdis(double aa, double bb, double cc, int i) {
  return sqrt((aa - a[i]) * (aa - a[i]) + (bb - b[i]) * (bb - b[i]) +
              (cc - c[i]) * (cc - c[i]));
}
int main() {
  while (scanf("%d", &n) != -1) {
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++) {
      scanf("%lf%lf%lf", &a[i], &b[i], &c[i]);
    }
    T = 200;
    posx = (rand() % 1000 + 1.0) / 1000 * 100.0;
    posy = (rand() % 1000 + 1.0) / 1000 * 100.0;
    posz = (rand() % 1000 + 1.0) / 1000 * 100.0;
    dis = 0;
    for (int i = 1; i <= n; i++) {
      double len = getdis(posx, posy, posz, i);
      dis = max(dis, len);
    }
    while (T > eps) {
      for (int k = 1; k <= 100; k++) {
        for (int j = 0; j < 26; j++) {
          double tx = posx + (rand() % 1000 + 1.0) / 1000 * T * dx[j];
          double ty = posy + (rand() % 1000 + 1.0) / 1000 * T * dy[j];
          double tz = posy + (rand() % 1000 + 1.0) / 1000 * T * dz[j];
          if (tx < 0 || tx > 100 || ty < 0 || ty > 100 || tz < 0.0 ||
              tz > 100) {
            continue;
          }
          double tdis = 0;
          for (int i = 1; i <= n; i++) {
            double tlen = getdis(tx, ty, tz, i);
            tdis = max(tdis, tlen);
          }
          if (tdis < dis) {
            posx = tx;
            posy = ty;
            posz = tz;
            dis = tdis;
          }
        }
      }
      T = T * 0.99;
    }
    printf("%.5f\\n", dis);
  }
  return 0;
}