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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const int INF = 1 << 29;
const LL MOD = 100000;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define all(c) (c).begin(), (c).end()

struct edge {
  int to, cost;
  edge(int t, int c) : to(t), cost(c) {}
};

// 隣接行列版dijk
vector<int> dijkstr(int s, vector<vector<int>> &graph) {
  int n = graph.size();
  vector<int> dists(n, INF);
  vector<bool> used(n);
  dists[0] = 0;
  while (true) {
    int v = -1;
    rep(i, n) if (!used[i] && (v == -1 || dists[i] < dists[v])) v = i;
    if (v == -1)
      break;

    used[v] = true;
    rep(i, n) dists[i] = min(dists[i], dists[v] + graph[v][i]);
  }
  return move(dists);
}

int dxk[] = {1, 1, 1, 0, -1, 0};
int dxg[] = {0, 1, 0, -1, -1, -1};
int dy[] = {-1, 0, 1, 1, 0, -1};

int main() {
  // FILE* fp_out = freopen("result.txt", "w", stdout);

  int n;
  while (cin >> n) {
    int k;
    cin >> k;
    vector<vector<int>> visitable(n, vector<int>(n, INF));
    vector<vector<int>> graph(n);
    vector<int> costs(n);
    vector<int> Rs(n);
    rep(i, n) cin >> costs[i] >> Rs[i];

    rep(i, k) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }

    rep(i, n) {
      queue<P> que;
      vector<bool> visited(n);
      que.emplace(i, Rs[i]);
      while (!que.empty()) {
        P p = que.front();
        que.pop();

        visited[p.first] = true;
        visitable[i][p.first] = costs[i];
        if (p.second == 0)
          continue;
        for (int x : graph[p.first])
          if (!visited[x]) {
            que.emplace(x, p.second - 1);
          }
      }
    }

    cout << dijkstr(0, visitable)[n - 1] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const int INF = 1 << 29;
const LL MOD = 100000;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define all(c) (c).begin(), (c).end()

struct edge {
  int to, cost;
  edge(int t, int c) : to(t), cost(c) {}
};

// 隣接行列版dijk
vector<int> dijkstr(int s, vector<vector<int>> &graph) {
  int n = graph.size();
  vector<int> dists(n, INF);
  vector<bool> used(n);
  dists[0] = 0;
  while (true) {
    int v = -1;
    rep(i, n) if (!used[i] && (v == -1 || dists[i] < dists[v])) v = i;
    if (v == -1)
      break;

    used[v] = true;
    rep(i, n) dists[i] = min(dists[i], dists[v] + graph[v][i]);
  }
  return move(dists);
}

int dxk[] = {1, 1, 1, 0, -1, 0};
int dxg[] = {0, 1, 0, -1, -1, -1};
int dy[] = {-1, 0, 1, 1, 0, -1};

int main() {
  // FILE* fp_out = freopen("result.txt", "w", stdout);

  int n;
  while (cin >> n) {
    int k;
    cin >> k;
    vector<vector<int>> visitable(n, vector<int>(n, INF));
    vector<vector<int>> graph(n);
    vector<int> costs(n);
    vector<int> Rs(n);
    rep(i, n) cin >> costs[i] >> Rs[i];

    rep(i, k) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }

    rep(k, n) {
      queue<P> que;
      vector<bool> visited(n);
      que.emplace(k, Rs[k]);
      visited[k] = true;
      while (!que.empty()) {
        P p = que.front();
        que.pop();

        visitable[k][p.first] = costs[k];
        if (p.second == 0)
          continue;
        for (int x : graph[p.first])
          if (!visited[x]) {
            que.emplace(x, p.second - 1);
          }
      }
    }

    cout << dijkstr(0, visitable)[n - 1] << endl;
  }
  return 0;
}