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

// Enjoy your stay.

#include <bits/stdc++.h>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;

// geometry library from hos

const double PI = acos(-1);

int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }

struct PT {
  double x, y, z;
  PT() {}
  PT(double x, double y, double z) : x(x), y(y), z(z) {}
  PT operator+(const PT &a) const { return PT(x + a.x, y + a.y, z + a.z); }
  PT operator-(const PT &a) const { return PT(x - a.x, y - a.y, z - a.z); }
  PT operator-() const { return PT(-x, -y, -z); }
  PT operator*(const double &k) const { return PT(x * k, y * k, z * k); }
  PT operator/(const double &k) const { return PT(x / k, y / k, z / k); }
  double abs() const { return sqrt(x * x + y * y + z * z); }
  double abs2() const { return x * x + y * y + z * z; }
  double dot(const PT &a) const { return x * a.x + y * a.y + z * a.z; }
  PT cross(const PT a) const {
    return PT(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x);
  }
  bool operator<(const PT &a) const {
    return (x != a.x) ? (x < a.x) : (y != a.y) ? (y < a.y) : (z < a.z);
  }
  bool operator==(const PT &a) const {
    return (sig(x - a.x) == 0) && (sig(y - a.y) == 0) && (sig(z - a.z) == 0);
  }
};
ostream &operator<<(ostream &os, const PT &a) {
  os << "(" << a.x << ", " << a.y << ", " << a.z << ")";
  return os;
}
PT tri(PT a, PT b, PT c) { return (b - a).cross(c - a); }
double tet(PT a, PT b, PT c, PT d) { return (b - a).cross(c - a).dot(d - a); }

double dLP(PT a, PT b, PT c) { return tri(a, b, c).abs() / (b - a).abs(); }
double dSP(PT a, PT b, PT c) {
  if (sig((b - a).dot(c - a)) <= 0)
    return (c - a).abs();
  if (sig((a - b).dot(c - b)) <= 0)
    return (c - b).abs();
  return tri(a, b, c).abs() / (b - a).abs();
}

// end of library

int N, M, R;
PT S[2010];
double SR[2010];
PT T[16];
double TB[16];
PT E;
int table[2010];

int main2() {
  fill(table, table + N, 0);
  rep(i, N) rep(j, M) {
    if (dSP(E, T[j], S[i]) < SR[i] &&
        !(max((E - S[i]).abs2(), (T[j] - S[i]).abs2()) < SR[i] * SR[i])) {
      table[i] |= 1 << j;
    }
  }

  double ans = 0;
  rep(mask, 1 << M) {
    int rem = 0;
    rep(i, N) {
      if (table[i] & mask)
        rem++;
    }
    if (rem <= R) {
      double res = 0;
      rep(j, M) if (mask >> j & 1) { res += TB[j] / (E - T[j]).abs2(); }
      ans = max(ans, res);
    }
  }
  cout << setprecision(16) << ans << endl;
  return 0;
}

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

  while (cin >> N >> M >> R, N) {
    rep(i, N) cin >> S[i].x >> S[i].y >> S[i].z >> SR[i];
    rep(i, M) cin >> T[i].x >> T[i].y >> T[i].z >> TB[i];
    cin >> E.x >> E.y >> E.z;
    main2();
  }
}

Error: TLE

Fixed submission:

// Enjoy your stay.

#include <bits/stdc++.h>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;

// geometry library from hos

const double PI = acos(-1);

int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }

struct PT {
  double x, y, z;
  PT() {}
  PT(double x, double y, double z) : x(x), y(y), z(z) {}
  PT operator+(const PT &a) const { return PT(x + a.x, y + a.y, z + a.z); }
  PT operator-(const PT &a) const { return PT(x - a.x, y - a.y, z - a.z); }
  PT operator-() const { return PT(-x, -y, -z); }
  PT operator*(const double &k) const { return PT(x * k, y * k, z * k); }
  PT operator/(const double &k) const { return PT(x / k, y / k, z / k); }
  double abs() const { return sqrt(x * x + y * y + z * z); }
  double abs2() const { return x * x + y * y + z * z; }
  double dot(const PT &a) const { return x * a.x + y * a.y + z * a.z; }
  PT cross(const PT a) const {
    return PT(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x);
  }
  bool operator<(const PT &a) const {
    return (x != a.x) ? (x < a.x) : (y != a.y) ? (y < a.y) : (z < a.z);
  }
  bool operator==(const PT &a) const {
    return (sig(x - a.x) == 0) && (sig(y - a.y) == 0) && (sig(z - a.z) == 0);
  }
};
ostream &operator<<(ostream &os, const PT &a) {
  os << "(" << a.x << ", " << a.y << ", " << a.z << ")";
  return os;
}
PT tri(PT a, PT b, PT c) { return (b - a).cross(c - a); }
double tet(PT a, PT b, PT c, PT d) { return (b - a).cross(c - a).dot(d - a); }

double dLP(PT a, PT b, PT c) { return tri(a, b, c).abs() / (b - a).abs(); }
double dSP(PT a, PT b, PT c) {
  if (sig((b - a).dot(c - a)) <= 0)
    return (c - a).abs();
  if (sig((a - b).dot(c - b)) <= 0)
    return (c - b).abs();
  return tri(a, b, c).abs() / (b - a).abs();
}

// end of library

int N, M, R;
PT S[2010];
double SR[2010];
PT T[16];
double TB[16];
PT E;

int main2() {
  double ans = 0;
  rep(mask, 1 << M) {
    int rem = 0;
    rep(i, N) {
      int ok = 1;
      rep(j, M) if (mask >> j & 1) {
        if (dSP(E, T[j], S[i]) < SR[i] &&
            !(max((E - S[i]).abs2(), (T[j] - S[i]).abs2()) < SR[i] * SR[i])) {
          ok = 0;
          break;
        }
      }
      if (!ok)
        rem++;
    }
    if (rem <= R) {
      double res = 0;
      rep(j, M) if (mask >> j & 1) { res += TB[j] / (E - T[j]).abs2(); }
      ans = max(ans, res);
    }
  }
  cout << setprecision(16) << ans << endl;
  return 0;
}

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

  while (cin >> N >> M >> R, N) {
    rep(i, N) cin >> S[i].x >> S[i].y >> S[i].z >> SR[i];
    rep(i, M) cin >> T[i].x >> T[i].y >> T[i].z >> TB[i];
    cin >> E.x >> E.y >> E.z;
    main2();
  }
}