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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1e4 + 1;
const double EPS = 1e-6;
const double INF = 1e18;
const double PI = acos(-1);
typedef complex<double> P;

double angle(P a, P b) { return abs(arg(a / b)); }

int main() {
  int n;
  double r, theta;
  cin >> n >> r >> theta;
  theta = theta / 180 * PI;

  vector<P> xy(n);
  for (int i = 0; i < n; i++) {
    double x, y;
    cin >> x >> y;
    xy[i] = P(x, y);
  }

  vector<vector<vector<double>>> dp(
      n + 1, vector<vector<double>>(n, vector<double>(MAX, INF)));
  dp[n][0][0] = 0;
  vector<vector<vector<bool>>> cango(
      n + 1, vector<vector<bool>>(n, vector<bool>(n, true)));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (i == j || j == k ||
            angle(xy[j] - xy[i], xy[k] - xy[j]) > theta + EPS) {
          cango[i][j][k] = false;
        }
      }
    }
  }
  int ans = 0;
  for (int k = 0; k < MAX - 1; k++) {
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n; j++) {
        if (dp[i][j][k] == INF)
          continue;
        for (int p = 0; p < n; p++) {
          if (p == j)
            continue;
          if (!cango[i][j][p])
            continue;
          double dist = dp[i][j][k] + abs(xy[p] - xy[j]);
          if (dist < dp[j][p][k + 1] && dist < r + EPS) {
            dp[j][p][k + 1] = dist;
            ans = k + 1;
          }
        }
      }
    }
    if (ans != k + 1)
      break;
  }
  cout << ans << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1e5 + 1;
const double EPS = 1e-6;
const double INF = 1e18;
const double PI = acos(-1);
typedef complex<double> P;

double angle(P a, P b) { return abs(arg(a / b)); }

int main() {
  int n;
  double r, theta;
  cin >> n >> r >> theta;
  theta = theta / 180 * PI;

  vector<P> xy(n);
  for (int i = 0; i < n; i++) {
    double x, y;
    cin >> x >> y;
    xy[i] = P(x, y);
  }

  vector<vector<vector<double>>> dp(
      n + 1, vector<vector<double>>(n, vector<double>(MAX, INF)));
  dp[n][0][0] = 0;
  vector<vector<vector<bool>>> cango(
      n + 1, vector<vector<bool>>(n, vector<bool>(n, true)));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (i == j || j == k ||
            angle(xy[j] - xy[i], xy[k] - xy[j]) > theta + EPS) {
          cango[i][j][k] = false;
        }
      }
    }
  }
  int ans = 0;
  for (int k = 0; k < MAX - 1; k++) {
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n; j++) {
        if (dp[i][j][k] == INF)
          continue;
        for (int p = 0; p < n; p++) {
          if (p == j)
            continue;
          if (!cango[i][j][p])
            continue;
          double dist = dp[i][j][k] + abs(xy[p] - xy[j]);
          if (dist < dp[j][p][k + 1] && dist < r + EPS) {
            dp[j][p][k + 1] = dist;
            ans = k + 1;
          }
        }
      }
    }
    if (ans != k + 1)
      break;
  }
  cout << ans << endl;
  return 0;
}