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

#include <bits/stdc++.h>
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;

struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : 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 k) { return Point(x * k, y * k); }
  Point operator/(double k) { return Point(x / k, y / k); }
  double norm() { return x * x + y * y; }
  double abs() { return sqrt(norm()); }

  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;
typedef vector<Point> Polygon;

double norm(Vector a) { return a.x * a.x + a.y * a.y; }
double abs(Vector a) { return sqrt(norm(a)); }
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 dp[20][20][20000];
const double PI = 3.1415926535;
int main() {
  int n, i, j, k, ans = 0;
  cin >> n;
  double r, arc;
  cin >> r >> arc;
  arc = PI / 180.0 * arc;
  Polygon g;
  g.resize(n);
  for (i = 0; i < n; i++)
    cin >> g[i].x >> g[i].y;
  double e[n][n][n], inf = 1 << 28;
  fill(e[0][0], e[0][0] + n * n * n, inf);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      for (k = 0; k < n; k++) {
        if (i == j || j == k || k == i)
          continue;
        Vector a = g[j] - g[i], b = g[k] - g[j];
        if (fabs(acos(dot(a, b) / (abs(a) * abs(b)))) <= arc)
          e[i][j][k] = abs(g[k] - g[j]);
      }
    }
  }
  for (k = 1; k < n; k++)
    e[0][0][k] = abs(g[k] - g[0]);

  fill(dp[0][0], dp[0][0] + 20 * 20 * 20000, inf);
  typedef pair<int, int> P;
  typedef pair<int, P> PP;
  typedef pair<double, PP> PPP;
  priority_queue<PPP, vector<PPP>, greater<PPP>> q;
  q.push(PPP(0.0, PP(0, P(0, n))));
  while (!q.empty()) {
    PPP ppp = q.top();
    q.pop();
    double d = ppp.first, z;
    PP pp = ppp.second;
    k = pp.first;
    P p = pp.second;
    if (d > r)
      continue;
    ans = max(ans, k);
    if (p.second == n) {
      for (i = 0; i < n; i++) {
        z = inf;
        for (j = 0; j < n; j++)
          z = min(z, e[j][0][i]);
        q.push(PPP(d + z, PP(k + 1, P(i, p.first))));
      }
    } else {
      if (dp[p.first][p.second][k] <= d)
        continue;
      dp[p.first][p.second][k] = d;
      for (i = 0; i < n; i++)
        if (dp[i][p.first][k + 1] > d + e[p.second][p.first][i])
          q.push(PPP(d + e[p.second][p.first][i], PP(k + 1, P(i, p.first))));
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;

struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : 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 k) { return Point(x * k, y * k); }
  Point operator/(double k) { return Point(x / k, y / k); }
  double norm() { return x * x + y * y; }
  double abs() { return sqrt(norm()); }

  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;
typedef vector<Point> Polygon;

double norm(Vector a) { return a.x * a.x + a.y * a.y; }
double abs(Vector a) { return sqrt(norm(a)); }
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 dp[20][20][25000];
const double PI = 3.1415926535;
int main() {
  int n, i, j, k, ans = 0;
  cin >> n;
  double r, arc;
  cin >> r >> arc;
  arc = PI / 180.0 * arc;
  Polygon g;
  g.resize(n);
  for (i = 0; i < n; i++)
    cin >> g[i].x >> g[i].y;
  double e[n][n][n], inf = 1 << 28;
  fill(e[0][0], e[0][0] + n * n * n, inf);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      for (k = 0; k < n; k++) {
        if (i == j || j == k || k == i)
          continue;
        Vector a = g[j] - g[i], b = g[k] - g[j];
        if (fabs(acos(dot(a, b) / (abs(a) * abs(b)))) <= arc)
          e[i][j][k] = abs(g[k] - g[j]);
      }
    }
  }
  for (k = 1; k < n; k++)
    e[0][0][k] = abs(g[k] - g[0]);

  fill(dp[0][0], dp[0][0] + 20 * 20 * 25000, inf);
  typedef pair<int, int> P;
  typedef pair<int, P> PP;
  typedef pair<double, PP> PPP;
  priority_queue<PPP, vector<PPP>, greater<PPP>> q;
  q.push(PPP(0.0, PP(0, P(0, n))));
  while (!q.empty()) {
    PPP ppp = q.top();
    q.pop();
    double d = ppp.first, z;
    PP pp = ppp.second;
    k = pp.first;
    P p = pp.second;
    if (d > r)
      continue;
    ans = max(ans, k);
    if (p.second == n) {
      for (i = 0; i < n; i++) {
        z = inf;
        for (j = 0; j < n; j++)
          z = min(z, e[j][0][i]);
        q.push(PPP(d + z, PP(k + 1, P(i, p.first))));
      }
    } else {
      if (dp[p.first][p.second][k] <= d)
        continue;
      dp[p.first][p.second][k] = d;
      for (i = 0; i < n; i++)
        if (dp[i][p.first][k + 1] > d + e[p.second][p.first][i])
          q.push(PPP(d + e[p.second][p.first][i], PP(k + 1, P(i, p.first))));
    }
  }
  cout << ans << endl;
  return 0;
}