alexjercan/bugnet huggingface dataset:
Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> node;
typedef vector<node> Adj_list;
typedef vector<Adj_list> Adj_lists;

class less_cost {
public:
  bool operator()(const pair<int, int> lhs, const pair<int, int> rhs) const {
    return lhs.second > rhs.second;
  }
};

int main() {

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

  Adj_lists adj_lists(n);

  // read weigts
  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);
      adj_lists.at(u).push_back(node(v, c));
    }
  }

  // initialize cost
  vector<int> d(n, INT_MAX);
  d.at(0) = 0;
  priority_queue<node, vector<node>, less_cost> pq;
  pq.push(node(0, d.at(0)));
  vector<bool> is_belong_to_sssp(n, false);

  // dijkstra's algorithm
  while (!pq.empty()) {
    // extract node u in V-S that has minimum cost
    const node u = pq.top();
    pq.pop();

    // if u's cost is larger than d[u],
    // than ignore this one
    if (u.second > d.at(u.first))
      continue;

    is_belong_to_sssp.at(u.first) = true;

    // update node in V-S and adjacent to u
    for (const node &v : adj_lists.at(u.first)) {
      if (!is_belong_to_sssp.at(v.first) &&
          d.at(u.first) + v.second < d.at(v.first)) {
        d.at(v.first) = d.at(u.first) + v.second;
        pq.push(node(v.first, d.at(v.first)));
      }
    }
  }

  // output
  for (int i = 0; i < n; i++) {
    printf("%d %d\\n", i, d.at(i));
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> node;
typedef vector<node> Adj_list;
typedef vector<Adj_list> Adj_lists;

class less_cost {
public:
  bool operator()(const pair<int, int> lhs, const pair<int, int> rhs) const {
    return lhs.second < rhs.second;
  }
};

int main() {

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

  Adj_lists adj_lists(n);

  // read weigts
  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);
      adj_lists.at(u).push_back(node(v, c));
    }
  }

  // initialize cost
  vector<int> d(n, INT_MAX);
  d.at(0) = 0;
  priority_queue<node, vector<node>, less_cost> pq;
  pq.push(node(0, d.at(0)));
  vector<bool> is_belong_to_sssp(n, false);

  // dijkstra's algorithm
  while (!pq.empty()) {
    // extract node u in V-S that has minimum cost
    const node u = pq.top();
    pq.pop();
    is_belong_to_sssp.at(u.first) = true;

    // if u's cost is larger than d[u],
    // than ignore this one
    if (u.second < d.at(u.first))
      continue;

    // update node in V-S and adjacent to u
    for (const node &v : adj_lists.at(u.first)) {
      if (!is_belong_to_sssp.at(v.first) &&
          d.at(u.first) + v.second < d.at(v.first)) {
        d.at(v.first) = d.at(u.first) + v.second;
        pq.push(node(v.first, d.at(v.first)));
      }
    }
  }

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

  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

class node {
public:
  int to;
  int from;
  int cost;
};

int main(void) {

  int n;
  scanf("%d", &n);
  vector<vector<node>> v(n);
  for (int i = 0; i < n; i++) {
    int u, k;
    scanf("%d %d", &u, &k);
    for (int j = 0; j < k; j++) {
      int vv, c;
      scanf("%d %d", &vv, &c);
      node t;
      t.from = u;
      t.to = vv;
      t.cost = c;
      v[u].push_back(t);
    }
  }

  vector<int> dist(n, inf);
  vector<bool> vis(n, false);

  priority_queue<pair<int, int>> pq;
  pq.push(make_pair(0, 0));
  dist[0] = 0;
  vis[0] = true;
  while (!pq.empty()) {
    pair<int, int> pos = pq.top();
    pq.pop();
    int p = pos.second;
    vis[p] = true;
    for (int i = 0; i < v[p].size(); i++) {
      if (vis[v[p][i].to])
        continue;
      int tmp = dist[v[p][i].from] + v[p][i].cost;
      if (dist[v[p][i].to] <= tmp)
        continue;

      dist[v[p][i].to] = tmp;
      pq.push(make_pair(dist[v[p][i].to] * -1, v[p][i].to));
    }
  }

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

  return 0;
}

// EOF

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

class node {
public:
  int to;
  int from;
  int cost;
};

