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

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

typedef long long ll;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

typedef long double D;
typedef complex<D> P;
typedef pair<int, D> edge;

#define X real()
#define Y imag()

const D eps = 1e-8;
const D inf = 1e12;
const D PI = acos(-1);

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
} // namespace std
bool cmp_x(const P &a, const P &b) {
  return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
bool cmp_y(const P &a, const P &b) {
  return a.Y == b.Y ? a.X < b.X : a.Y < b.Y;
}
bool cmp_a(const P &a, const P &b) {
  return arg(a) == arg(b) ? norm(a) < norm(b) : arg(a) < arg(b);
}
D cross(P a, P b) { return (conj(a) * b).Y; }
D dot(P a, P b) { return (conj(a) * b).X; }
D toRag(D deg) { return deg * PI / 180.0; }
P rot(P base, P a, D theta) {
  return base + polar(abs(a - base), arg(a - base) + theta);
}

struct L : public vector<P> { // line and segment
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};

D f(P a, P b) { return acos(dot(a, b) / abs(a) / abs(b)) / PI * 180; }

int n;
D r, theta;
double dp[22][22][10010];
vector<edge> es[22][22];
P ps[22];

struct state {
  int pre, v, cnt;
  D d;
};
bool operator<(const state &a, const state &b) { return a.d > b.d; }

int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  cin >> r >> theta;
  rep(i, n) {
    D x, y;
    cin >> x >> y;
    ps[i] = P(x, y);
  }
  rep(i, n) rep(j, n) rep(k, 10010) dp[i][j][k] = inf;
  rep(i, n) rep(j, n) {
    if (i == j)
      continue;
    rep(k, n) { // (i,j) -> k
      if (i == k || j == k)
        continue;
      P v1 = ps[j] - ps[i];
      P v2 = ps[k] - ps[j];
      if (abs(f(v1, v2)) < theta) {
        // dbg(i); dbg(j); dbg(k);
        es[i][j].push_back(edge(k, abs(v2)));
      }
    }
  }
  priority_queue<state> que;
  repl(i, 1, n) { que.push((state){0, i, 1, abs(ps[i] - ps[0])}); }
  int res = 0;
  while (que.size()) {
    int pre = que.top().pre, v = que.top().v, cnt = que.top().cnt;
    D d = que.top().d;
    que.pop();
    if (dp[pre][v][cnt] != inf)
      continue;
    if (d < r)
      maxch(res, cnt);
    else
      continue;
    dp[pre][v][cnt] = d;
    for (edge nxt : es[pre][v]) {
      que.push((state){v, nxt.fi, cnt + 1, d + nxt.se});
    }
  }
  cout << res << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

typedef long double D;
typedef complex<D> P;
typedef pair<int, D> edge;

#define X real()
#define Y imag()

const D eps = 1e-8;
const D inf = 1e12;
const D PI = acos(-1);

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
} // namespace std
bool cmp_x(const P &a, const P &b) {
  return a.X == b.X ? a.Y < b.Y : a.X < b.X;
}
bool cmp_y(const P &a, const P &b) {
  return a.Y == b.Y ? a.X < b.X : a.Y < b.Y;
}
bool cmp_a(const P &a, const P &b) {
  return arg(a) == arg(b) ? norm(a) < norm(b) : arg(a) < arg(b);
}
D cross(P a, P b) { return (conj(a) * b).Y; }
D dot(P a, P b) { return (conj(a) * b).X; }
D toRag(D deg) { return deg * PI / 180.0; }
P rot(P base, P a, D theta) {
  return base + polar(abs(a - base), arg(a - base) + theta);
}

struct L : public vector<P> { // line and segment
  L(const P &a, const P &b) {
    push_back(a);
    push_back(b);
  }
};

D f(P a, P b) { return acos(dot(a, b) / abs(a) / abs(b)) / PI * 180; }

int n;
D r, theta;
D dp[22][22][10010];
vector<edge> es[22][22];
P ps[22];

struct state {
  int pre, v, cnt;
  D d;
};
bool operator<(const state &a, const state &b) { return a.d > b.d; }

int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  cin >> r >> theta;
  rep(i, n) {
    D x, y;
    cin >> x >> y;
    ps[i] = P(x, y);
  }
  rep(i, n) rep(j, n) rep(k, 10010) dp[i][j][k] = inf;
  rep(i, n) rep(j, n) {
    if (i == j)
      continue;
    rep(k, n) { // (i,j) -> k
      if (i == k || j == k)
        continue;
      P v1 = ps[j] - ps[i];
      P v2 = ps[k] - ps[j];
      if (abs(f(v1, v2)) < theta) {
        // dbg(i); dbg(j); dbg(k);
        es[i][j].push_back(edge(k, abs(v2)));
      }
    }
  }
  priority_queue<state> que;
  repl(i, 1, n) { que.push((state){0, i, 1, abs(ps[i] - ps[0])}); }
  int res = 0;
  while (que.size()) {
    int pre = que.top().pre, v = que.top().v, cnt = que.top().cnt;
    D d = que.top().d;
    que.pop();
    if (dp[pre][v][cnt] != inf)
      continue;
    if (d < r)
      maxch(res, cnt);
    else
      continue;
    dp[pre][v][cnt] = d;
    for (edge nxt : es[pre][v]) {
      que.push((state){v, nxt.fi, cnt + 1, d + nxt.se});
    }
  }
  cout << res << endl;
  return 0;
}