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

#include <bits/stdc++.h>
using namespace std;

using ld = double;
using P = complex<ld>;
const ld pi = acos(-1);
struct State {
  int v, pre;
  int c;
};

bool operator<(const State &lhs, const State &rhs) { return true; }

int idx(State st) { return st.v + 20 * st.pre + st.c * 20 * 20; }
int main() {
  int n;
  cin >> n;
  ld r, theta;
  cin >> r >> theta;
  theta = (theta) / 180 * pi;
  vector<P> xy(n);
  for (int i = 0; i < n; i++) {
    ld tmpx, tmpy;
    cin >> tmpx >> tmpy;
    xy[i] = {tmpx, tmpy};
  }
  int res = 0;
  priority_queue<pair<ld, State>, vector<pair<ld, State>>, greater<>> que;
  que.push({ld(0), State{0, 0, 0}});
  const ld INF = 1e9;
  vector<ld> dist(5 * 1e6, INF);
  vector<ld> check(5 * 1e6, 0);
  dist[0] = 0;

  vector<vector<vector<int>>> edge(n, vector<vector<int>>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (i == k || j == k)
          continue;
        if (i != j) {
          ld phi = arg((xy[k] - xy[j]) / (xy[j] - xy[i]));
          if (phi > pi)
            phi -= 2 * pi;
          if (abs(phi) > theta) {
            continue;
          }
        }
        edge[i][j].push_back(k);
      }
    }
  }

  while (!que.empty()) {
    auto tmp = que.top();
    que.pop();
    auto ret = tmp.second;
    int v = ret.v, pre = ret.pre;
    int c = ret.c;
    int id = idx(ret);
    ld w = tmp.first;
    if (check[id])
      continue;
    check[id] = true;
    if (w > r)
      break;
    res = max(res, c);
    for (int k = 0; k < edge[pre][v].size(); k++) {
      int i = edge[pre][v][k];
      State to{i, v, c + 1};
      int toidx = idx(to);
      if (w + abs(xy[i] - xy[v]) < dist[toidx]) {
        dist[toidx] = w + abs(xy[i] - xy[v]);
        que.push({dist[toidx], to});
      }
    }
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ld = double;
using P = complex<ld>;
const ld pi = acos(-1);
struct State {
  int v, pre;
  int c;
};

bool operator<(const State &lhs, const State &rhs) { return true; }

int idx(State st) { return st.v + 20 * st.pre + st.c * 20 * 20; }
int main() {
  int n;
  cin >> n;
  ld r, theta;
  cin >> r >> theta;
  theta = (theta) / 180 * pi;
  vector<P> xy(n);
  for (int i = 0; i < n; i++) {
    ld tmpx, tmpy;
    cin >> tmpx >> tmpy;
    xy[i] = {tmpx, tmpy};
  }
  int res = 0;
  priority_queue<pair<ld, State>, vector<pair<ld, State>>, greater<>> que;
  que.push({ld(0), State{0, 0, 0}});
  const ld INF = 1e9;
  vector<ld> dist(5 * 1e6, INF);
  vector<ld> check(5 * 1e6, 0);
  dist[0] = 0;
  while (!que.empty()) {
    auto tmp = que.top();
    que.pop();
    auto ret = tmp.second;
    int v = ret.v, pre = ret.pre;
    int c = ret.c;
    int id = idx(ret);
    ld w = tmp.first;
    if (check[id])
      continue;
    check[id] = true;
    if (w > r)
      break;
    res = max(res, c);
    for (int i = 0; i < n; i++) {
      if (i != v && i != pre) {
        if (pre != v) {
          ld phi = arg((xy[i] - xy[v]) / (xy[v] - xy[pre]));
          if (phi > pi)
            phi -= 2 * pi;
          if (abs(phi) > theta) {
            continue;
          }
        }
        State to{i, v, c + 1};
        int toidx = idx(to);
        if (w + abs(xy[i] - xy[v]) < dist[toidx]) {
          dist[toidx] = w + abs(xy[i] - xy[v]);
          que.push({dist[toidx], to});
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}