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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;

#define BIG_NUM 2000000000

using namespace std;

struct Node {

  Node(int arg_to, int arg_cost) {
    to = arg_to;
    cost = arg_cost;
  }
  bool operator<(const struct Node &arg) const { return cost > arg.cost; };

  int to, cost;
};

int main() {

  int N;
  scanf("%d", &N);

  vector<Node> V[N];

  int node, num, to, cost, D[N];
  ;

  for (int loop = 0; loop < N; loop++) {
    scanf("%d", &node);
    scanf("%d", &num);
    for (int i = 0; i < num; i++) {
      scanf("%d %d", &to, &cost);
      V[node].push_back(Node(to, cost));
    }
    D[loop] = BIG_NUM;
  }

  D[0] = 0;

  bool visited[N];
  for (int i = 1; i < N; i++)
    visited[i] = false;
  visited[0] = true;

  priority_queue<Node> Q;

  for (int i = 0; i < V[0].size(); i++) {
    D[V[0][i].to] = V[0][i].cost;
    Q.push(Node(V[0][i].to, V[0][i].cost));
  }

  int min_index;

  while (!Q.empty()) {
    if (Q.top().cost == D[Q.top().to]) {
      min_index = Q.top().to;
      Q.pop();
    } else {
      Q.pop();
      continue;
    }

    for (
        int k = 0; k < V[min_index].size();
        k++) { // min_index?????????????????´?????§????????¨??´??°??§??????????????§???min_index?????????????????¨?????????????????¢???
      if (D[V[min_index][k].to] > D[min_index] + V[min_index][k].cost) {
        D[V[min_index][k].to] = D[min_index] + V[min_index][k].cost;
        Q.push(Node(V[min_index][k].to, D[V[min_index][k].to]));
      }
    }
  }

  for (int i = 0; i < N; i++)
    printf("%d %d\\n", i, D[i]);

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;

#define BIG_NUM 2000000000

using namespace std;

struct Node {

  Node(int arg_to, int arg_cost) {
    to = arg_to;
    cost = arg_cost;
  }
  bool operator<(const struct Node &arg) const { return cost > arg.cost; };

  int to, cost;
};

int main() {

  int N;
  scanf("%d", &N);

  int **distance = new int *[N];

  for (int i = 0; i < N; i++) {
    distance[i] = new int[N];
    for (int k = 0; k < N; k++)
      distance[i][k] = BIG_NUM;
  }

  int node, num, to, cost, D[N];
  ;

  for (int loop = 0; loop < N; loop++) {
    scanf("%d", &node);
    scanf("%d", &num);
    for (int i = 0; i < num; i++) {
      scanf("%d %d", &to, &cost);
      distance[node][to] = cost;
    }
    D[loop] = BIG_NUM;
  }

  D[0] = 0;
  distance[0][0] = 0;

  bool visited[N];
  for (int i = 1; i < N; i++)
    visited[i] = false;
  visited[0] = true;

  priority_queue<Node> Q;

  for (int i = 1; i < N; i++) {
    if (distance[0][i] != BIG_NUM) {
      D[i] = distance[0][i];
      Q.push(Node(i, distance[0][i]));
    }
  }

  int min_index;

  while (!Q.empty()) {
    if (Q.top().cost == D[Q.top().to]) {
      min_index = Q.top().to;
      Q.pop();
    } else {
      Q.pop();
      continue;
    }

    for (int i = 1; i < N; i++) {
      if (distance[min_index][i] != BIG_NUM &&
          D[i] > D[min_index] + distance[min_index][i]) {
        D[i] = D[min_index] + distance[min_index][i];
        Q.push(Node(i, D[i]));
      }
    }
  }

  for (int i = 0; i < N; i++)
    printf("%d %d\\n", i, D[i]);

  return 0;
}