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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

#define EPS (1e-10)
#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))
#define PI acos(-1)

// ???/????????????
struct Point {
  double x, y;
  Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }

  double abs() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }

  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

// ????????????????????????
double norm(Vector v) { return v.x * v.x + v.y * v.y; }
// ?????????????????§??????
double abs(Vector v) { return sqrt(norm(v)); }
// ?????????????????????
double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
// ??????????????????????????§??????
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
// ?????????????????¢
double getDistance(Point a, Point b) { return abs(a - b); }
double getAngle(Point a, Point b, Point c) {
  Vector v = b - a, w = c - b;
  double alpha = atan2(v.y, v.x), beta = atan2(w.y, w.x);
  if (alpha > beta)
    swap(alpha, beta);
  double theta = (beta - alpha) * 180 / PI;
  return min(theta, 360 - theta);
}

int n;
double r, t;
vector<Point> ps;

double d[21][21][10001];

struct State {
  int now, prev, car;
  double dist;
  State() {}
  State(int now, int prev, int car, double dist)
      : now(now), prev(prev), car(car), dist(dist) {}
  bool operator<(const State &r) const { return dist > r.dist; }
};

int solve() {
  rep(i, 21) rep(j, 21) rep(k, 10001) d[i][j][k] = 1e10;

  priority_queue<State> que;
  reps(i, 1, n) {
    double dist = getDistance(ps[0], ps[i]);
    if (le(dist, r)) {
      d[i][0][1] = dist;
      que.push(State(i, 0, 1, dist));
    }
  }

  vector<vector<vector<int>>> G(n, vector<vector<int>>(n));
  rep(i, n) rep(j, n) rep(k, n) {
    if (i != j && i != k && le(getAngle(ps[j], ps[i], ps[k]), t)) {
      G[i][j].push_back(k);
    }
  }

  int ans = 0;
  while (!que.empty()) {
    State st = que.top();
    que.pop();
    int now = st.now, prev = st.prev, car = st.car;
    double dist = st.dist;
    ans = max(ans, car);
    if (dist > d[now][prev][car])
      continue;
    for (int to : G[now][prev]) {
      double di = dist + getDistance(ps[now], ps[to]);
      if (le(di, r) && d[to][now][car + 1] > di) {
        d[to][now][car + 1] = di;
        que.push(State(to, now, car + 1, di));
      }
    }
  }

  return ans;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> n >> r >> t;
  ps.resize(n);
  rep(i, n) cin >> ps[i].x >> ps[i].y;

  cout << solve() << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

#define EPS (1e-10)
#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))
#define PI acos(-1)

// ???/????????????
struct Point {
  double x, y;
  Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }

  double abs() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }

  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

// ????????????????????????
double norm(Vector v) { return v.x * v.x + v.y * v.y; }
// ?????????????????§??????
double abs(Vector v) { return sqrt(norm(v)); }
// ?????????????????????
double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
// ??????????????????????????§??????
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
// ?????????????????¢
double getDistance(Point a, Point b) { return abs(a - b); }
double getAngle(Point a, Point b, Point c) {
  Vector v = b - a, w = c - b;
  double alpha = atan2(v.y, v.x), beta = atan2(w.y, w.x);
  if (alpha > beta)
    swap(alpha, beta);
  double theta = (beta - alpha) * 180 / PI;
  return min(theta, 360 - theta);
}

int n;
double r, t;
vector<Point> ps;

double d[21][21][10001];

struct State {
  int now, prev, car;
  double dist;
  State() {}
  State(int now, int prev, int car, double dist)
      : now(now), prev(prev), car(car), dist(dist) {}
  bool operator<(const State &r) const { return dist > r.dist; }
};

int solve() {
  rep(i, 21) rep(j, 21) rep(k, 10001) d[i][j][k] = 1e10;

  priority_queue<State> que;
  reps(i, 1, n) {
    double dist = getDistance(ps[0], ps[i]);
    if (le(dist, r)) {
      d[i][0][1] = dist;
      que.push(State(i, 0, 1, dist));
    }
  }

  int ans = 0;
  while (!que.empty()) {
    State st = que.top();
    que.pop();
    int now = st.now, prev = st.prev, car = st.car;
    double dist = st.dist;
    ans = max(ans, car);
    if (dist > d[now][prev][car])
      continue;
    rep(i, n) if (i != now) {
      double di = dist + getDistance(ps[now], ps[i]);
      if (le(getAngle(ps[prev], ps[now], ps[i]), t) && le(di, r) &&
          d[i][now][car + 1] > di) {
        d[i][now][car + 1] = di;
        que.push(State(i, now, car + 1, di));
      }
    }
  }

  return ans;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> n >> r >> t;
  ps.resize(n);
  rep(i, n) cin >> ps[i].x >> ps[i].y;

  cout << solve() << endl;

  return 0;
}