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

#include <bits/stdc++.h>
#define N 2005
#define M 15
using namespace std;

const double EPS = 1e-8;

// ????¬????
struct P3 {
  double x, y, z;
  P3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
  P3 operator+(const P3 &a) const { return P3(x + a.x, y + a.y, z + a.z); }
  P3 operator-(const P3 &a) const { return P3(x - a.x, y - a.y, z - a.z); }
  P3 operator*(const double &c) const { return P3(x * c, y * c, z * c); }
};

struct L3 {
  P3 a, b;
};

double abs(P3 a) { return sqrt(a.x * a.x + a.y * a.y + a.z * a.z); }

double dot(P3 a, P3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }

// ????????¨???????????¢
double distSP(L3 S, P3 p) {
  double a = dot(S.b - S.a, S.b - S.a);
  double b = dot(S.b - S.a, S.a - p);
  double c = dot(S.a - p, S.a - p);
  double t = -b / a;
  if (t < -EPS)
    return pow(abs(S.a - p), 2);
  if (t > 1 + EPS)
    return pow(abs(S.b - p), 2);
  return a * t * t + 2 * b * t + c;
}

double distPP(P3 a, P3 b) { return abs(a - b); }

int main() {

  while (1) {

    int n, m, r;
    double sx[N], sy[N], sz[N], sr[N];
    double tx[M], ty[M], tz[M], tb[M];
    double ex, ey, ez;
    vector<int> ball[M];

    cin >> n >> m >> r;
    if (!n && !m && !r)
      break;

    for (int i = 0; i < n; i++)
      cin >> sx[i] >> sy[i] >> sz[i] >> sr[i];

    for (int i = 0; i < m; i++)
      cin >> tx[i] >> ty[i] >> tz[i] >> tb[i];

    cin >> ex >> ey >> ez;

    for (int i = 0; i < m; i++) {

      for (int j = 0; j < n; j++) {

        P3 p = P3(sx[j], sy[j], sz[j]);
        P3 a = P3(ex, ey, ez);
        P3 b = P3(tx[i], ty[i], tz[i]);

        if (distPP(a, p) < sr[j] && distPP(b, p) < sr[j])
          continue;

        if (sr[j] * sr[j] > EPS + distSP(L3{a, b}, p))
          ball[i].push_back(j);
      }
    }

    double ans = 0;

    for (int i = 0; i < (1 << m); i++) {

      unordered_set<int> cnt;
      double sum = 0;

      for (int j = 0; j < m; j++) {

        if (!(i & (1 << j)))
          continue;

        double X = tx[j] - ex;
        double Y = ty[j] - ey;
        double Z = tz[j] - ez;

        sum += tb[j] / (X * X + Y * Y + Z * Z);
      }

      if (sum < ans)
        continue;

      for (int j = 0; j < m; j++) {

        if (!(i & (1 << j)))
          continue;

        for (int k = 0; k < ball[j].size(); k++)
          cnt.insert(ball[j][k]);
      }

      if (r >= cnt.size())
        ans = max(ans, sum);
    }

    printf("%.5f\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 2005
#define M 15
using namespace std;
const double INF = 1e+9;
const double EPS = 1e-8;

// ????¬????
struct P3 {
  double x, y, z;
  P3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
  P3 operator+(const P3 &a) const { return P3(x + a.x, y + a.y, z + a.z); }
  P3 operator-(const P3 &a) const { return P3(x - a.x, y - a.y, z - a.z); }
  P3 operator*(const double &c) const { return P3(x * c, y * c, z * c); }
};

struct L3 {
  P3 a, b;
};

double abs(P3 a) { return sqrt(a.x * a.x + a.y * a.y + a.z * a.z); }

double dot(P3 a, P3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }

// ????????¨???????????¢
double distSP(L3 S, P3 p) {
  double a = dot(S.b - S.a, S.b - S.a);
  double b = dot(S.b - S.a, S.a - p);
  double c = dot(S.a - p, S.a - p);
  double t = -b / a;
  if (t < -EPS)
    return abs(S.a - p);
  if (t > 1 + EPS)
    return abs(S.b - p);
  return sqrt(a * t * t + 2 * b * t + c);
}

double distPP(P3 a, P3 b) { return abs(a - b); }

int n, m, r;
double sx[N], sy[N], sz[N], sr[N];
double tx[M], ty[M], tz[M], tb[M];
double ex, ey, ez;
vector<int> ball[M];

int main() {

  while (1) {

    cin >> n >> m >> r;
    if (!n && !m && !r)
      break;

    for (int i = 0; i < n; i++)
      cin >> sx[i] >> sy[i] >> sz[i] >> sr[i];

    for (int i = 0; i < m; i++)
      cin >> tx[i] >> ty[i] >> tz[i] >> tb[i];

    cin >> ex >> ey >> ez;

    for (int i = 0; i < m; i++) {

      for (int j = 0; j < n; j++) {

        P3 p = P3(sx[j], sy[j], sz[j]);
        P3 a = P3(ex, ey, ez);
        P3 b = P3(tx[i], ty[i], tz[i]);
        if (sr[j] > distSP(L3{a, b}, p)) {
          if (distPP(a, p) < sr[j] && distPP(b, p) < sr[j])
            continue;
          ball[i].push_back(j);
        }
      }
    }

    double ans = 0;

    for (int i = 0; i < (1 << m); i++) {

      unordered_set<int> cnt;
      double sum = 0;

      for (int j = 0; j < m; j++) {

        if (!(i & (1 << j)))
          continue;

        double X = tx[j] - ex;
        double Y = ty[j] - ey;
        double Z = tz[j] - ez;

        sum += tb[j] / (X * X + Y * Y + Z * Z);
      }

      if (sum < ans)
        continue;

      for (int j = 0; j < m; j++) {

        if (!(i & (1 << j)))
          continue;

        for (int k = 0; k < ball[j].size(); k++)
          cnt.insert(ball[j][k]);
      }

      if (r == cnt.size())
        ans = max(ans, sum);
    }

    printf("%.8f\\n", ans);

    for (int i = 0; i < m; i++)
      ball[i].clear();
  }
  return 0;
}