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

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
double EPS = 1e-10;
double INF = 1LL << 55;
double PI = acos(-1);
#define equals(a, b) (fabs(a - b) < EPS)
#define lt(a, b) (a - b < -EPS)
#define le(a, b) (lt(a, b) || equals(a, b))
struct Point3D {
  double x, y, z;
  Point3D() {}
  Point3D(double x, double y, double z) : x{x}, y{y}, z{z} {}

  Point3D operator+(const Point3D &p) const {
    return Point3D(x + p.x, y + p.y, z + p.z);
  }

  Point3D operator-(const Point3D &p) const {
    return Point3D(x - p.x, y - p.y, z - p.z);
  }

  Point3D operator*(double k) const { return Point3D(x * k, y * k, z * k); }

  Point3D operator/(double k) const { return Point3D(x / k, y / k, z / k); }
};

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

double norm(const Point3D &p) { return dot(p, p); }

double norm(const Point3D &a, const Point3D &b) {
  return (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 abs(const Point3D &p) { return sqrt(norm(p)); }

double dist(const Point3D &a, const Point3D &b) { return abs(a - b); }
struct Segment {
  Point3D s, t;
  Segment() {}
  Segment(Point3D s, Point3D t) : s{s}, t{t} {}
};
double distance_SP(const Segment &s, const Point3D &p) {
  double a = dot(s.t - s.s, s.t - s.s);
  double b = dot(s.t - s.s, s.s - p);
  double c = dot(s.s - p, s.s - p);
  double t = -b / a;
  if (t < 0)
    return abs(s.s - p);
  if (t > 1)
    return abs(s.t - p);
  return sqrt(a * t * t + 2 * b * t + c);
}
double x, y, z, r[2001], po[15], ans, num[15];
int n, m, p, o = 0;
Point3D X;
vector<Point3D> v, c;
vector<int> t[15];
bool used[15];
void calc() {
  double score = 0;
  set<int> res;
  r(i, m) if (used[i]) score += num[i];
  if (score < ans)
    return;
  r(i, m) if (used[i]) { r(j, t[i].size()) res.insert(t[i][j]); }
  if (res.size() <= p)
    ans = score;
}
void dfs(int d) {
  if (d == m)
    calc();
  else {
    used[d] = 0;
    dfs(d + 1);
    used[d] = 1;
    dfs(d + 1);
  }
}
int main() {
  while (cin >> n >> m >> p, n) {
    v.clear();
    c.clear();
    r(i, 15) t[i].clear();
    r(i, n) {
      cin >> x >> y >> z >> r[i];
      v.push_back(Point3D(x, y, z));
    }
    r(i, m) {
      cin >> x >> y >> z >> po[i];
      c.push_back(Point3D(x, y, z));
    }
    cin >> x >> y >> z;
    X = Point3D(x, y, z);
    r(i, m) r(j, n) {
      if (dist(c[i], v[j]) <= r[j] && dist(X, v[j]) <= r[j])
        continue;
      if (distance_SP(Segment(c[i], X), v[j]) <= r[j])
        t[i].push_back(j);
    }
    r(i, m) num[i] = po[i] / ((c[i].x - X.x) * (c[i].x - X.x) +
                              (c[i].y - X.y) * (c[i].y - X.y) +
                              (c[i].z - X.z) * (c[i].z - X.z));
    ans = 0;
    dfs(0);
    printf("%.4f\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
double EPS = 1e-10;
double INF = 1LL << 55;
double PI = acos(-1);
#define equals(a, b) (fabs(a - b) < EPS)
#define lt(a, b) (a - b < -EPS)
#define le(a, b) (lt(a, b) || equals(a, b))
struct Point3D {
  double x, y, z;
  Point3D() {}
  Point3D(double x, double y, double z) : x{x}, y{y}, z{z} {}

  Point3D operator+(const Point3D &p) const {
    return Point3D(x + p.x, y + p.y, z + p.z);
  }

  Point3D operator-(const Point3D &p) const {
    return Point3D(x - p.x, y - p.y, z - p.z);
  }

  Point3D operator*(double k) const { return Point3D(x * k, y * k, z * k); }

  Point3D operator/(double k) const { return Point3D(x / k, y / k, z / k); }
};

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

double norm(const Point3D &p) { return dot(p, p); }

double norm(const Point3D &a, const Point3D &b) {
  return (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 abs(const Point3D &p) { return sqrt(norm(p)); }

double dist(const Point3D &a, const Point3D &b) { return abs(a - b); }
struct Segment {
  Point3D s, t;
  Segment() {}
  Segment(Point3D s, Point3D t) : s{s}, t{t} {}
};
double distance_SP(const Segment &s, const Point3D &p) {
  double a = dot(s.t - s.s, s.t - s.s);
  double b = dot(s.t - s.s, s.s - p);
  double c = dot(s.s - p, s.s - p);
  double t = -b / a;
  if (t < 0)
    return abs(s.s - p);
  if (t > 1)
    return abs(s.t - p);
  return sqrt(a * t * t + 2 * b * t + c);
}
double x, y, z, r[2001], po[15], ans;
int n, m, p, o = 0;
Point3D X;
vector<Point3D> v, c;
vector<int> t[15];
bool used[15];
void calc() {
  double score = 0;
  set<int> res;
  r(i, m) if (used[i]) {
    score += po[i] / ((c[i].x - X.x) * (c[i].x - X.x) +
                      (c[i].y - X.y) * (c[i].y - X.y) +
                      (c[i].z - X.z) * (c[i].z - X.z));
    r(j, t[i].size()) res.insert(t[i][j]);
  }
  if (res.size() <= p)
    ans = max(ans, score);
  res.clear();
}
void dfs(int d) {
  if (d == m)
    calc();
  else {
    used[d] = 0;
    dfs(d + 1);
    used[d] = 1;
    dfs(d + 1);
  }
}
int main() {
  while (cin >> n >> m >> p, n) {
    v.clear();
    c.clear();
    r(i, 15) t[i].clear();
    r(i, n) {
      cin >> x >> y >> z >> r[i];
      v.push_back(Point3D(x, y, z));
    }
    r(i, m) {
      cin >> x >> y >> z >> po[i];
      c.push_back(Point3D(x, y, z));
    }
    cin >> x >> y >> z;
    X = Point3D(x, y, z);
    r(i, m) r(j, n) {
      if (dist(c[i], v[j]) <= r[j] && dist(X, v[j]) <= r[j])
        continue;
      if (distance_SP(Segment(c[i], X), v[j]) <= r[j])
        t[i].push_back(j);
    }
    ans = 0;
    dfs(0);
    printf("%.9f\\n", ans);
  }
}