alexjercan/bugnet huggingface dataset:
Problem p02242 (C++) — buggy submission, verdict: Time Limit Exceeded

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


Error: TLE

Fixed submission:

#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; 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;
          must_update[e.to] = true;
          upd_num++;
        }
      }
    }
  }

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

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

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
typedef pair<int, int> P;
#define to first
#define co second
#define fi first
#define se second
const int INF = INT_MAX;
const int MAX_V = 100000;
vector<P> G[MAX_V];
int v, d[MAX_V];
void dijk(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + v, INF);
  d[s] = 0;
  que.push(P(0, s));
  for (; !que.empty();) {
    P p = que.top();
    que.pop();
    int v = p.se;
    if (d[v] < p.fi)
      continue;
    for (int i = 0; i < (int)G[v].size(); i++) {
      P e = G[v][i];
      if (d[e.to] > d[v] + e.co) {
        d[e.to] = d[v] + e.co;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
signed main() {

  cin >> v;
  for (int i = 0, u, k; i < v && cin >> u >> k; i++)
    for (int j = 0, a, b; j < k && cin >> a >> b; j++)
      G[u].push_back(P(a, b));
  dijk(0);
  for (int i = 0; i < v; i++)
    cout << i << " " << d[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
const int inf = 1e9;
using namespace std;
int main() {

  int n, now = 0, next;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      g[100000], a;
  cin >> n;
  for (int i = 0, u, k, v, c; i < n && cin >> u >> k; i++)
    for (int j = 0; j < k && cin >> v >> c; j++)
      g[u].push(make_pair(c, v));
  int flag[100] = {}, c[100] = {};
  for (int i = 1; i < n; i++)
    c[i] = inf;
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    for (; !g[now].empty(); g[now].pop())
      if (!flag[g[now].top().second]) {
        c[g[now].top().second] =
            min(c[g[now].top().second], c[now] + g[now].top().first);
        a.push(make_pair(g[now].top().first, g[now].top().second));
        if (!cnt)
          next = g[now].top().second;
        cnt++;
      }
    flag[now] = 1;
    for (; flag[a.top().second] != 1;)
      a.pop();
    now = a.top().second;
  }
  for (int i = 0; i < n; i++)
    cout << i << " " << c[i] << endl;
  return 0;
}
Problem p02242 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define MAX_V 100
#define INF 1e9
using namespace std;
int cost[MAX_V][MAX_V];
int d[MAX_V];
bool used[MAX_V];
int V;
void dijkstra(int s) {
  for (int i = 0; i < MAX_V; i++) {
    d[i] = INF;
    used[i] = false;
  }
  d[s] = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && ((v == -1) || d[u] < d[v])) {
        v = u;
      }
    }
    // 全ての頂点が到達済み
    if (v == -1) {
      break;
    }
    // 集合に新たに頂点を追加
    used[v] = true;
    for (int u = 0; u < V; u++) {
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}

int main() {
  for (int i = 0; i < MAX_V; i++) {
    for (int j = 0; j < MAX_V; j++) {
      cost[i][j] = INF;
    }
  }
  cin >> V;
  for (int i = 0; i < V; i++) {
    int u;
    cin >> u;
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      int c;
      cin >> c;
      cost[u][v] = c;
    }
  }
  dijkstra(0);

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define MAX_V 100
#define INF 1e9
using namespace std;
int cost[MAX_V][MAX_V];
int d[MAX_V];
bool used[MAX_V];
int V;
void dijkstra(int s) {
  for (int i = 0; i < V; i++) {
    d[i] = INF;
    used[i] = false;
  }
  d[s] = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1) || d[u] < d[v]) {
        v = u;
      }
    }
    // 全ての頂点が到達済み
    if (v == -1) {
      break;
    }
    // 集合に新たに頂点を追加
    used[v] = true;
    for (int u = 0; u < V; u++) {
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}

int main() {
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      cost[i][j] = INF;
    }
  }
  cin >> V;
  for (int i = 0; i < V; i++) {
    int u;
    cin >> u;
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      int c;
      cin >> c;
      cost[u][v] = c;
    }
  }
  dijkstra(0);

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

