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

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

int n, k;
int c[5000], r[5000];
vector<int> g[5000];

typedef pair<int, int> P;
vector<int> v[5000];
int INF = 1e9 + 7;
int d[5000];

void bfs(int x) {
  queue<P> q;
  q.push(P(0, x));
  int w[5000];
  fill(w, w + n, INF);
  w[x] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int i = 0; i < g[p.second].size(); ++i) {
      if (p.first + 1 < w[g[p.second][i]]) {
        w[g[p.second][i]] = p.first + 1;
        if (p.first + 1 < r[x])
          q.push(P(p.first + 1, g[p.second][i]));
      }
    }
  }
  /*	if(x == 2){
                  for(int i=0; i<n; ++i){
                          printf("%d\\n", w[i]);
                  }
          }
  */
  for (int i = 0; i < n; ++i) {
    if (w[i] != INF && i != x)
      v[x].push_back(i);
  }
}

void dijkstra() {
  fill(d, d + n, INF);
  d[0] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, 0));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int ve = p.second;
    if (d[ve] < p.first)
      continue;
    for (int i = 0; i < v[ve].size(); i++) {
      if (d[v[ve][i]] > d[ve] + c[ve]) {
        d[v[ve][i]] = d[ve] + c[ve];
        q.push(P(d[v[ve][i]], v[ve][i]));
      }
    }
  }
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; ++i)
    scanf("%d %d", &c[i], &r[i]);

  for (int i = 0; i < k; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }
  /*
          for(int i=0; i<n; ++i){
                  printf("%d\\n", i);
                  for(int j=0; j<g[i].size(); j++){
                          printf("%d ", g[i][j]);
                  }
                  printf("\\n");
          }
  */
  for (int i = 0; i < n; ++i)
    bfs(i);
  /*
          for(int i=0; i<n; ++i){
                  printf("\\n%d %d\\n", i+1, r[i]);
                  for(int j=0; j<v[i].size(); j++){
                          printf("%d\\n", v[i][j]+1);
                  }
          }
  */
  dijkstra();
  printf("%d\\n", d[n - 1]);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

int n, k;
int c[5000], r[5000];

int d[5000][5000];
bool l[5000][5000];
int dis[5000];
const int INF = 10e8 + 7;
typedef pair<int, int> P;

// vector<P> d[5000];

void dfs(int x) {
  //	printf("%d\\n", x);
  queue<P> q;
  q.push(P(x, r[x]));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int i = 0; i < n; i++)
      if (l[p.first][i] && p.second > 0) {
        d[x][i] = min(c[x], d[x][i]);
        //				d[x].push_back(P(c[x], i));
        q.push(P(i, p.second - 1));
      }
  }
}

void dijkstra() {
  priority_queue<P, vector<P>, greater<P>> q;
  fill(dis, dis + n, INF);
  dis[0] = 0;
  q.push(P(0, 0));

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (dis[p.second] < p.first)
      continue;
    for (int i = 0; i < n; i++) {
      if (d[p.second][i] == INF)
        continue;
      if (dis[i] > dis[p.second] + d[p.second][i]) {
        dis[i] = dis[p.second] + d[p.second][i];
        q.push(P(dis[i], i));
      }
    }
/*		for(int i=0; i<d[p.second].size(); i++){
			P pp = d[p.second][i];
			if(dis[pp.second] > dis[p.second] + pp.first){
				dis[pp.second] = dis[p.second] + pp.first;
				q.push(P(dis[pp.second], pp.second));
			}
		}
*/	}
}

int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++)
  scanf("%d %d", &c[i], &r[i]);
for (int i = 0; i < k; i++) {
  int s, t;
  scanf("%d %d", &s, &t);
  l[s - 1][t - 1] = true;
  l[t - 1][s - 1] = true;
}
/*
        for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                        if(l[i][j]) printf("1\\t");
                        else printf("0\\t");
                }
                printf("\\n");
        }
*/
fill(d[0], d[4999], INF);
for (int i = 0; i < n; i++)
  d[i][i] = 0;

for (int i = 0; i < n; i++)
  dfs(i);
/*
        for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                        printf("%d\\t", d[i][j]);
                }
                printf("\\n");
        }
*/
dijkstra();

printf("%d\\n", dis[n - 1]);
}