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

#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef unsigned long long ull;

const int N = 20;
int x[N], y[N];

double dist[10010][N][N];

vector<int> G[N][N];
double D[N][N];
int ok[N][N][N];

const double PI = 4 * atan(1);
const double EPS = 1e-9;

int main() {
  int n;
  double r, theta;
  cin >> n >> r >> theta;
  theta = theta / 180 * PI;
  rep(i, n) cin >> x[i] >> y[i];

  rep(i, n) rep(j, n) {
    D[i][j] =
        sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
  }

  rep(i, n) rep(j, n) rep(k, n) {
    if (i == j || j == k)
      continue;
    double x1 = x[j] - x[i], y1 = y[j] - y[i];
    double x2 = x[k] - x[j], y2 = y[k] - y[j];

    double ct = (x1 * x2 + y1 * y2) / (D[i][j] * D[j][k]);
    double t = acos(ct);

    if (abs(t) < theta + EPS)
      G[i][j].push_back(k), ok[i][j][k] = 1;
  }

  MEMSET(dist, 0x7f);

  priority_queue<tuple<double, int, int, int>> pq;

  rep(i, n) rep(j, n) {
    if (i)
      continue;
    if (i == j)
      continue;
    if (D[i][j] > r - EPS)
      continue;
    pq.emplace(-D[i][j], 1, i, j);
    dist[1][i][j] = D[i][j];
  }

  int ans = 0;
  // while (!pq.empty()){
  //	auto t = pq.top();
  //	pq.pop();
  //	double d;
  //	int c, prv, cur;
  //	tie(d, c, prv, cur) = t;
  //	d = -d;

  //	if (dist[c][prv][cur] + EPS < d) continue;

  //	ans = max(ans, c);

  //	for (auto v : G[prv][cur]){
  //		double nd = d + D[cur][v];
  //		if (r < nd + EPS) continue;
  //		if (dist[c + 1][cur][v] < nd + EPS) continue;
  //		dist[c + 1][cur][v] = nd;
  //		pq.emplace(-nd, c + 1, cur, v);
  //	}
  //}

  rep(i, 10010) rep(j, n) rep(k, n) {
    if (dist[i][j][k] > r - EPS)
      continue;
    ans = i;
    rep(l, n) if (ok[j][k][l]) dist[i + 1][k][l] =
        min(dist[i + 1][k][l], dist[i][j][k] + D[k][l]);
  }

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef unsigned long long ull;

const int N = 30;
int x[N], y[N];

double dist[10010][N][N];

vector<int> G[N][N];
double D[N][N];

const double PI = 4 * atan(1);
const double EPS = 1e-9;

int main() {
  int n;
  double r, theta;
  cin >> n >> r >> theta;
  theta = theta / 180 * PI;
  rep(i, n) cin >> x[i] >> y[i];

  rep(i, n) rep(j, n) {
    D[i][j] =
        sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
  }

  rep(i, n) rep(j, n) rep(k, n) {
    if (i == j || j == k)
      continue;
    double x1 = x[j] - x[i], y1 = y[j] - y[i];
    double x2 = x[k] - x[j], y2 = y[k] - y[j];

    double ct = (x1 * x2 + y1 * y2) / (D[i][j] * D[j][k]);
    double t = acos(ct);

    if (abs(t) < theta + EPS)
      G[i][j].push_back(k);
  }

  MEMSET(dist, 0x7f);
  priority_queue<tuple<double, int, int, int>> pq;

  rep(i, n) rep(j, n) {
    if (i == j)
      continue;
    if (D[i][j] > r - EPS)
      continue;
    pq.emplace(-D[i][j], 1, i, j);
    dist[1][i][j] = D[i][j];
  }

  int ans = 0;
  while (!pq.empty()) {
    auto t = pq.top();
    pq.pop();
    double d;
    int c, prv, cur;
    tie(d, c, prv, cur) = t;
    d = -d;

    if (dist[c][prv][cur] + EPS < d)
      continue;

    ans = max(ans, c);

    for (auto v : G[prv][cur]) {
      double nd = d + D[cur][v];
      if (r < nd + EPS)
        continue;
      if (dist[c + 1][cur][v] < nd + EPS)
        continue;
      dist[c + 1][cur][v] = nd;
      pq.emplace(-nd, c + 1, cur, v);
    }
  }

  cout << ans << endl;

  return 0;
}