#include <algorithm>
#include <iostream>
#include <vector>
#define MAX_V 100
#define INF 1e9
using namespace std;
int V;
int d[MAX_V];
int cost[MAX_V][MAX_V];
bool used[MAX_V];

void dijkstra(int s) {
  d[s] = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) {
        v = u;
      }
    }
    if (v == -1) {
      break;
    }
    used[v] = true;

    for (int u = 0; u < V; u++) {
      // if (cost[v][u] = INF)
      //{
      //	continue;
      // }
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}
int main() {
  cin >> V;
  for (int i = 0; i < MAX_V; i++) {
    for (int j = 0; j < MAX_V; j++) {
      cost[i][j] = INF;
    }
  }
  for (int i = 0; i < MAX_V; i++) {
    d[i] = INF;
    used[i] = false;
  }
  for (int i = 0; i < V; i++) {
    int u;
    cin >> u;
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int v_to;
      cin >> v_to;
      int c;
      cin >> c;

      cost[u][v_to] = c;
    }
  }

  dijkstra(0);

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

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define MAX_V 100
#define INF 1e9
using namespace std;
int V;
int d[MAX_V];
int cost[MAX_V][MAX_V];
bool used[MAX_V];

void dijkstra(int s) {
  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) {
        v = u;
      }
    }
    if (v == -1) {
      break;
    }
    used[v] = true;

    for (int u = 0; u < V; u++) {
      // if (cost[v][u] = INF)
      //{
      //	continue;
      // }
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}
int main() {
  cin >> V;
  for (int i = 0; i < MAX_V; i++) {
    for (int j = 0; j < MAX_V; j++) {
      cost[i][j] = INF;
    }
  }
  for (int i = 0; i < MAX_V; i++) {
    d[i] = INF;
    used[i] = false;
  }
  for (int i = 0; i < V; i++) {
    int u;
    cin >> u;
    int k;
    cin >> k;
    for (int j = 0; j < k; i++) {
      int v_to;
      cin >> v_to;
      int c;
      cin >> c;

      cost[u][v_to] = c;
    }
  }

  dijkstra(0);

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

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

#include <iostream>

using namespace std;

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 99999999

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

int dijkstra(int s) {

  int mincost;
  int u;

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

  while (true) {
    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 < n; i++) {
      if (color[i] != BLACK && M[u][i] != INFTY) {
        if (d[u] + M[u][i] < d[i]) {
          d[i] = d[u] + M[u][i];
          p[i] = u;
          color[i] = GRAY;
        }
      }
    }
  }

  int sum = 0;

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

  return sum;
}

int main(void) {

  cin >> n;

  int u;
  int k;
  int v;

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

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

  dijkstra(0);

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

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 999999999999999

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

int dijkstra(int s) {

  int mincost;
  int u;

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

  while (true) {
    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 < n; i++) {
      if (color[i] != BLACK && M[u][i] != -1) {
        if (d[u] + M[u][i] < d[i]) {
          d[i] = d[u] + M[u][i];
          p[i] = u;
          color[i] = GRAY;
        }
      }
    }
  }

  int sum = 0;

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

  return sum;
}

int main(void) {

  cin >> n;

  int u;
  int k;
  int v;

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

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

  return 0;
}
Problem p02242 (C++) — buggy submission, verdict: 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;
}


Error: TLE

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, verdict: Time Limit Exceeded

//
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define NIL -1
#define INFTY 1000000
#define TOTAL_INFTY INFTY *MAX_N

using namespace std;

enum Status {
  White,
  Gray,
  Black,
};

int n;
int w[MAX_N][MAX_N];
int d[MAX_N], parent[MAX_N];
Status st[MAX_N];

int min(int x, int y) {
  if (x < y)
    return x;
  return y;
}

void dijkstra() {
  for (int i = 0; i < n; i++) {
    d[i] = TOTAL_INFTY;
    st[i] = White;
    parent[i] = NIL;
  }

  int p = 0;
  d[p] = 0;

  while (true) {
    if (p == -1)
      break;
    st[p] = Black;

    for (int i = 0; i < n; i++) {
      if (w[p][i] != TOTAL_INFTY && st[i] != Black) {
        d[i] = min(d[i], d[p] + w[p][i]);
        st[i] = Gray;
      }
    }

    p = -1;
    int c = TOTAL_INFTY;
    for (int i = 0; i < n; i++) {
      if (st[i] == Gray && d[i] < c) {
        p = i;
        c = d[i];
      }
    }
  }
}

