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

#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;

#define NIL -1
#define INF 100000000
#define WHITE 1
#define GREY 2
#define BLACK 3
#define N_MAX 10000

vector<pair<int, int>> G[N_MAX];
int V[N_MAX];
int V_value[N_MAX];

void init() {
  for (int i = 0; i < N_MAX; i++) {
    V[i] = WHITE;
    V_value[i] = INF;
  }
}

int main(void) {
  init();
  int n;
  scanf("%d\\n", &n);

  for (int i = 0; i < n; i++) {
    int u, k;
    scanf("%d %d", &u, &k);
    for (int j = 0; j < k; j++) {
      int v, c;
      scanf("%d %d", &v, &c);
      G[u].push_back(make_pair(v, c));
    }
  }

  priority_queue<pair<int, int>> PQ;

  V[0] = BLACK;
  V_value[0] = 0;
  int new_node = 0;

  while (1) {
    for (int i = 0; i < G[new_node].size(); i++) {
      int node_num = G[new_node][i].first;
      int node_value = G[new_node][i].second;
      V_value[node_num] =
          min(V_value[node_num], V_value[new_node] + node_value);
      PQ.push(make_pair(V_value[node_num] * (-1), node_num));
    }

    int min_value;
    int min_index = NIL;
    while (!PQ.empty()) {
      pair<int, int> p = PQ.top();
      PQ.pop();
      // printf("pop %d %d\\n", p.second, p.first * (-1));
      if (p.first * (-1) == V_value[p.second] && V[p.second] == WHITE) {
        min_value = p.first * (-1);
        min_index = p.second;
        break;
      }
    }

    if (min_index == NIL)
      break;

    new_node = min_index;
    V[new_node] = BLACK;
    V_value[new_node] = min_value;
  }

  for (int i = 0; i < n; i++) {
    printf("%d %d\\n", i, V_value[i]);
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;

#define NIL -1
#define INF 100000000
#define WHITE 1
#define GREY 2
#define BLACK 3
#define N_MAX 10000

int G[N_MAX][N_MAX];
int V[N_MAX];
int V_value[N_MAX];

void init() {
  for (int i = 0; i < N_MAX; i++) {
    V[i] = WHITE;
    V_value[i] = INF;
  }

  for (int i = 0; i < N_MAX; i++) {
    for (int j = 0; j < N_MAX; j++) {
      G[i][j] = NIL;
    }
  }
}

int main(void) {
  init();
  int n;
  scanf("%d\\n", &n);

  for (int i = 0; i < n; i++) {
    int u, k;
    scanf("%d %d", &u, &k);
    for (int j = 0; j < k; j++) {
      int v, c;
      scanf("%d %d", &v, &c);
      G[u][v] = c;
    }
  }

  priority_queue<pair<int, int>> PQ;

  V[0] = BLACK;
  V_value[0] = 0;
  int new_node = 0;

  while (1) {
    // printf("new_node: %d\\n", new_node);
    for (int i = 0; i < n; i++) {
      if (G[new_node][i] != NIL && V[i] == WHITE) {
        V_value[i] = min(V_value[i], V_value[new_node] + G[new_node][i]);
        // printf("push %d %d\\n",i,  V_value[i]);
        PQ.push(make_pair(V_value[i] * (-1), i));
      }
    }

    int min_value;
    int min_index = NIL;
    while (!PQ.empty()) {
      pair<int, int> p = PQ.top();
      PQ.pop();
      // printf("pop %d %d\\n", p.second, p.first * (-1));
      if (p.first * (-1) == V_value[p.second] && V[p.second] == WHITE) {
        min_value = p.first * (-1);
        min_index = p.second;
        break;
      }
    }

    if (min_index == NIL)
      break;

    new_node = min_index;
    V[new_node] = BLACK;
    V_value[new_node] = min_value;
  }

  for (int i = 0; i < n; i++) {
    printf("%d %d\\n", i, V_value[i]);
  }

  return 0;
}