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

#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const double EPS = 1e-9;

struct P3 {
  double x, y, z;
  P3() {}
  P3(double x, double y, double z) : x(x), y(y), z(z) {}
};

P3 operator+(const P3 &a, const P3 &b) {
  return P3(a.x + b.x, a.y + b.y, a.z + b.z);
}

P3 operator-(const P3 &a, const P3 &b) {
  return P3(a.x - b.x, a.y - b.y, a.z - b.z);
}

double dot(const P3 &p, const P3 &q) {
  return p.x * q.x + p.y * q.y + p.z * q.z;
}

P3 cross(const P3 &a, const P3 &b) {
  return P3(a.y * b.z - b.y * a.z, a.z * b.x - b.z * a.x,
            a.x * b.y - b.x * a.y);
}

double abs(const P3 &p) { return sqrt(dot(p, p)); }

istream &operator>>(istream &is, P3 &p) { return is >> p.x >> p.y >> p.z; }

// line(a, b) and c
double dist(const P3 &a, const P3 &b, const P3 &c) {
  if (dot(c - a, b - a) <= 0)
    return abs(c - a);
  if (dot(c - b, a - b) <= 0)
    return abs(c - b);
  return abs(cross(c - a, b - a)) / abs(b - a);
}

struct Sphere {
  P3 center;
  double r;
};

bool intersect(const P3 &a, const P3 &b, const Sphere &s) {
  const double d_ca = abs(s.center - a);
  const double d_cb = abs(s.center - b);
  if (d_ca < s.r + EPS && d_cb < s.r + EPS)
    return false;
  return dist(a, b, s.center) < s.r + EPS;
}

bool solve() {
  int N, M, R;
  cin >> N >> M >> R;
  if (!N && !M && !R)
    return false;

  vector<Sphere> balloons(N);
  vector<P3> lights(M);
  vector<double> brightness(M);
  for (int i = 0; i < N; ++i) {
    cin >> balloons[i].center >> balloons[i].r;
  }
  for (int i = 0; i < M; ++i) {
    cin >> lights[i] >> brightness[i];
  }
  P3 target;
  cin >> target;

  vector<bitset<2000>> dependency(M);
  for (int i = 0; i < M; ++i) {   // lights[i]
    for (int j = 0; j < N; ++j) { // balloons[j]
      if (intersect(lights[i], target, balloons[j])) {
        dependency[i][j] = true;
      }
    }
  }
  double ans = 0;
  for (int pat = 0; pat < (1 << M); ++pat) {
    bitset<2000> to_remove;
    for (int i = 0; i < M; ++i) {
      if (pat & (1 << i)) {
        to_remove |= dependency[i];
      }
    }
    if (to_remove.count() > R)
      continue;

    double res = 0;
    for (int i = 0; i < M; ++i) { // lights[i]
      if ((to_remove & dependency[i]) == dependency[i]) {
        // cout << pat << " " << i << endl;
        const P3 d = lights[i] - target;
        res += brightness[i] / dot(d, d);
      }
    }
    ans = max(ans, res);
  }
  printf("%.6f\\n", ans);
  return true;
}

int main() {
  cin.tie(0);
  // ios::sync_with_stdio(0);

  while (solve())
    ;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

struct P3 {
  double x, y, z;
  P3() {}
  P3(double x, double y, double z) : x(x), y(y), z(z) {}
};

P3 operator+(const P3 &a, const P3 &b) {
  return P3(a.x + b.x, a.y + b.y, a.z + b.z);
}

P3 operator-(const P3 &a, const P3 &b) {
  return P3(a.x - b.x, a.y - b.y, a.z - b.z);
}

double dot(const P3 &p, const P3 &q) {
  return p.x * q.x + p.y * q.y + p.z * q.z;
}

P3 cross(const P3 &a, const P3 &b) {
  return P3(a.y * b.z - b.y * a.z, a.z * b.x - b.z * a.x,
            a.x * b.y - b.x * a.y);
}

double abs(const P3 &p) { return sqrt(dot(p, p)); }

istream &operator>>(istream &is, P3 &p) { return is >> p.x >> p.y >> p.z; }

// line(a, b) and c
double dist(const P3 &a, const P3 &b, const P3 &c) {
  P3 v = b - a;
  return abs(cross(v, c - a)) / abs(v);
}

struct Sphere {
  P3 center;
  double r;
};

bool intersect(const P3 &a, const P3 &b, const Sphere &s) {
  if (abs(s.center - a) <= s.r)
    return true;
  if (abs(s.center - b) <= s.r)
    return true;
  const double d = abs(cross(b - a, s.center - a)) / abs(b - a);
  if (d > s.r)
    return false;
  return dot(s.center - a, s.center - a) - d * d <= dot(b - a, b - a);
}

bool solve() {
  int N, M, R;
  cin >> N >> M >> R;
  if (!N && !M && !R)
    return false;

  vector<Sphere> balloons(N);
  vector<P3> lights(M);
  vector<double> brightness(M);
  for (int i = 0; i < N; ++i) {
    cin >> balloons[i].center >> balloons[i].r;
  }
  for (int i = 0; i < M; ++i) {
    cin >> lights[i] >> brightness[i];
  }
  P3 target;
  cin >> target;

  vector<vector<int>> dependency(M);
  for (int i = 0; i < M; ++i) {   // lights[i]
    for (int j = 0; j < N; ++j) { // balloons[j]
      const double d_lc = abs(lights[i] - balloons[j].center);
      const double d_tc = abs(target - balloons[j].center);
      if (d_lc <= balloons[j].r && d_tc <= balloons[j].r)
        continue;
      if (intersect(lights[i], target, balloons[j])) {
        dependency[i].push_back(j);
      }
    }
  }
  double ans = 0;
  for (int pat = 0; pat < (1 << M); ++pat) {
    set<int> to_remove;
    for (int i = 0; i < M; ++i) {
      if (pat & (1 << i)) {
        for (int j : dependency[i]) {
          to_remove.insert(j);
        }
      }
    }
    if (to_remove.size() > R)
      continue;

    double res = 0;
    for (int i = 0; i < M; ++i) { // lights[i]
      bool ok = true;
      for (int j = 0; j < N; ++j) { // balloons[j]
        if (to_remove.count(j))
          continue;
        const double d_lc = abs(lights[i] - balloons[j].center);
        const double d_tc = abs(target - balloons[j].center);
        if (d_lc <= balloons[j].r && d_tc <= balloons[j].r)
          continue;
        if (intersect(lights[i], target, balloons[j])) {
          // cout << pat << " " << i << " " << j << endl;
          ok = false;
          break;
        }
      }
      if (ok) {
        // cout << pat << " " << i << endl;
        const P3 d = lights[i] - target;
        res += brightness[i] / dot(d, d);
      }
    }
    ans = max(ans, res);
  }
  printf("%.6f\\n", ans);
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  while (solve())
    ;
  return 0;
}