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

#include <bits/stdc++.h>
using namespace std;
#define M 100000007
typedef pair<short, short> P;
int main(void) {
  int n, m;
  cin >> n >> m;
  P a[n], b[m];
  short x, y;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    a[i] = P(x, y);
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    b[i] = P(x, y);
  }
  vector<P> cost[n + 1];
  vector<short> rin[n + 1];
  for (int i = 0; i < m; i++) {
    P p = b[i];
    rin[p.first].push_back(p.second);
    rin[p.second].push_back(p.first);
  }
  for (int i = 0; i < n; i++) {
    int lim = a[i].second;
    int flg[n + 1];
    fill(flg, flg + n + 1, 0);
    queue<P> q;
    q.push(P(i + 1, 0));
    while (!q.empty()) {
      P p = q.front();
      q.pop();
      for (int j = 0; j < rin[p.first].size(); j++) {
        if (rin[p.first][j] != i + 1) {
          cost[i + 1].push_back(P(rin[p.first][j], a[i].first));
          if (p.second < lim - 1 && flg[rin[p.first][j]] != 1) {
            flg[rin[p.first][j]] = 1;
            q.push(P(rin[p.first][j], p.second + 1));
          }
        }
      }
    }
  }
  /*	for(int i=1;i<=n;i++){
                  for(int j=0;j<cost[i].size();j++){
                          cout<<cost[i][j].first<<':'<<cost[i][j].second<<' ';
                  }
                  cout<<endl;
          }*/
  int leng[n + 1];
  fill(leng, leng + n + 1, M);
  priority_queue<P, vector<P>, greater<P>> que;
  leng[1] = 0;
  que.push(P(0, 1));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (leng[v] < p.first)
      continue;
    for (int i = 0; i < cost[v].size(); i++) {
      P u = cost[v][i];
      int t = leng[v] + u.second;
      if (leng[u.first] > t) {
        leng[u.first] = t;
        que.push(P(t, u.first));
      }
    }
  }
  cout << leng[n] << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define M 100000007
typedef pair<int, int> P;
int main(void) {
  int n, m;
  cin >> n >> m;
  P a[n], b[m];
  int x, y;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    a[i] = P(x, y);
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    b[i] = P(x, y);
  }
  vector<P> cost[n + 1];
  vector<int> rin[n + 1];
  for (int i = 0; i < m; i++) {
    P p = b[i];
    rin[p.first].push_back(p.second);
    rin[p.second].push_back(p.first);
  }
  for (int i = 0; i < n; i++) {
    int lim = a[i].second;
    int flg[n + 1];
    fill(flg, flg + n + 1, 0);
    queue<P> q;
    q.push(P(i + 1, 0));
    while (!q.empty()) {
      P p = q.front();
      q.pop();
      for (int j = 0; j < rin[p.first].size(); j++) {
        if (rin[p.first][j] != i + 1) {
          cost[i + 1].push_back(P(rin[p.first][j], a[i].first));
          if (p.second < lim - 1 && flg[rin[p.first][j]] != 1) {
            flg[rin[p.first][j]] = 1;
            q.push(P(rin[p.first][j], p.second + 1));
          }
        }
      }
    }
  }
  /*	for(int i=1;i<=n;i++){
                  for(int j=0;j<cost[i].size();j++){
                          cout<<cost[i][j].first<<':'<<cost[i][j].second<<' ';
                  }
                  cout<<endl;
          }*/
  int leng[n + 1];
  fill(leng, leng + n + 1, M);
  priority_queue<P, vector<P>, greater<P>> que;
  leng[1] = 0;
  que.push(P(0, 1));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (leng[v] < p.first)
      continue;
    for (int i = 0; i < cost[v].size(); i++) {
      P u = cost[v][i];
      int t = leng[v] + u.second;
      if (leng[u.first] > t) {
        leng[u.first] = t;
        que.push(P(t, u.first));
      }
    }
  }
  cout << leng[n] << endl;
  return 0;
}