int main() {
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      w[i][j] = TOTAL_INFTY;
    }
  }

  cin >> n;

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

  dijkstra();

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

Error: TLE

Fixed submission:

//
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define NIL -1
#define INFTY 1000000

using namespace std;

enum Status {
  NotVisited,
  Visited,
};

int n;
int w[MAX_N][MAX_N];
Status st[MAX_N];

int min(int x, int y) {
  if (x < y)
    return x;
  return y;
}

int dfs(int v, int cost, int target) {
  if (v == target) {
    return cost;
  }

  int c = MAX_N * INFTY;

  st[v] = Visited;

  for (int i = 0; i < n; i++) {
    int x = w[v][i];
    if (x == INFTY || st[i] == Visited)
      continue;

    c = min(c, dfs(i, cost + x, target));
  }

  st[v] = NotVisited;

  return c;
}

int solve(int i) { return dfs(0, 0, i); }

int main() {
  for (int i = 0; i < MAX_N; i++) {
    st[i] = NotVisited;
    for (int j = 0; j < MAX_N; j++) {
      w[i][j] = INFTY;
    }
  }

  cin >> n;

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

  for (int i = 0; i < n; i++) {
    cout << i << " " << solve(i) << endl;
  }
}
Problem p02242 (C++) — buggy submission, verdict: 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 = 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;
}


Error: 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 = 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;
}
Problem p02242 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i, s, e) for (int i = s; i < e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100;

int weight[N_MAX][N_MAX];
int dis[N_MAX];

void set_shortest_distance(const int n) {
  bool ismember[n] = {false};

  int newmember = 0;
  dis[newmember] = 0;
  ismember[newmember] = true;

  FOR(t, 0, n - 1) {
    FOR(i, 0, n) {
      if (weight[newmember][i] < 0) {
        continue;
      }
      if (ismember[i]) {
        continue;
      }
      const int d = dis[newmember] + weight[newmember][i];
      if (dis[i] < 0 || d < dis[i]) {
        dis[i] = d;
      }
    }
    int min_i = -1, min_d = -1;
    FOR(i, 0, n) {
      if (dis[i] < 0) {
        continue;
      }
      if (ismember[i]) {
        continue;
      }
      if (min_d < 0 || dis[i] < min_d) {
        min_i = i;
        min_d = dis[i];
      }
    }
    newmember = min_i;
    ismember[newmember] = true;
    // cout << "#" << min_i << " " << min_d << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  FOR(i, 0, n) {
    dis[i] = -1;
    FOR(j, 0, n) { weight[i][j] = -1; }
  }

  int u, k, v, c;
  FOR(i, 0, n) {
    cin >> u >> k;
    FOR(j, 0, k) {
      cin >> v >> c;
      weight[u][v] = c;
    }
  }

  set_shortest_distance(n);

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i, s, e) for (int i = s; i < e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100;

int weight[N_MAX][N_MAX];
int dis[N_MAX];

void exhaust_search(const int cur, const int d, const int n,
                    std::array<bool, N_MAX> isvisited) {
  isvisited[cur] = true;
  if (dis[cur] < 0 || d < dis[cur]) {
    dis[cur] = d;
  }
  for (int next = 0; next < n; ++next) {
    if (isvisited[next]) {
      continue;
    }
    if (weight[cur][next] < 0) {
      continue;
    }
    exhaust_search(next, d + weight[cur][next], n, isvisited);
  }
}

void set_shortest_distance(const int n) {
  std::array<bool, N_MAX> isvisited;
  isvisited.fill(false);
  exhaust_search(0, 0, n, isvisited);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  FOR(i, 0, n) {
    dis[i] = -1;
    FOR(j, 0, n) { weight[i][j] = -1; }
  }

  int u, k, v, c;
  FOR(i, 0, n) {
    cin >> u >> k;
    FOR(j, 0, k) {
      cin >> v >> c;
      weight[u][v] = c;
    }
  }

  set_shortest_distance(n);

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

  return 0;
}
Problem p02242 (C++) — buggy submission, verdict: 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[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;
}


Error: 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[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;
}