int main(void) {

  int n;
  scanf("%d", &n);
  vector<vector<node>> v(n);
  for (int i = 0; i < n; i++) {
    int u, k;
    scanf("%d %d", &u, &k);
    for (int j = 0; j < k; j++) {
      int vv, c;
      scanf("%d %d", &vv, &c);
      node t;
      t.from = u;
      t.to = vv;
      t.cost = c;
      v[u].push_back(t);
    }
  }

  vector<int> dist(n, inf);
  vector<bool> vis(n, false);

  priority_queue<pair<int, int>> pq;
  pq.push(make_pair(0, 0));
  dist[0] = 0;
  vis[0] = true;
  while (!pq.empty()) {
    pair<int, int> pos = pq.top();
    pq.pop();
    int p = pos.second;
    vis[pos.second] = true;
    for (int i = 0; i < v[p].size(); i++) {
      if (p != v[p][i].from)
        continue;
      if (dist[v[p][i].to] < dist[v[p][i].from] + v[p][i].cost)
        continue;

      dist[v[p][i].to] =
          min(dist[v[p][i].to], dist[v[p][i].from] + v[p][i].cost);
      pq.push(make_pair(dist[v[p][i].to], v[p][i].to));
    }
  }

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

  return 0;
}

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

// Single Source Shortest Path II
#include <climits>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

static const int N = 10000;
static const int WHITE = 0;
static const int BLACK = 1;
int n, colors[N], d[N];
vector<pair<int, int>> G[N];

void dijkstra() {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      PQ;
  for (int i = 0; i < N; i++) {
    colors[i] = WHITE;
    d[i] = INT_MAX;
  }
  d[0] = 0;
  PQ.push(make_pair(0, 0));
  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    if (colors[u] == BLACK)
      continue;
    for (int i = 0; i < G[u].size(); i++) {
      if (d[G[u][i].first] > f.first + G[u][i].second) {
        d[G[u][i].first] = f.first + G[u][i].second;
        PQ.push(make_pair(d[G[u][i].first], G[u][i].first));
      }
    }
  }
}

int main() {
  int u, k, v, 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].push_back(make_pair(v, c));
    }
  }

  dijkstra();

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


Error: 0

Fixed submission:

// Single Source Shortest Path II
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

static const int N = 100;
static const int WHITE = 0;
static const int BLACK = 1;
int n, colors[N], d[N];
vector<pair<int, int>> G[N];

void dijkstra() {
  int u, mind;
  for (int i = 0; i < N; i++) {
    colors[i] = WHITE;
    d[i] = INT_MAX;
  }
  d[0] = 0;
  while (1) {
    u = -1;
    mind = INT_MAX;
    for (int i = 0; i < n; i++) {
      if (colors[i] == WHITE && d[i] < mind) {
        u = i;
        mind = d[i];
      }
    }
    if (u == -1)
      break;
    colors[u] = BLACK;
    for (int i = 0; i < G[u].size(); i++) {
      d[G[u][i].first] = min(d[G[u][i].first], mind + G[u][i].second);
    }
  }
}

int main() {
  int u, k, v, 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].push_back(make_pair(v, c));
    }
  }

  dijkstra();

  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
}
Problem p02243 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef vector<pair<int, int>> adj;

struct current_cost {
  int index, cost;
  bool operator<(const current_cost &cur) const { return cost > cur.cost; }
};

current_cost init(int i, int c) {
  current_cost cur;
  cur.index = i, cur.cost = c;
  return cur;
}

