prithivMLmods/Coder-Stat huggingface dataset:
Problem p02241 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int n; // number of vertices
int P[110], A[2020], B[2020], C[2020];
pair<int, int> D[2020]; // Weights and coordinates

void init(int N) { // Initially all the points are individualized
  for (int i = 0; i <= N; ++i)
    P[i] = i;
}

int root(int a) { // find the root of a
  if (P[a] == a)
    return a;                 // a is the root
  return (P[a] = root(P[a])); // find root of a, make it a's parent
}

bool is_same_set(int a, int b) { // a and b are in the same group?
  return root(a) == root(b);
}

void unite(int a, int b) { // put a and b in same group
  P[root(a)] = root(b);
}

int main() {
  cin >> n; // the number of vertices
  for (int i = 0; i < n; ++i) {
    P[i] = i; // Initialize vertices to disjoint sets
    for (int j = 0; j < n; ++j) {
      A[(n * i) + j] = i;
      B[(n * i) + j] = j;
      cin >> C[(n * i) + j];
    }
  }
  for (int i = 0; i < n * n; i++) {
    D[i].first = C[i];
    D[i].second = i;
  }
  sort(D, D + n * n);
  int sum = 0;

  for (int i = 0; i < n * n; ++i) {
    if (D[i].first >= 0) { // Assuming the weight is not -1
      if (!is_same_set(A[D[i].second], B[D[i].second])) {
        unite(A[D[i].second], B[D[i].second]);
        sum += D[i].first;
      }
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int n; // number of vertices
int P[20200], A[20200], B[20200], C[20200];
pair<int, int> D[20200]; // Weights and coordinates

int root(int a) { // find the root of a
  if (P[a] == a)
    return a;                 // a is the root
  return (P[a] = root(P[a])); // find root of a, make it a's parent
}

bool is_same_set(int a, int b) { // a and b are in the same group?
  return root(a) == root(b);
}

void unite(int a, int b) { // put a and b in same group
  P[root(a)] = root(b);
}

int main() {
  cin >> n; // the number of vertices
  for (int i = 0; i < n; ++i) {
    P[i] = i; // Initialize vertices to disjoint sets
    for (int j = 0; j < n; ++j) {
      A[(n * i) + j] = i;
      B[(n * i) + j] = j;
      cin >> C[(n * i) + j];
    }
  }
  for (int i = 0; i < n * n; i++) {
    D[i].first = C[i];
    D[i].second = i;
  }
  sort(D, D + n * n);
  int sum = 0;

  for (int i = 0; i < n * n; ++i) {
    if (D[i].first >= 0) { // Assuming the weight is not -1
      if (!is_same_set(A[D[i].second], B[D[i].second])) {
        unite(A[D[i].second], B[D[i].second]);
        sum += D[i].first;
      }
    }
  }
  cout << sum << endl;
}
Problem p02241 (C++)

Buggy submission (status: Runtime Error):
// kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 999
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
  int x = root(a), y = root(b);
  if (x == y)
    return 0;
  parent[x] = y;
  return 1;
}
int main() {
  int i, j, s, n, m, x;
  cin >> n;
  for (m = i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (cin >> x, ~x)
        a[m] = i, b[m] = j, node[m].first = x, node[m].second = m++;
  sort(node, node + m);
  for (i = 0; i < n; i++)
    parent[i] = i;
  for (s = i = 0; i < m; i++)
    if (unite(a[node[i].second], b[node[i].second]))
      s += node[i].first;
  cout << s << endl;
}

Fixed submission:
// kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 9999
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
  int x = root(a), y = root(b);
  if (x == y)
    return 0;
  parent[x] = y;
  return 1;
}
int main() {
  int i, j, s, n, m, x;
  cin >> n;
  for (m = i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (cin >> x, ~x)
        a[m] = i, b[m] = j, node[m].first = x, node[m].second = m++;
  sort(node, node + m);
  for (i = 0; i < n; i++)
    parent[i] = i;
  for (s = i = 0; i < m; i++)
    if (unite(a[node[i].second], b[node[i].second]))
      s += node[i].first;
  cout << s << endl;
}
Problem p02242 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
typedef pair<int, int> P;
typedef pair<P, P> PP;
int main(void) {
  int i, j, k, u, n, flg[1001], v, c, sum, ky[1001], t, min, g;
  PP ke[1001];
  scanf("%d", &n);
  sum = 0;
  for (i = 0; i < n; i++)
    ky[i] = M, flg[i] = 0;
  ky[0] = 0;
  for (i = 0; i < n; i++) {
    scanf("%d%d", &u, &k);
    for (j = sum; j < k + sum; j++) {
      scanf("%d%d", &v, &c);
      ke[j].ff = u, ke[j].fs = v, ke[j].sf = c;
    }
    sum += k;
  }
  sort(ke, ke + sum);
  // for(i=0;i<sum;i++) printf("ke[%d].ff=%d ke[%d].fs=%d
  // ke[%d].sf=%d\n",i,ke[i].ff,i,ke[i].fs,i,ke[i].sf);
  for (i = 0; i < sum; i++) {
    min = M;
    for (j = 0; j < sum; j++)
      if (flg[j] == 0 && min > ky[j])
        g = j, min = j;
    flg[g] = 1;
    if (min == M)
      break;
    for (j = 0; j < sum; j++) {
      t = ky[ke[j].ff] + ke[j].sf;
      if (ky[ke[j].fs] > t)
        g = j, ky[ke[j].fs] = t;
    }
  }
  for (i = 0; i < n; i++)
    printf("%d %d\n", i, ky[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
typedef pair<int, int> P;
typedef pair<P, P> PP;
int main(void) {
  int i, j, k, u, n, flg[10001], v, c, sum, ky[10001], t, min, g;
  PP ke[10001];
  scanf("%d", &n);
  sum = 0;
  for (i = 0; i < n; i++)
    ky[i] = M, flg[i] = 0;
  ky[0] = 0;
  for (i = 0; i < n; i++) {
    scanf("%d%d", &u, &k);
    for (j = sum; j < k + sum; j++) {
      scanf("%d%d", &v, &c);
      ke[j].ff = u, ke[j].fs = v, ke[j].sf = c;
    }
    sum += k;
  }
  sort(ke, ke + sum);
  // for(i=0;i<sum;i++) printf("ke[%d].ff=%d ke[%d].fs=%d
  // ke[%d].sf=%d\n",i,ke[i].ff,i,ke[i].fs,i,ke[i].sf);
  for (i = 0; i < sum; i++) {
    min = M;
    for (j = 0; j < sum; j++)
      if (flg[j] == 0 && min > ky[j])
        g = j, min = j;
    flg[g] = 1;
    if (min == M)
      break;
    for (j = 0; j < sum; j++) {
      t = ky[ke[j].ff] + ke[j].sf;
      if (ky[ke[j].fs] > t)
        g = j, ky[ke[j].fs] = t;
    }
  }
  for (i = 0; i < n; i++)
    printf("%d %d\n", i, ky[i]);
  return 0;
}
Problem p02242 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>

using namespace std;
using graph = vector<vector<int>>;

int main() {
  int n;
  cin >> n;

  graph G(n, vector<int>(n, -1));
  for (int i = 0; i < n; ++i) {
    int u, k;
    cin >> u >> k;

    for (int j = 0; j < k; ++j) {
      int v, c;
      cin >> v >> c;
      G[u][v] = c;
    }
  }

  // Dijkstra's algorithm
  vector<int> d(n, -1);
  vector<bool> visited(n, false);

  d[0] = 0;
  visited[0] = true;
  while (1) {
    int u = -1;
    int v = -1;
    int minw = numeric_limits<int>::max();
    for (int i = 0; i < n; ++i) {
      if (!visited[i])
        continue;
      for (int j = 0; j < n; ++j) {
        if (visited[j] && G[i][j] < 0)
          continue;
        int w = G[i][j] + d[i];
        if (w < minw) {
          u = i;
          v = j;
          minw = w;
        }
      }
    }
    if (v < 0) {
      break;
    }
    d[v] = minw;
    visited[v] = true;
  }

  for (int i = 0; i < n; ++i) {
    cout << i << " " << d[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits>
#include <vector>

using namespace std;
using graph = vector<vector<int>>;

int main() {
  int n;
  cin >> n;

  graph G(n, vector<int>(n, -1));
  for (int i = 0; i < n; ++i) {
    int u, k;
    cin >> u >> k;

    for (int j = 0; j < k; ++j) {
      int v, c;
      cin >> v >> c;
      G[u][v] = c;
    }
  }

  // Dijkstra's algorithm
  vector<int> d(n, -1);
  vector<bool> visited(n, false);

  d[0] = 0;
  visited[0] = true;
  while (1) {
    int u = -1;
    int v = -1;
    int minw = numeric_limits<int>::max();
    for (int i = 0; i < n; ++i) {
      if (!visited[i])
        continue;
      for (int j = 0; j < n; ++j) {
        if (visited[j] || G[i][j] < 0)
          continue;
        int w = G[i][j] + d[i];
        if (w < minw) {
          u = i;
          v = j;
          minw = w;
        }
      }
    }
    if (v < 0) {
      break;
    }
    d[v] = minw;
    visited[v] = true;
  }

  for (int i = 0; i < n; ++i) {
    cout << i << " " << d[i] << endl;
  }

  return 0;
}
Problem p02242 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

void dijkstra() {
  int minv;
  int d[MAX], color[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }

  d[0] = 0;
  color[0] = GRAY;
  while (1) {
    minv = INFTY;
    int u = -1;
    for (int i = 0; i < n; i++)
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    if (u == -1)
      break;
    color[u] == BLACK;
    for (int v = 0; v < n; v++)
      if (color[v] != BLACK && M[u][v] != INFTY)
        if (d[v] > d[u] + M[u][v]) {
          d[v] = d[u] + M[u][v];
          color[v] = GRAY;
        }
  }

  for (int i = 0; i < n; i++)
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      M[i][j] = INFTY;

  int k, c, u, v;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      M[u][v] = c;
    }
  }

  dijkstra();

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

void dijkstra() {
  int minv;
  int d[MAX], color[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }

  d[0] = 0;
  color[0] = GRAY;
  while (1) {
    minv = INFTY;
    int u = -1;
    for (int i = 0; i < n; i++)
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++)
      if (color[v] != BLACK && M[u][v] != INFTY)
        if (d[v] > d[u] + M[u][v]) {
          d[v] = d[u] + M[u][v];
          color[v] = GRAY;
        }
  }

  for (int i = 0; i < n; i++)
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      M[i][j] = INFTY;

  int k, c, u, v;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      M[u][v] = c;
    }
  }

  dijkstra();

  return 0;
}
Problem p02242 (C++)

Buggy submission (status: Runtime Error):
/*宣言*/
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
int main() {
  int n, u, k, w[101][101], v, c;
  int d[101], kas[101] = {0}, K;
  cin >> n;
  memset(d, 0x7f, sizeof(d));
  memset(w, 0x7f, sizeof(w));
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v >> c;
      w[u][v] = c;
    }
  }
  d[0] = 0;
  int p[101];
  int Min, Min1;
  Min = 0;
  Min1 = 0x7f;
  kas[0] = 1;
  rep(j, n) {
    rep(i, n) {
      if (d[i] > d[Min] + w[Min][i]) {
        d[i] = d[Min] + w[Min][i];
        p[i] = Min;
      }
      if (Min1 > d[i] && kas[i] == 0) {
        Min1 = d[i];
        K = i;
      }
    }
    kas[K] = 1;
    Min = K;
    Min1 = 100000000;
  }
  rep(i, n) { cout << i << ' ' << d[i] << endl; }
  return 0;
}


Fixed submission:
/*宣言*/
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
int main() {
  int n, u, k, w[101][101], v, c;
  int d[101], kas[101] = {0}, K;
  cin >> n;
  memset(d, 0x7f, sizeof(d));
  memset(w, 0x7f, sizeof(w));
  rep(i, n) {
    cin >> u >> k;
    rep(j, k) {
      cin >> v >> c;
      w[u][v] = c;
    }
  }
  d[0] = 0;
  int p[101];
  int Min, Min1;
  Min = 0;
  Min1 = 100000000;
  kas[0] = 1;
  rep(j, n) {
    rep(i, n) {
      if (d[i] > d[Min] + w[Min][i]) {
        d[i] = d[Min] + w[Min][i];
        p[i] = Min;
      }
      if (Min1 > d[i] && kas[i] == 0) {
        Min1 = d[i];
        K = i;
      }
    }
    kas[K] = 1;
    Min = K;
    Min1 = 100000000;
  }
  rep(i, n) { cout << i << ' ' << d[i] << endl; }
  return 0;
}
Problem p02242 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <climits>
#include <cstdio>
#include <iostream>
using namespace std;

#define N 10000
int n;

int g[N][N];

int weight[N];
bool us[N];

// prim
void prim() {

  int v;

  weight[0] = 0;

  while (true) {
    v = -1;

    for (int i = 0; i < n; i++) {
      if (us[i] != true && (v == -1 || weight[i] < weight[v]))
        v = i;
    }

    if (v == -1)
      break;
    us[v] = true;

    for (int i = 0; i < n; i++) {
      if (weight[i] > (g[v][i] + weight[v])) {

        weight[i] = (g[v][i] + weight[v]);
        //      cout <<"fsafa"<<endl;
      }
    }
  }
}

int main() {

  int u, k1, c, v;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      g[i][j] = 9953535;
    }
  }

  // load n
  cin >> n;

  // load data
  for (int i = 0; i < n; i++) {
    weight[i] = INT_MAX;
    us[i] = false;
    scanf("%d %d", &u, &k1);
    for (int j = 0; j < k1; j++) {
      scanf("%d %d", &v, &c);
      g[u][v] = c;
      //    cout <<g[i][j]<<" ";
    }
    //    cout <<endl;
  }

  // print
  prim();

  for (int i = 0; i < n; i++) {
    cout << i << " " << weight[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <climits>
#include <cstdio>
#include <iostream>
using namespace std;

#define N 1010
int n;

int g[N][N];

int weight[N];
bool us[N];

// prim
void prim() {

  int v;

  weight[0] = 0;

  while (true) {
    v = -1;

    for (int i = 0; i < n; i++) {
      if (us[i] != true && (v == -1 || weight[i] < weight[v]))
        v = i;
    }

    if (v == -1)
      break;
    us[v] = true;

    for (int i = 0; i < n; i++) {
      if (weight[i] > (g[v][i] + weight[v])) {

        weight[i] = (g[v][i] + weight[v]);
        //      cout <<"fsafa"<<endl;
      }
    }
  }
}

int main() {

  int u, k1, c, v;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      g[i][j] = 9953535;
    }
  }

  // load n
  cin >> n;

  // load data
  for (int i = 0; i < n; i++) {
    weight[i] = INT_MAX;
    us[i] = false;
    scanf("%d %d", &u, &k1);
    for (int j = 0; j < k1; j++) {
      scanf("%d %d", &v, &c);
      g[u][v] = c;
      //    cout <<g[i][j]<<" ";
    }
    //    cout <<endl;
  }

  // print
  prim();

  for (int i = 0; i < n; i++) {
    cout << i << " " << weight[i] << endl;
  }
  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1000000000
#define MAX_N 100
using namespace std;
int n;
int d[MAX_N];

struct edge {
  int to, cost;
  edge(int to_, int cost_) : to(to_), cost(cost_) {}
};
typedef pair<int, int> P;
vector<edge> G[MAX_N];

void Dijkstra(int start) {
  fill(d, d + n, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  d[start] = 0;
  que.push(P(0, start));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> n;
  for (int i = 0, u, k; i < n; i++) {
    cin >> u >> k;
    for (int j = 0, v, c; j < k; j++) {
      cin >> v >> c;
      G[u].push_back(edge(v, c));
    }
  }
  Dijkstra(0);
  for (int i = 0; i < n; i++) {
    cout << i << ' ' << d[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1000000000
#define MAX_N 10000
using namespace std;
int n;
int d[MAX_N];

struct edge {
  int to, cost;
  edge(int to_, int cost_) : to(to_), cost(cost_) {}
};
typedef pair<int, int> P;
vector<edge> G[MAX_N];

void Dijkstra(int start) {
  fill(d, d + n, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  d[start] = 0;
  que.push(P(0, start));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> n;
  for (int i = 0, u, k; i < n; i++) {
    cin >> u >> k;
    for (int j = 0, v, c; j < k; j++) {
      cin >> v >> c;
      G[u].push_back(edge(v, c));
    }
  }
  Dijkstra(0);
  for (int i = 0; i < n; i++) {
    cout << i << ' ' << d[i] << endl;
  }
  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> pii;
#define INF 0x3f3f3f3f
#define fi first
#define se second

int mcos[105];
vector<pii> G[105];
int N;

void dijkstra(int start) {
  priority_queue<pii, vector<pii>, greater<pii>> pq;

  pq.push(pii(0, start));
  memset(mcos, INF, sizeof(mcos));
  mcos[start] = 0;

  while (!pq.empty()) {
    const pii tmp = pq.top();
    pq.pop();

    const int now = tmp.se;

    if (mcos[now] < tmp.fi)
      continue;

    for (int i = 0; i < G[now].size(); ++i) {
      const int newcost = G[now][i].fi + tmp.fi;
      const int next = G[now][i].se;

      if (newcost < mcos[next]) {
        mcos[next] = newcost;
        pq.push(pii(newcost, next));
      }
    }
  }
  return;
}

int main() {
  cin >> N;
  int k, u, v, c;

  for (int i = 0; i < N; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      G[u].push_back(make_pair(c, v));
    }
  }
  dijkstra(0);
  for (int i = 0; i < N; i++) {
    cout << i << " " << mcos[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> pii;
#define INF 0x3f3f3f3f
#define fi first
#define se second

int mcos[10005];
vector<pii> G[1000005];
int N;

void dijkstra(int start) {
  priority_queue<pii, vector<pii>, greater<pii>> pq;

  pq.push(pii(0, start));
  memset(mcos, INF, sizeof(mcos));
  mcos[start] = 0;

  while (!pq.empty()) {
    const pii tmp = pq.top();
    pq.pop();

    const int now = tmp.se;

    if (mcos[now] < tmp.fi)
      continue;

    for (int i = 0; i < G[now].size(); ++i) {
      const int newcost = G[now][i].fi + tmp.fi;
      const int next = G[now][i].se;

      if (newcost < mcos[next]) {
        mcos[next] = newcost;
        pq.push(pii(newcost, next));
      }
    }
  }
  return;
}

int main() {
  cin >> N;
  int k, u, v, c;

  for (int i = 0; i < N; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      G[u].push_back(make_pair(c, v));
    }
  }
  dijkstra(0);
  for (int i = 0; i < N; i++) {
    cout << i << " " << mcos[i] << endl;
  }
  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
static const int NMAX = 100;
#define INF (1 << 30)

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> adj[NMAX];

  for (int i = 0; i < n; i++) {
    int id, nn;
    cin >> id >> nn;
    for (int j = 0; j < nn; j++) {
      int to, weight;
      cin >> to >> weight;
      // use negative weights for priority_queue
      adj[id].push_back(make_pair(-weight, to));
    }
  }

  bool visited[n];
  int dist[n];
  for (int i = 0; i < n; i++) {
    visited[i] = false;
    dist[i] = -INF;
  }

  priority_queue<pair<int, int>> distQ;

  // add node 0
  for (int i = 0; i < adj[0].size(); i++) {
    distQ.push(adj[0][i]);
    int weight = adj[0][i].first;
    int id = adj[0][i].second;
    dist[id] = weight;
  }
  visited[0] = true;

  while (!distQ.empty()) {
    // find nearest node
    int minnode = distQ.top().second;
    distQ.pop();

    // add node
    visited[minnode] = true;

    // update dist
    for (int i = 0; i < adj[minnode].size(); i++) {
      int weight = adj[minnode][i].first;
      int id = adj[minnode][i].second;

      if (visited[id] == false) {
        int newWeight = dist[minnode] + weight;
        if (-newWeight < -dist[id]) {
          distQ.push(make_pair(newWeight, id));
          dist[id] = newWeight;
        }
      }
    }
  }

  dist[0] = 0;
  for (int i = 0; i < n; i++)
    cout << i << " " << -dist[i] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
static const int NMAX = 10000;
#define INF (1 << 30)

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> adj[NMAX];

  for (int i = 0; i < n; i++) {
    int id, nn;
    cin >> id >> nn;
    for (int j = 0; j < nn; j++) {
      int to, weight;
      cin >> to >> weight;
      // use negative weights for priority_queue
      adj[id].push_back(make_pair(-weight, to));
    }
  }

  bool visited[n];
  int dist[n];
  for (int i = 0; i < n; i++) {
    visited[i] = false;
    dist[i] = -INF;
  }

  priority_queue<pair<int, int>> distQ;

  // add node 0
  for (int i = 0; i < adj[0].size(); i++) {
    distQ.push(adj[0][i]);
    int weight = adj[0][i].first;
    int id = adj[0][i].second;
    dist[id] = weight;
  }
  visited[0] = true;

  while (!distQ.empty()) {
    // find nearest node
    int minnode = distQ.top().second;
    distQ.pop();

    // add node
    visited[minnode] = true;

    // update dist
    for (int i = 0; i < adj[minnode].size(); i++) {
      int weight = adj[minnode][i].first;
      int id = adj[minnode][i].second;

      if (visited[id] == false) {
        int newWeight = dist[minnode] + weight;
        if (-newWeight < -dist[id]) {
          distQ.push(make_pair(newWeight, id));
          dist[id] = newWeight;
        }
      }
    }
  }

  dist[0] = 0;
  for (int i = 0; i < n; i++)
    cout << i << " " << -dist[i] << endl;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define d first
#define wgt first
#define to second

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vec;
typedef vector<vec> Graph;

Graph G(11000);
int n, d[11000];

void Dijkstra() {
  bool used[1100];
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  fill(d, d + n, INT_MAX);
  memset(used, false, sizeof(used));
  pq.push(make_pair(0, 0));
  while (!pq.empty()) {
    pii u = pq.top();
    pq.pop();
    if (used[u.to])
      continue;
    used[u.to] = true;
    d[u.to] = u.d;
    for (int i = 0; i < G[u.to].size(); i++)
      if (d[G[u.to][i].to] > d[u.to] + G[u.to][i].wgt) {
        d[G[u.to][i].to] = d[u.to] + G[u.to][i].wgt;
        pq.push(make_pair(d[G[u.to][i].to], G[u.to][i].to));
      }
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int u, t;
    scanf("%d%d", &u, &t);
    for (int j = 0; j < t; j++) {
      int v, w;
      scanf("%d%d", &v, &w);
      G[u].push_back(make_pair(w, v));
    }
  }
  Dijkstra();
  for (int i = 0; i < n; i++)
    printf("%d %d\n", i, d[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define d first
#define wgt first
#define to second

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vec;
typedef vector<vec> Graph;

Graph G(11000);
int n, d[11000];

void Dijkstra() {
  bool used[11000];
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  fill(d, d + n, INT_MAX);
  memset(used, false, sizeof(used));
  pq.push(make_pair(0, 0));
  while (!pq.empty()) {
    pii u = pq.top();
    pq.pop();
    if (used[u.to])
      continue;
    used[u.to] = true;
    d[u.to] = u.d;
    for (int i = 0; i < G[u.to].size(); i++)
      if (d[G[u.to][i].to] > d[u.to] + G[u.to][i].wgt) {
        d[G[u.to][i].to] = d[u.to] + G[u.to][i].wgt;
        pq.push(make_pair(d[G[u.to][i].to], G[u.to][i].to));
      }
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int u, t;
    scanf("%d%d", &u, &t);
    for (int j = 0; j < t; j++) {
      int v, w;
      scanf("%d%d", &v, &w);
      G[u].push_back(make_pair(w, v));
    }
  }
  Dijkstra();
  for (int i = 0; i < n; i++)
    printf("%d %d\n", i, d[i]);
  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#define MAX 1000
#define INF (1 << 21)
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

int n;
vector<pair<int, int>> adj_list[MAX];

void Dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int next, min_cost;
  int color[n], dist[n];

  for (int i = 0; i < n; ++i) {
    color[i] = WHITE;
    dist[i] = INF;
  }

  dist[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!PQ.empty()) {
    pair<int, int> next = PQ.top();
    PQ.pop();
    int u = next.second;

    color[u] = BLACK;

    if (dist[u] < next.first * (-1))
      continue;

    for (int i = 0; i < adj_list[u].size(); ++i) {
      int v = adj_list[u][i].first;
      if (color[v] == BLACK)
        continue;
      if (dist[v] > dist[u] + adj_list[u][i].second) {
        color[v] = GRAY;
        dist[v] = dist[u] + adj_list[u][i].second;
        PQ.push(make_pair(dist[v] * (-1), v));
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; ++i) {
    cout << i << ' ' << (dist[i] != INF ? dist[i] : -1) << endl;
  }
}

int main() {
  int id, deg, v, w;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> id >> deg;
    for (int j = 0; j < deg; ++j) {
      cin >> v >> w;
      adj_list[id].push_back(make_pair(v, w));
    }
  }

  Dijkstra();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#define MAX 10000
#define INF (1 << 21)
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

int n;
vector<pair<int, int>> adj_list[MAX];

void Dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int next, min_cost;
  int color[n], dist[n];

  for (int i = 0; i < n; ++i) {
    color[i] = WHITE;
    dist[i] = INF;
  }

  dist[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!PQ.empty()) {
    pair<int, int> next = PQ.top();
    PQ.pop();
    int u = next.second;

    color[u] = BLACK;

    if (dist[u] < next.first * (-1))
      continue;

    for (int i = 0; i < adj_list[u].size(); ++i) {
      int v = adj_list[u][i].first;
      if (color[v] == BLACK)
        continue;
      if (dist[v] > dist[u] + adj_list[u][i].second) {
        color[v] = GRAY;
        dist[v] = dist[u] + adj_list[u][i].second;
        PQ.push(make_pair(dist[v] * (-1), v));
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; ++i) {
    cout << i << ' ' << (dist[i] != INF ? dist[i] : -1) << endl;
  }
}

int main() {
  int id, deg, v, w;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> id >> deg;
    for (int j = 0; j < deg; ++j) {
      cin >> v >> w;
      adj_list[id].push_back(make_pair(v, w));
    }
  }

  Dijkstra();

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int x[10000][2000];
int x2[10000][2000];
int y[10000];
int z[10000];

int main() {
  memset(x, 1, sizeof(x));
  memset(y, 1, sizeof(y));
  int n, a, b, c, d, e;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    for (int i = 0; i < b; i++) {
      cin >> c >> d;
      x[a][z[a]] = c;
      x2[a][z[a]] = d;
      z[a]++;
    }
  }
  y[0] = 0;
  e = min(n, 2000);
  for (int i = 0; i < e; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < z[j]; k++) {
        y[x[j][k]] = min(y[x[j][k]], y[j] + x2[j][k]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i << ' ' << y[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int x[10000][2000];
int x2[10000][2000];
int y[10000];
int z[10000];

int main() {
  memset(x, 1, sizeof(x));
  memset(y, 1, sizeof(y));
  int n, a, b, c, d, e;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    for (int i = 0; i < b; i++) {
      cin >> c >> d;
      x[a][z[a]] = c;
      x2[a][z[a]] = d;
      z[a]++;
    }
  }
  y[0] = 0;
  e = min(n, 1600);
  for (int i = 0; i < e; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < z[j]; k++) {
        y[x[j][k]] = min(y[x[j][k]], y[j] + x2[j][k]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i << ' ' << y[i] << endl;
  }
  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

struct Edge {
  int to, weight;
};

int main() {
  int n, u, k, v, c, distance[100]{}, upd_num = 1;
  bool must_update[100]{1};
  std::vector<Edge> dest[100];

  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> u >> k;
    for (int j = 0; j < k; ++j) {
      std::cin >> v >> c;
      dest[u].push_back({v, c});
    }
    if (i)
      distance[i] = 2000000000;
  }

  while (upd_num)
    for (int i = 0; i < n && upd_num; i++) {
      if (!must_update[i])
        continue;

      must_update[i] = false;
      upd_num--;
      for (auto e : dest[i]) {
        int tmp = distance[i] + e.weight;
        if (tmp < distance[e.to]) {
          distance[e.to] = tmp;
          if (!must_update[e.to]) {
            must_update[e.to] = true;
            upd_num++;
          }
        }
      }
    }

  for (int i = 0; i < n; i++) {
    std::cout << i << " " << distance[i] << std::endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

struct Edge {
  int to, weight;
};

int main() {
  int n, u, k, v, c, distance[100000]{}, upd_num = 1;
  bool must_update[100000]{1};
  std::vector<Edge> dest[500000];

  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> u >> k;
    for (int j = 0; j < k; ++j) {
      std::cin >> v >> c;
      dest[u].push_back({v, c});
    }
    if (i)
      distance[i] = 2000000000;
  }

  while (upd_num)
    for (int i = 0; i < n && upd_num; i++) {
      if (!must_update[i])
        continue;

      must_update[i] = false;
      upd_num--;
      for (auto e : dest[i]) {
        int tmp = distance[i] + e.weight;
        if (tmp < distance[e.to]) {
          distance[e.to] = tmp;
          if (!must_update[e.to]) {
            must_update[e.to] = true;
            upd_num++;
          }
        }
      }
    }

  for (int i = 0; i < n; i++) {
    std::cout << i << " " << distance[i] << std::endl;
  }

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY (1 << 30)
int n;
vector<pair<int, int>> adj[MAX];

void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int d[MAX], color[MAX];
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }
  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;
  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = BLACK;
    if (d[u] < f.first * (-1))
      continue;
    for (int j = 0; j < adj[u].size(); j++) {
      int v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
  }
}

int main() {
  cin >> n;

  int k, c, u, v;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }
  dijkstra();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MAX 10000
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY (1 << 30)
int n;
vector<pair<int, int>> adj[MAX];

void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int d[MAX], color[MAX];
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }
  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;
  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = BLACK;
    if (d[u] < f.first * (-1))
      continue;
    for (int j = 0; j < adj[u].size(); j++) {
      int v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
  }
}

int main() {
  cin >> n;

  int k, c, u, v;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }
  dijkstra();
  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
vector<pair<int, int>> adj[MAX];

void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int d[MAX], color[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }

  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;
  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;

    color[u] = BLACK;

    if (d[u] < f.first * (-1))
      continue;

    for (int j = 0; j < adj[u].size(); j++) {
      int v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
  }
}

int main() {
  int k, c, u, v;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }

  dijkstra();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
static const int MAX = 10000;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
vector<pair<int, int>> adj[MAX];

void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int d[MAX], color[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }

  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;
  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;

    color[u] = BLACK;

    if (d[u] < f.first * (-1))
      continue;

    for (int j = 0; j < adj[u].size(); j++) {
      int v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
  }
}

int main() {
  int k, c, u, v;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }

  dijkstra();

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

#define MAX 10000
#define INFTY 99999999

#define WHITE 0
#define GRAY 1
#define BLACK 2

struct Node {
  vector<int> ch;
  vector<int> co;
};

Node G[MAX];

int n;
int d[MAX];
int p[MAX];
int color[MAX];

void dijkstra(int s) {
  int u;

  for (int i = 0; i < n; i++) {
    p[i] = -1;
    d[i] = INFTY;
  }

  d[s] = 0;

  while (true) {

    int mincost = INFTY;

    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INFTY) {
      break;
    }

    color[u] = BLACK;

    for (int i = 0; i < G[u].ch.size(); i++) {
      if (color[G[u].ch[i]] != BLACK) {
        if (d[u] + G[u].co[i] < d[G[u].ch[i]]) {
          d[G[u].co[i]] = d[u] + G[u].co[i];
          p[G[u].co[i]] = d[u];
          color[G[u].co[i]] = GRAY;
        }
      }
    }
  }
}

int main(void) {
  int u;
  int k;
  int v;
  int c;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      G[u].ch.push_back(v);
      G[u].co.push_back(c);
    }
  }

  dijkstra(0);

  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

#define MAX 10000
#define INFTY 99999999

#define WHITE 0
#define GRAY 1
#define BLACK 2

struct Node {
  vector<int> ch;
  vector<int> co;
};

Node G[MAX];

int n;
int d[MAX];
int p[MAX];
int color[MAX];

void dijkstra(int s) {
  int u;

  for (int i = 0; i < n; i++) {
    p[i] = -1;
    d[i] = INFTY;
  }

  d[s] = 0;

  while (true) {

    int mincost = INFTY;

    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INFTY) {
      break;
    }

    color[u] = BLACK;

    for (int i = 0; i < G[u].ch.size(); i++) {
      int co = G[u].co[i];
      int ch = G[u].ch[i];

      if (color[ch] != BLACK) {
        if (d[u] + co < d[ch]) {
          d[ch] = d[u] + co;
          p[ch] = d[u];
          color[ch] = GRAY;
        }
      }
    }
  }
}

int main(void) {
  int u;
  int k;
  int v;
  int c;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      G[u].ch.push_back(v);
      G[u].co.push_back(c);
    }
  }

  dijkstra(0);

  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
const int inf = 1e9;
// ダイクストラO(elogv)
struct edge {
  int to, cost;
  edge(int t, int c) { to = t, cost = c; }
};
typedef pair<int, int> P;
int N;
vector<edge> G[111];
int d[111];
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  FOR(i, 1, N + 1) d[i] = inf;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  FOR(i, 0, N) {
    int u, k;
    cin >> u >> k;
    FOR(j, 0, k) {
      int v, c;
      cin >> v >> c;
      G[u].push_back(edge(v, c));
    }
  }
  dijkstra(0);
  FOR(i, 0, N) { cout << i << " " << d[i] << endl; }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
const int inf = 1e9;
// ダイクストラO(elogv)
struct edge {
  int to, cost;
  edge(int t, int c) { to = t, cost = c; }
};
typedef pair<int, int> P;
int N;
vector<edge> G[11111];
int d[11111];
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  FOR(i, 1, N + 1) d[i] = inf;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  FOR(i, 0, N) {
    int u, k;
    cin >> u >> k;
    FOR(j, 0, k) {
      int v, c;
      cin >> v >> c;
      G[u].push_back(edge(v, c));
    }
  }
  dijkstra(0);
  FOR(i, 0, N) { cout << i << " " << d[i] << endl; }
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
const int INF = 2100000000;
int n;
vector<edge> G[110];

vector<int> dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));
  vector<int> d(n, INF);
  d[s] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto e : G[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
  return d;
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int u, k, v, c;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      G[u].push_back(edge{v, c});
    }
  }
  vector<int> d = dijkstra(0);
  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
const int INF = 2100000000;
int n;
vector<edge> G[10010];

vector<int> dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));
  vector<int> d(n, INF);
  d[s] = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto e : G[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
  return d;
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int u, k, v, c;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      G[u].push_back(edge{v, c});
    }
  }
  vector<int> d = dijkstra(0);
  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define P pair<int, int> // cost to
using namespace std;

vector<P> rinsetu[100];
int mincost[100];
signed main() {
  int a;
  cin >> a;
  for (int b = 0; b < a; b++) {
    int c, d;
    cin >> c >> d;
    for (int e = 0; e < d; e++) {
      int f, g;
      scanf("%d%d", &f, &g);
      rinsetu[c].push_back(P(g, f));
    }
  }
  fill(mincost, mincost + a, 1 << 29);
  mincost[0] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 0));
  while (Q.size()) {
    P t = Q.top();
    Q.pop();
    if (t.first > mincost[t.second])
      continue;
    for (P i : rinsetu[t.second]) {
      if (mincost[i.second] > t.first + i.first) {
        mincost[i.second] = t.first + i.first;
        Q.push(P(t.first + i.first, i.second));
      }
    }
  }
  for (int i = 0; i < a; i++) {
    cout << i << " " << mincost[i] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define P pair<int, int> // cost to
using namespace std;

vector<P> rinsetu[10000];
int mincost[10000];
signed main() {
  int a;
  cin >> a;
  for (int b = 0; b < a; b++) {
    int c, d;
    cin >> c >> d;
    for (int e = 0; e < d; e++) {
      int f, g;
      scanf("%d%d", &f, &g);
      rinsetu[c].push_back(P(g, f));
    }
  }
  fill(mincost, mincost + a, 1 << 29);
  mincost[0] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, 0));
  while (Q.size()) {
    P t = Q.top();
    Q.pop();
    if (t.first > mincost[t.second])
      continue;
    for (P i : rinsetu[t.second]) {
      if (mincost[i.second] > t.first + i.first) {
        mincost[i.second] = t.first + i.first;
        Q.push(P(t.first + i.first, i.second));
      }
    }
  }
  for (int i = 0; i < a; i++) {
    cout << i << " " << mincost[i] << endl;
  }
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int n;
  cin >> n;
  int ans[101] = {};
  vector<vector<pair<int, int>>> graph(n + 1);
  for (int i = 0; i < n; i++) {
    ans[i] = -1;
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v, c;
      cin >> v >> c;
      graph[u].push_back(make_pair(v, c));
    }
  }
  priority_queue<pair<int, int>> qu;
  qu.push(make_pair(0, 0));
  ans[0] = 0;
  while (!qu.empty()) {
    int nowd = -(qu.top()).first;
    int nownode = (qu.top()).second;
    qu.pop();
    for (int i = 0; i < graph[nownode].size(); i++) {
      int node = graph[nownode][i].first;
      int next = nowd + graph[nownode][i].second;
      if (ans[node] < 0 || ans[node] > next) {
        ans[node] = next;
        qu.push(make_pair(-next, node));
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i << " " << ans[i] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int n;
  cin >> n;
  int ans[10001] = {};
  vector<vector<pair<int, int>>> graph(n + 1);
  for (int i = 0; i < n; i++) {
    ans[i] = -1;
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v, c;
      cin >> v >> c;
      graph[u].push_back(make_pair(v, c));
    }
  }
  priority_queue<pair<int, int>> qu;
  qu.push(make_pair(0, 0));
  ans[0] = 0;
  while (!qu.empty()) {
    int nowd = -(qu.top()).first;
    int nownode = (qu.top()).second;
    qu.pop();
    for (int i = 0; i < graph[nownode].size(); i++) {
      int node = graph[nownode][i].first;
      int next = nowd + graph[nownode][i].second;
      if (ans[node] < 0 || ans[node] > next) {
        ans[node] = next;
        qu.push(make_pair(-next, node));
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i << " " << ans[i] << endl;
  }
}