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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
long double x[2000], y[2000], z[2000], r[2000], a[15], b[15], c[15], d[15],
    g[15], X, Y, Z;
int n, m, p;
vector<int> e[15];
long double dst(long double ax, long double ay, long double az, long double bx,
                long double by, long double bz) {
  return sqrtl((ax - bx) * (ax - bx) + (ay - by) * (ay - by) +
               (az - bz) * (az - bz));
}
long double solve(long double ax, long double ay, long double az,
                  long double bx, long double by, long double bz,
                  long double fx, long double fy, long double fz) {
  long double L = (long double)0, R = (long double)1, c1, c2, ret = 1e18;
  long double vx = bx - ax, vy = by - ay, vz = bz - az;
  for (int i = 0; i < 70; i++) {
    c1 = (L + L + R) / 3.0l, c2 = (L + R + R) / 3.0l;
    long double cx = ax + vx * c1, cy = ay + vy * c1, cz = az + vz * c1;
    long double dx = ax + vx * c2, dy = ay + vy * c2, dz = az + vz * c2;
    long double e1 = dst(fx, fy, fz, cx, cy, cz);
    long double e2 = dst(fx, fy, fz, dx, dy, dz);
    if (e1 < e2) {
      R = c2;
    }
    if (e1 > e2) {
      L = c1;
    }
    ret = min(ret, min(e1, e2));
  }
  // cout << ax << ' ' << ay << ' ' << az << ' ' << bx << ' ' << by << ' ' << bz
  // << ' ' << fx << ' ' << fy << ' ' << fz << ' ' << ret << endl;
  return ret;
}
int main() {
  while (true) {
    cin >> n >> m >> p;
    for (int i = 0; i < m; i++)
      e[i].clear();
    if (n + m + p == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i] >> z[i] >> r[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    cin >> X >> Y >> Z;
    for (int i = 0; i < m; i++) {
      g[i] = d[i] / pow(dst(X, Y, Z, a[i], b[i], c[i]), 2.0l);
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (solve(X, Y, Z, a[i], b[i], c[i], x[j], y[j], z[j]) <= r[j]) {
          if (dst(X, Y, Z, x[j], y[j], z[j]) <= r[j] &&
              dst(a[i], b[i], c[i], x[j], y[j], z[j]) <= r[j])
            continue;
          e[i].push_back(j);
        }
      }
    }
    long double maxn = 0.0l;
    for (int i = 0; i < (1 << m); i++) {
      bool used[2000];
      for (int j = 0; j < n; j++)
        used[j] = false;
      for (int j = 0; j < m; j++) {
        if ((i / (1 << j)) % 2 == 0)
          continue;
        for (int k : e[j])
          used[k] = true;
      }
      int cnt = 0;
      for (int j = 0; j < n; j++)
        cnt += used[j];
      if (cnt <= p) {
        long double w = 0;
        for (int j = 0; j < m; j++) {
          if ((i / (1 << j)) % 2 == 1)
            w += g[j];
        }
        maxn = max(maxn, w);
      }
    }
    printf("%.12Lf\\n", maxn);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
long double x[2000], y[2000], z[2000], r[2000], a[15], b[15], c[15], d[15],
    g[15], X, Y, Z;
int n, m, p;
vector<int> e[15];
long double dst(long double ax, long double ay, long double az, long double bx,
                long double by, long double bz) {
  return sqrtl((ax - bx) * (ax - bx) + (ay - by) * (ay - by) +
               (az - bz) * (az - bz));
}
long double solve(long double ax, long double ay, long double az,
                  long double bx, long double by, long double bz,
                  long double fx, long double fy, long double fz) {
  long double L = (long double)0, R = (long double)1, c1, c2, ret = 1e18;
  long double vx = bx - ax, vy = by - ay, vz = bz - az;
  for (int i = 0; i < 70; i++) {
    c1 = (L + L + R) / 3.0l, c2 = (L + R + R) / 3.0l;
    long double cx = ax + vx * c1, cy = ay + vy * c1, cz = az + vz * c1;
    long double dx = ax + vx * c2, dy = ay + vy * c2, dz = az + vz * c2;
    long double e1 = dst(fx, fy, fz, cx, cy, cz);
    long double e2 = dst(fx, fy, fz, dx, dy, dz);
    if (e1 < e2) {
      R = c2;
    }
    if (e1 > e2) {
      L = c1;
    }
    ret = min(ret, min(e1, e2));
  }
  return ret;
}
int main() {
  while (true) {
    cin >> n >> m >> p;
    for (int i = 0; i < m; i++)
      e[i].clear();
    if (n + m + p == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i] >> z[i] >> r[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    cin >> X >> Y >> Z;
    for (int i = 0; i < m; i++) {
      g[i] = d[i] / pow(dst(X, Y, Z, a[i], b[i], c[i]), 2.0l);
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (solve(X, Y, Z, a[i], b[i], c[i], x[j], y[j], z[j]) <= r[j])
          e[i].push_back(j);
      }
    }
    long double maxn = 0.0l;
    for (int i = 0; i < (1 << m); i++) {
      bool used[2000];
      for (int j = 0; j < n; j++)
        used[j] = false;
      for (int j = 0; j < m; j++) {
        if ((i / (1 << j)) % 2 == 0)
          continue;
        for (int k : e[j])
          used[k] = true;
      }
      int cnt = 0;
      for (int j = 0; j < n; j++)
        cnt += used[j];
      if (cnt <= p) {
        long double w = 0;
        for (int j = 0; j < m; j++) {
          if ((i / (1 << j)) % 2 == 1)
            w += g[j];
        }
        maxn = max(maxn, w);
      }
    }
    printf("%.12Lf\\n", maxn);
  }
  return 0;
}