int main() {
  int i, j, k, n, m, next, cost;
  scanf("%d", &n);
  adj *adlist;
  adlist = new adj[n];

  for (k = 0; k < n; k++) {
    scanf("%d %d", &i, &m);
    for (j = 0; j < m; j++) {
      scanf("%d %d", &next, &cost);
      adlist[i].push_back(make_pair(next, cost));
    }
  }

  int *d;
  d = new int[n];
  d[0] = 0;
  for (i = 1; i < n; i++) {
    d[i] = 200000;
  }

  priority_queue<current_cost> C;
  current_cost cur = init(0, 0);
  C.push(cur);

  int size;
  bool *check;
  check = new bool[n];
  for (i = 0; i < n; i++) {
    check[i] = false;
  }

  while (!C.empty()) {
    cur = C.top();
    C.pop();
    if (check[cur.index])
      continue;
    check[cur.index] = true;

    i = cur.index;
    size = adlist[i].size();
    for (j = 0; j < size; j++) {
      k = adlist[i][j].first;
      if (d[i] + adlist[i][j].second < d[k]) {
        d[k] = d[i] + adlist[i][j].second;
        C.push(init(k, d[k]));
      }
    }
  };
  for (i = 0; i < n; i++) {
    printf("%d %d\\n", i, d[i]);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef vector<pair<int, int>> adj;

struct current_cost {
  int index, cost;
  bool operator<(const current_cost &cur) const { return cost < cur.cost; }
};

current_cost init(int i, int c) {
  current_cost cur;
  cur.index = i, cur.cost = c;
  return cur;
}

int main() {
  int i, j, k, n, m, next, cost;
  scanf("%d", &n);
  adj *adlist;
  adlist = new adj[n];

  for (k = 0; k < n; k++) {
    scanf("%d %d", &i, &m);
    for (j = 0; j < m; j++) {
      scanf("%d %d", &next, &cost);
      adlist[i].push_back(make_pair(next, cost));
    }
  }

  int *d;
  d = new int[n];
  d[0] = 0;
  for (i = 1; i < n; i++) {
    d[i] = 200000;
  }

  priority_queue<current_cost> C;
  current_cost cur = init(0, 0);
  C.push(cur);

  int size;
  while (!C.empty()) {
    cur = C.top();
    C.pop();
    if (cur.cost > d[cur.index])
      continue;

    i = cur.index;
    size = adlist[i].size();
    for (j = 0; j < size; j++) {
      k = adlist[i][j].first;
      if (d[i] + adlist[i][j].second < d[k]) {
        d[k] = d[i] + adlist[i][j].second;
        C.push(init(k, d[k]));
      }
    }
  };
  for (i = 0; i < n; i++) {
    printf("%d %d\\n", i, d[i]);
  }

  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <utility>
#include <vector>

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

struct Edge {
  int to, w;
};

typedef pair<int, int> P;

vector<Edge> adj[MAX_NODE];
int d[MAX_NODE];

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

  while (!heap.empty()) {
    P p = heap.top();
    heap.pop();
    int from = p.second;
    if (d[from] < p.first)
      continue;
    for (int i = 0; i < adj[from].size(); i++) {
      Edge e = adj[from][i];
      if (d[from] + e.w < d[e.to]) {
        d[e.to] = d[from] + e.w;
        heap.push({d[e.to], e.to});
      }
    }
  }
}

int main() {
  int n, u, k, c, 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({v, c});
    }
  }
  dijkstra(n, 0);
  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#define min(a, b) ((a) > (b)) ? (b) : (a)
int main(void) {
  int i, j, n, u, k, s, tmp, w, adj[100][100] = {0};
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d %d", &u, &k);
    for (j = 0; j < k; j++) {
      scanf("%d %d", &tmp, &w);
      adj[u][tmp] = w;
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if ((adj[i][j] == 0) && (i != j)) {
        adj[i][j] = 100000000;
      }
    }
  }
  for (s = 0; s < n; s++) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][s] + adj[s][j]);
      }
    }
  }
  for (i = 0; i < n; i++) {
    printf("%d %d\\n", i, adj[0][i]);
  }
  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

static const int MAX = 10000;
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 color[MAX];
  int d[MAX];

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

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

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

    color[u] = BLACK;

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

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

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

  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;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

static const int MAX = 1000;
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 color[MAX];
  int d[MAX];

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

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

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

    color[u] = BLACK;

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

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

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

  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, verdict: Runtime Error

#include <iostream>
using namespace std;
#include <algorithm>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
bool b[2003];

