Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define INT(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define INPUT(x) scanf("%d", &x)
#define REP1(x, n) for (int x = 0; x < n; x++)
#define REP2(x, s, e) for (int x = s; x <= e; x++)
#define BR printf("\\n")
#define INF 2000000000

class edge {
public:
  int toid;
  int norm;
  int dist;
  edge(int toid, int norm, int dist) {
    this->toid = toid;
    this->norm = norm;
    this->dist = dist;
  }
  bool operator<(const edge &E) const { return dist < E.dist; }
  bool operator>(const edge &E) const { return dist > E.dist; }
};

vector<edge> V[10000];
int d[10000];
int count_P = 0;
priority_queue<edge, vector<edge>, greater<edge>> PQ;

void Dijksra(int i, int dist, int n) {
  PQ.pop();
  d[i] = dist;
  count_P++;
  if (count_P == n)
    return;

  REP1(k, V[i].size()) {
    if (d[V[i][k].toid] == INF) {
      V[i][k].dist = dist + V[i][k].norm;
      PQ.push(V[i][k]);
    }
  }

  while (d[PQ.top().toid] != INF)
    PQ.pop();
  if (!PQ.empty())
    Dijksra(PQ.top().toid, PQ.top().dist, n);
}

int main() {
  INT(N);
  REP1(i, N) d[i] = INF;
  REP1(i, N) {
    int from, n;
    scanf("%d %d", &from, &n);
    REP1(j, n) {
      int toid, norm;
      scanf("%d %d", &toid, &norm);
      edge E = edge(toid, norm, INF);
      V[i].push_back(E);
    }
  }

  PQ.push(edge(0, 0, 0));
  Dijksra(PQ.top().toid, 0, N);

  REP1(i, N) { printf("%d %d\\n", i, d[i]); }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define INT(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define INPUT(x) scanf("%d", &x)
#define REP1(x, n) for (int x = 0; x < n; x++)
#define REP2(x, s, e) for (int x = s; x <= e; x++)
#define BR printf("\\n")
#define INF 2000000000

class edge {
public:
  int toid;
  int norm;
  int dist;
  edge(int toid, int norm, int dist) {
    this->toid = toid;
    this->norm = norm;
    this->dist = dist;
  }
  bool operator<(const edge &E) const { return dist < E.dist; }
  bool operator>(const edge &E) const { return dist > E.dist; }
};

vector<edge> V[10000];
int d[10000];
int count_P = 0;
priority_queue<edge, vector<edge>, greater<edge>> PQ;

void Dijksra(int i, int dist, int n) {
  PQ.pop();
  d[i] = dist;
  count_P++;
  // if(count_P == n) return;

  REP1(k, V[i].size()) {
    if (d[V[i][k].toid] == INF && V[i][k].dist > dist + V[i][k].norm) {
      V[i][k].dist = dist + V[i][k].norm;
      PQ.push(V[i][k]);
    }
  }

  while (d[PQ.top().toid] != INF)
    PQ.pop();
  if (!PQ.empty())
    Dijksra(PQ.top().toid, PQ.top().dist, n);
}

int main() {
  INT(N);
  REP1(i, N) d[i] = INF;
  REP1(i, N) {
    int from, n;
    scanf("%d %d", &from, &n);
    REP1(j, n) {
      int toid, norm;
      scanf("%d %d", &toid, &norm);
      edge E = edge(toid, norm, INF);
      V[i].push_back(E);
    }
  }

  PQ.push(edge(0, 0, 0));
  Dijksra(PQ.top().toid, 0, N);

  REP1(i, N) { printf("%d %d\\n", i, d[i]); }

  return 0;
}