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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

typedef valarray<ld> Point;

const ld eps = 1e-8, inf = 1e12;

Point make_point(ld x, ld y, ld z) {
  Point p(3);
  p[0] = x;
  p[1] = y;
  p[2] = z;
  return p;
}

ld dot(const Point &a, const Point &b) { return (a * b).sum(); }

Point cross(const Point &a, const Point &b) {
  return a.cshift(+1) * b.cshift(-1) - a.cshift(-1) * b.cshift(+1);
}

ld norm(const Point &a) { return dot(a, a); }
ld dist(const Point &a) { return sqrt(norm(a)); }

// Line

struct Line {
  Point a, b;
};

bool is_in_segment(Line l, Point p) {
  return abs(dist(l.a - p) + dist(l.b - p) - dist(l.a - l.b)) < eps;
}

Point project_lp(Line l, Point p) {
  Point point = l.a, vec = l.b - l.a;
  return point + dot(p - point, vec) / norm(vec) * vec;
}

ld distance_lp(Line l, Point p) { return dist(p - project_lp(l, p)); }

ld distance_sp(Line l, Point p) {
  Point proj = project_lp(l, p);
  if (dist(l.a - proj) + dist(l.b - proj) < dist(l.b - l.a) + eps)
    return dist(p - proj);
  else
    return min(dist(p - l.a), dist(p - l.b));
}

struct light {
  Point p;
  ld br;
};

struct balloon {
  Point p;
  ld r;
};

int main() {
  int n, m, r;
  while (cin >> n >> m >> r, n) {
    vector<balloon> vb(n);
    vector<light> vl(m);
    REP(i, n) {
      ld x, y, z, r;
      cin >> x >> y >> z >> r;
      vb[i] = balloon{Point{x, y, z}, r};
    }
    REP(i, m) {
      ld x, y, z, r;
      cin >> x >> y >> z >> r;
      vl[i] = light{Point{x, y, z}, r};
    }
    ld x, y, z;
    cin >> x >> y >> z;
    Point p{x, y, z};
    vector<vi> obst(m);
    REP(i, m) REP(j, n) {
      ld d = distance_sp(Line{vl[i].p, p}, vb[j].p);
      if ((vb[j].r - dist(vb[j].p - vl[i].p) > -EPS &&
           vb[j].r - dist(vb[j].p - p) > -EPS))
        continue;
      if (vb[j].r - d > EPS) {
        obst[i].push_back(j);
      }
    }
    vector<ld> bright(m);
    REP(i, m) {
      ld d = norm(p - vl[i].p);
      bright[i] = vl[i].br / d;
    }
    ld ans = 0;
    REP(i, 1 << m) {
      vector<bool> rem(n);
      ld tmp = 0;
      REP(j, m) {
        if ((i >> j) & 1)
          continue;
        tmp += bright[j];
        REP(k, obst[j].size()) rem[obst[j][k]] = true;
      }
      if (count(ALL(rem), true) > r)
        continue;
      chmax(ans, tmp);
    }
    cout << D10 << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

typedef valarray<ld> Point;

const ld eps = 1e-8, inf = 1e12;

Point make_point(ld x, ld y, ld z) {
  Point p(3);
  p[0] = x;
  p[1] = y;
  p[2] = z;
  return p;
}

ld dot(const Point &a, const Point &b) { return (a * b).sum(); }

Point cross(const Point &a, const Point &b) {
  return a.cshift(+1) * b.cshift(-1) - a.cshift(-1) * b.cshift(+1);
}

ld norm(const Point &a) { return dot(a, a); }
ld dist(const Point &a) { return sqrt(norm(a)); }

// Line

struct Line {
  Point a, b;
};

bool is_in_segment(Line l, Point p) {
  return abs(dist(l.a - p) + dist(l.b - p) - dist(l.a - l.b)) < eps;
}

Point project_lp(Line l, Point p) {
  Point point = l.a, vec = l.b - l.a;
  return point + dot(p - point, vec) / norm(vec) * vec;
}

ld distance_lp(Line l, Point p) { return dist(p - project_lp(l, p)); }

ld distance_sp(Line l, Point p) {
  Point proj = project_lp(l, p);
  if (dist(l.a - proj) + dist(l.b - proj) < dist(l.b - l.a) + eps)
    return dist(p - proj);
  else
    return min(dist(p - l.a), dist(p - l.b));
}

struct light {
  Point p;
  ld br;
};

struct balloon {
  Point p;
  ld r;
};

int main() {
  int n, m, r;
  while (cin >> n >> m >> r, n) {
    vector<balloon> vb(n);
    vector<light> vl(m);
    REP(i, n) {
      ld x, y, z, r;
      cin >> x >> y >> z >> r;
      vb[i] = balloon{Point{x, y, z}, r};
    }
    REP(i, m) {
      ld x, y, z, r;
      cin >> x >> y >> z >> r;
      vl[i] = light{Point{x, y, z}, r};
    }
    ld x, y, z;
    cin >> x >> y >> z;
    Point p{x, y, z};
    vector<vi> obst(m);
    REP(i, m) REP(j, n) {
      ld d = distance_sp(Line{vl[i].p, p}, vb[j].p);
      if ((vb[j].r - dist(vb[j].p - vl[i].p) > -EPS &&
           vb[j].r - dist(vb[j].p - p) > -EPS))
        continue;
      if (vb[j].r - d > EPS) {
        obst[i].push_back(j);
      }
    }
    vector<ld> bright(m);
    REP(i, m) {
      ld d = norm(p - vl[i].p);
      bright[i] = vl[i].br / d;
    }
    ld ans = 0;
    REP(i, 1 << m) {
      set<int> rem;
      ld tmp = 0;
      REP(j, m) {
        if ((i >> j) & 1)
          continue;
        tmp += bright[j];
        REP(k, obst[j].size()) rem.insert(obst[j][k]);
      }
      if (rem.size() > r)
        continue;
      chmax(ans, tmp);
    }
    cout << D10 << ans << endl;
  }
  return 0;
}