#define REP(i, a) for (int i = 0; i < a; i++)
int N = (1 << 20);
int max = 101;
int main() {
  pair<int, int> f;
  vector<pair<int, int>> A[100000];
  priority_queue<pair<int, int>> PQ;
  int min, b, a, k, m, n, e, p[101], d[100000], u, v; // u ?????????v????????¨
  bool color[10001];

  cin >> n;
  REP(i, n) {
    cin >> u >> k;
    REP(i, k) {
      cin >> a >> b;
      if (b == -1)
        b = N;
      A[u].push_back(make_pair(a, b));
    }
    color[i] = 0;
    d[i] = N;
  }
  a = 0;
  d[0] = 0;
  PQ.push(make_pair(0, 0));
  while (!PQ.empty()) {
    f = PQ.top();
    PQ.pop();
    u = f.second;
    color[u] = 1;
    REP(i, A[u].size()) {
      v = A[u][i].first;
      if (color[v] == 1) {
        continue;
      }
      if (d[v] > A[u][i].second + d[u]) {
        d[v] = A[u][i].second + d[u];
        PQ.push(make_pair(d[v] * -1, v));
      }
    }
  }
  REP(i, n) { cout << i << " " << (d[i] == N ? -1 : d[i]) << endl; }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#include <algorithm>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
bool b[2003];

#define REP(i, a) for (int i = 0; i < a; i++)
int N = (1 << 20);
int main() {
  pair<int, int> f;
  vector<pair<int, int>> A[10001];
  priority_queue<pair<int, int>> PQ;
  int min, b, a, k, m, n, e, p[101], d[101], u, v; // u ?????????v????????¨
  bool color[10001];

  cin >> n;
  REP(i, n) {
    cin >> u >> k;
    REP(i, k) {
      cin >> a >> b;
      if (b == -1)
        b = N;
      A[u].push_back(make_pair(a, b));
    }
    color[i] = 0;
    d[i] = N;
  }
  a = 0;
  d[0] = 0;
  PQ.push(make_pair(0, 0));
  while (!PQ.empty()) {
    f = PQ.top();
    PQ.pop();
    u = f.second;
    color[u] = 1;
    REP(i, A[u].size()) {
      v = A[u][i].first;
      if (color[v] == 1) {
        continue;
      }
      if (d[v] > A[u][i].second + d[u]) {
        d[v] = A[u][i].second + d[u];
        PQ.push(make_pair(d[v] * -1, v));
      }
    }
  }
  REP(i, n) { cout << i << " " << (d[i] == N ? -1 : d[i]) << endl; }
  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main() {

  int n, aaa;
  cin >> n;
  vector<vector<int>> v(n), c(n);
  vector<int> k(n), ans(n), d(n);
  rep(i, n) {
    cin >> aaa >> k[i];
    v[i].resize(k[i]);
    c[i].resize(k[i]);
    rep(j, k[i]) { cin >> v[i][j] >> c[i][j]; }
    ans[i] = -1;
    d[i] = INF;
  }
  d[0] = 0;
  int m = INF, mi;
  rep(aaaa, n) {
    m = INF;
    rep(i, n) {
      if (ans[i] == -1 && d[i] < m) {
        m = d[i];
        mi = i;
      }
    }
    ans[mi] = m;
    rep(j, k[mi]) { d[v[mi][j]] = min(ans[mi] + c[mi][j], d[v[mi][j]]); }
  }

  rep(i, n) { cout << i << " " << ans[i] << endl; }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main() {

  int n, aaa;
  cin >> n;
  vector<vector<int>> v(n), c(n);
  vector<int> k(n), ans(n);
  rep(i, n) {
    cin >> aaa >> k[i];
    v[i].resize(k[i]);
    c[i].resize(k[i]);
    rep(j, k[i]) { cin >> v[i][j] >> c[i][j]; }
    ans[i] = -1;
  }
  ans[0] = 0;
  int m = INF, mi, mj;
  rep(aaaa, n - 1) {
    m = INF;
    rep(i, n) {
      if (ans[i] != -1) {
        rep(j, k[i]) {
          if (ans[v[i][j]] == -1 && m > ans[i] + c[i][j]) {
            m = ans[i] + c[i][j];
            mi = i;
            mj = j;
          }
        }
      }
    }
    ans[v[mi][mj]] = m;
  }
  rep(i, n) { cout << i << " " << ans[i] << endl; }
}
Problem p02243 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <vector>

using namespace std;

struct edge {
  int src;
  int dst;
  int weight;

  edge(int src, int dst, int w) : src(src), dst(dst), weight(w) {}
};

bool operator<(const edge &a, const edge &b) { return a.weight > b.weight; }

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

  vector<map<int, int>> G(n);
  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 (with pritority queue)
  vector<int> d(n, -1);
  vector<bool> visited(n, false);
  priority_queue<edge> Q;

  Q.push(edge(-1, 0, 0));
  while (!Q.empty()) {
    auto e = Q.top();
    Q.pop();
    if (visited[e.dst])
      continue;
    d[e.dst] = e.weight;
    visited[e.dst] = true;

    int i = e.dst;
    int minw = numeric_limits<int>::max();
    for (auto &x : G[i]) {
      if (visited[x.first])
        continue;
      int w = x.second + d[i];
      Q.push(edge(i, x.first, w));
    }
  }

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

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <vector>

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

struct edge {
  int src;
  int dst;
  int weight;

  edge(int src, int dst, int w) : src(src), dst(dst), weight(w) {}
};

bool operator<(const edge &a, const edge &b) { return a.weight > b.weight; }

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 (with pritority queue)
  vector<int> d(n, -1);
  vector<bool> visited(n, false);
  priority_queue<edge> Q;

  Q.push(edge(-1, 0, 0));
  while (!Q.empty()) {
    auto e = Q.top();
    Q.pop();
    if (visited[e.dst])
      continue;
    d[e.dst] = e.weight;
    visited[e.dst] = true;

    int i = e.dst;
    int minw = numeric_limits<int>::max();
    for (int j = 0; j < n; ++j) {
      if (visited[j] || G[i][j] < 0)
        continue;
      int w = G[i][j] + d[i];
      Q.push(edge(i, j, w));
    }
  }

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

  return 0;
}