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

#include <bits/stdc++.h>
using namespace std;
#define MAX 100
#define INF 1 << 25
int n, cost[MAX][MAX];
int mincost[MAX];
bool used[MAX];
int prim() {
  for (int i = 0; i < n; i++)
    mincost[i] = INF, used[i] = false;
  mincost[0] = 0;
  int res = 0;
  while (1) {
    int v = -1;
    for (int u = 0; u < n; u++)
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    if (v == -1)
      break;
    used[v] = 1;
    res += mincost[v];
    for (int u = 0; u < n; u++)
      mincost[u] = min(mincost[u], cost[v][u]);
  }
  return res;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      cost[i][j] = (e == -1) ? INF : e;
    }
  }
  cout << prim() << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define r(i, a, n) for (int i = a; i < n; i++)
using namespace std;
int main() {
  int n, p;
  while (cin >> n >> p, n) {
    int q, a[10001][1262], ch[10001][1262], b, c, d, e;
    r(i, 0, p) r(j, 0, 1261) ch[i][j] = a[i][j] = 0;
    cin >> q;
    r(i, 0, q) {
      cin >> b >> c >> d >> e;
      if (e == 1) {
        a[d - 1][b]++;
        ch[d - 1][b] = c;
      } else {
        for (int j = b - 1; j >= 0; j--) {
          if (ch[d - 1][j] == c)
            break;
          a[d - 1][j]++;
        }
      }
    }
    cin >> q;
    while (q--) {
      int s = 0;
      cin >> b >> c >> d;
      r(i, b, c) if (a[d - 1][i]) s++;
      cout << s << endl;
    }
  }
  return 0;
}
Problem p02242 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int n, m, i, j, a, w;
  int *adj[100];
  int *dist[100];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> i >> m;
    adj[i] = new int[m + 1];
    dist[i] = new int[m + 1];
    for (j = 0; j < m; j++) {
      cin >> a >> w;
      adj[i][j] = a;
      dist[i][j] = w;
    }
    adj[i][m] = -1;
    dist[i][m] = -1;
  }
  int *d, *prev, *Q;
  d = new int[n];
  prev = new int[n];
  Q = new int[n];
  for (i = 0; i < n; i++) {
    d[i] = 200000;
    prev[i] = -1;
    Q[i] = i;
  }
  int rest = n;

  int u, v, min;
  d[0] = 0;
  while (rest > 0) {
    min = 200000;
    for (i = 0; i < n; i++) {
      if (min > d[i] && Q[i] >= 0) {
        min = d[i];
        u = i;
      }
    }
    Q[u] = -1;
    rest--;

    for (j = 0; adj[u][j] >= 0; j++) {
      v = adj[u][j];
      if (d[v] > d[u] + dist[u][j]) {
        d[v] = d[u] + dist[u][j];
        prev[v] = u;
      }
    }
  };
  for (i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
  for (i = 0; i < n; i++) {
    delete adj[i];
    delete dist[i];
  }
  delete d;
  delete prev;
  delete Q;
  cin >> n;

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
#include <climits>
#include <stdio.h>

#define INFTY INT_MAX

int main() {
  int n, m, i, j, a, w;
  int *adj[100];
  int *dist[100];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d %d", &i, &m);
    adj[i] = new int[m + 1]; // adjacensy list.
    dist[i] = new int[m + 1];
    for (j = 0; j < m; j++) {
      scanf("%d %d", &a, &m);
      adj[i][j] = a;
      dist[i][j] = w;
    }
    adj[i][m] = -1;
    dist[i][m] = -1;
  }

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

  int u, v, min;
  d[0] = 0; // 0 is start.
  while (rest > 0) {
    min = 200000;
    for (i = 0; i < n; i++) {
      if (min > d[i] && Q[i] >= 0) {
        min = d[i];
        u = i;
      }
    }
    Q[u] = -1;
    rest--;
    for (j = 0; adj[u][j] >= 0; j++) {
      v = adj[u][j];
      if (d[v] > d[u] + dist[u][j]) {
        d[v] = d[u] + dist[u][j];
        prev[v] = u;
      }
    }
  };
  for (i = 0; i < n; i++) {
    printf("%d %d\\n", i, d[i]);
  }
  for (i = 0; i < n; i++) {
    delete adj[i];
    delete dist[i];
  }
  delete d;
  delete prev;
  delete Q;

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

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

Error: MLE

Fixed submission:

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

#include <iostream>
#include <vector>
using namespace std;

int n;
const int INF = 1000000;
vector<vector<int>> v, c;
vector<int> w;

void trip(int id, int dp) {
  if (dp > n)
    return;

  for (int j = 0; j < (int)v[id].size(); j++) {
    if (w[v[id][j]] > w[id] + c[id][j]) {
      w[v[id][j]] = w[id] + c[id][j];
      trip(v[id][j], dp + 1);
    }
  }
}

int main() {
  cin >> n;

  int u, k, v1, c1;
  v.resize(n);
  c.resize(n);
  w.assign(n, INF);

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

  w[0] = 0;
  trip(0, 1);

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int n;
const int INF = 1000000;
vector<vector<int>> v, c;
vector<int> w;

void trip(int id, int dp) {
  if (dp > n)
    return;

  for (int j = 0; j < (int)v[id].size(); j++) {
    if (w[v[id][j]] > w[id] + c[id][j])
      w[v[id][j]] = w[id] + c[id][j];
    trip(v[id][j], dp + 1);
  }
}

int main() {
  cin >> n;

  int u, k, v1, c1;
  v.resize(n);
  c.resize(n);
  w.assign(n, INF);

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

  w[0] = 0;
  trip(0, 1);

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

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

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

  vector<vector<int>> cost(N, vector<int>(N, 10000001));
  cost[0][0] = 0;
  pair<int, int> ddata;
  for (int i = 0; i < N; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> ddata.first >> ddata.second;
      cost[u][ddata.first] = ddata.second;
    }
  }
  //

  vector<int> maxcost(N, 10000001);

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;

  que.push(pair<int, int>(0, 0));

  while (!que.empty()) {
    int n = que.top().second;
    if (que.top().first < maxcost[n]) {
      maxcost[n] = que.top().first;
    }
    for (int j = 1; j < N; j++) {
      if (que.top().first + cost[n][j] < maxcost[j]) {
        maxcost[j] = que.top().first + cost[n][j];
        ddata.first = que.top().first + cost[n][j];
        ddata.second = j;
        que.push(ddata);
      }
    }
    que.pop();
  }

  for (int i = 0; i < N; i++) {
    cout << i << ' ' << maxcost[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  bitset<100> data(1);

  vector<vector<int>> cost(N, vector<int>(N, 100001));
  cost[0][0] = 0;
  pair<int, int> ddata;
  for (int i = 0; i < N; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> ddata.first >> ddata.second;
      cost[u][ddata.first] = ddata.second;
    }
  }
  //
  int n;
  for (int i = 0; data.count() < N; i = n) {
    n = 1;
    for (int j = 1; j < N; j++) {
      if (i != j && cost[0][j] > cost[0][i] + cost[i][j]) {
        cost[0][j] = cost[0][i] + cost[i][j];
      }
      if ((!data[j] && cost[i][j] < cost[i][n]) || i == n) {
        n = j;
      }
    }
    data.set(n);
  }
  for (int i = 0; i < N; i++) {
    cout << i << ' ' << cost[0][i] << endl;
  }
  return 0;
}
Problem p02242 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define N 100
#define INF (1 << 30)
using namespace std;

int n, M[N][N], d[N], color[N] = {0};

void Dijk(int s) {
  int min;
  for (int i = 0; i < n; i++)
    d[i] = INF;
  d[s] = 0;
  color[s] = 1;
  while (true) {
    int u = -1;
    min = INF;
    for (int i = 0; i < n; i++) {
      if (color[i] != 2 && d[i] < min) {
        u = i;
        min = d[u];
      }
    }

    if (u == -1)
      break;

    color[u] = 2;

    for (int v = 0; v < n; v++) {
      if (color[v] != 2 && M[u][v] != INF)
        if (d[v] > d[u] + M[u][v]) {
          d[v] = d[u] + M[u][v];
          color[v] = 1;
        }
    }
  }

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

int main() {

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      M[i][j] = INF;

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

  Dijk(0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define N 100
#define INF (1 << 30)
using namespace std;

int n, M[N][N], d[N], color[N] = {0};

void Dijk(int s) {
  int min;
  for (int i = 0; i < n; i++)
    d[i] = INF;
  d[s] = 0;
  while (true) {
    int u = -1;
    min = INF;
    for (int i = 0; i < n; i++) {
      if (color[i] == 0 && d[i] < min)
        min = d[i];
      u = i;
    }

    if (u == -1)
      break;

    color[u] = 1;

    for (int v = 0; v < n; v++) {
      if (color[v] == 0 && M[u][v] != INF)
        if (d[v] > d[u] + M[u][v]) {
          d[v] = d[u] + M[u][v];
        }
    }
  }

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

int main() {

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      M[i][j] = INF;

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

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

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
using namespace std;
const int INF = 2000000000;
const int num = 100;
int n, u, k, v, w;
int m[num][num];
int d[num];
int c[num];
int main() {
  cin >> n;
  fill(m[0], m[num], INF);
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> w;
      m[u][v] = w;
    }
  }
  fill(d, d + num, INF);
  fill(c, c + num, 0);
  d[0] = 0;
  while (true) {
    int ne = -1;
    int minv = INF;
    for (int i = 0; i < n; i++)
      if (!c[i] && d[i] < minv) {
        ne = i;
        minv = d[i];
      }
    if (ne == -1)
      break;
    c[ne] = 1;
    for (int i = 0; i < n; i++) {
      if (d[i] > d[ne] + m[ne][i])
        d[i] = d[ne] + m[ne][i];
    }
  }
  for (int i = 0; i < n; i++)
    cout << i << " " << d[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
using namespace std;
const int INF = 2000000000;
const int num = 100;
int n, u, k, v, w;
int m[num][num];
int d[num];
int c[num];
int main() {
  cin >> n;
  fill(m[0], m[num], INF);
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; i++) {
      cin >> v >> w;
      m[u][v] = w;
    }
  }
  fill(d, d + num, INF);
  fill(c, c + num, 0);
  d[0] = 0;
  while (true) {
    int ne = -1;
    int minv = INF;
    for (int i = 0; i < n; i++)
      if (!c[i] && d[i] < minv) {
        ne = i;
      }
    if (ne == -1)
      break;
    c[ne] = 1;
    for (int i = 0; i < n; i++) {
      if (d[i] > d[ne] + m[ne][i])
        d[i] = d[ne] + m[ne][i];
    }
  }
  for (int i = 0; i < n; i++)
    cout << i << " " << d[i] << endl;
  return 0;
}
Problem p02242 (C++) — buggy submission, verdict: 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;
}

Error: TLE

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

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

#define INF 1000000000
using namespace std;

struct edge {
  int from;
  int to;
  int w;
};
struct node {
  node() : id{-1}, d{INF}, ns{} {};
  node(int i, vector<edge> &vec) : id{i}, d{INF}, ns{vec} {};
  int id;
  int d;
  vector<edge> ns;
};

vector<int> dijkstra(node *graph, int n, int st) {
  auto ds = vector<int>(n, INF);
  auto comp = [](node *x, node *y) { return x->d < y->d; };
  priority_queue<node *, vector<node *>, decltype(comp)> que(comp);
  node *start = &graph[st];
  start->d = 0;
  que.push(start);
  while (que.size()) {
    node *n = que.top();
    que.pop();
    if (n->d < ds[n->id]) {
      ds[n->id] = n->d;
      for (auto e : n->ns) {
        node *next_n = &graph[e.to];
        next_n->d = min(next_n->d, n->d + e.w);
        que.push(next_n);
      }
    }
  }
  return ds;
}

int main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  int n;
  cin >> n;
  node graph[n];
  for (int i = 0; i < n; ++i) {
    int id;
    int m;
    cin >> id >> m;
    auto vec = vector<edge>(m);
    int v, c;
    for (int j = 0; j < m; ++j) {
      cin >> v >> c;
      vec[j] = edge{id, v, c};
    }
    graph[id] = node(id, vec);
  }
  auto ds = dijkstra(graph, n, 0);
  for (int i = 0; i < n; ++i) {
    cout << i << ' ' << ds[i] << endl;
  }
}

Error: TLE

Fixed submission:

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

#define INF 1000000000
using namespace std;

struct edge {
  int from;
  int to;
  int w;
};
struct node {
  node() : id{-1}, d{INF}, ns{} {};
  node(int i, vector<edge> &vec) : id{i}, d{INF}, ns{vec} {};
  int id;
  int d;
  vector<edge> ns;
};

vector<int> dijkstra(node *graph, int n, int st) {
  auto ds = vector<int>(n, INF);
  auto comp = [](node &x, node &y) { return x.d < y.d; };
  priority_queue<node, vector<node>, decltype(comp)> que(comp);
  node start = graph[st];
  start.d = 0;
  que.push(start);
  while (que.size()) {
    node n = que.top();
    que.pop();
    if (n.d < ds[n.id]) {
      ds[n.id] = n.d;
      for (auto e : n.ns) {
        node next_n = graph[e.to];
        next_n.d = n.d + e.w;
        que.push(next_n);
      }
    }
  }
  return ds;
}

int main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  int n;
  cin >> n;
  node graph[n];
  for (int i = 0; i < n; ++i) {
    int id;
    int m;
    cin >> id >> m;
    auto vec = vector<edge>(m);
    int v, c;
    for (int j = 0; j < m; ++j) {
      cin >> v >> c;
      vec[j] = edge{id, v, c};
    }
    graph[id] = node(id, vec);
  }
  auto ds = dijkstra(graph, n, 0);
  for (int i = 0; i < n; ++i) {
    cout << i << ' ' << ds[i] << endl;
  }
}
Problem p02242 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#define MAX 120000
using namespace std;

int matrix[105][105], n, closest[105], close[105];

void djst() {
  int i, j, k;
  for (i = 1; i < n; i++) {
    if (matrix[0][i] != -1)
      close[i] = matrix[0][i];
  }
  while (1) {
    int mi = MAX, rec = -1;
    for (i = 1; i < n; i++) {
      if (close[i] != -1 && close[i] < mi && closest[i] == -1) {
        mi = close[i];
        rec = i;
      }
    }
    if (rec == -1)
      break;
    closest[rec] = mi;
    close[rec] = MAX;
    for (j = 1; j < n; j++) {
      if (closest[j] == -1 && matrix[rec][j] != -1) {
        if (mi + matrix[rec][j] < close[j] || close[j] == -1)
          close[j] = mi + matrix[rec][j];
      }
    }
  }
}
int main() {
  memset(close, -1,
         sizeof(close)); // memset??????????????¨0???-1???????????°???????§????
  memset(closest, -1, sizeof(closest));
  memset(matrix, -1, sizeof(matrix));
  int i, j;
  cin >> n;
  for (i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    for (j = 0; j < b; j++) {
      int c, d;
      cin >> c >> d;
      matrix[a][c] = d;
    }
  }
  closest[0] = 0;
  djst();
  for (i = 0; i < n; i++) {
    cout << i << " " << closest[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#define MAX 120000
using namespace std;

int matrix[105][105], n, closest[105], close[105];

void djst() {
  int i, j, k;
  for (i = 1; i < n; i++) {
    if (matrix[0][i] < MAX)
      close[i] = matrix[0][i];
  }
  while (1) {
    int mi = MAX, rec = -1;
    for (i = 1; i < n; i++) {
      if (close[i] < mi && closest[i] == -1) {
        mi = close[i];
        rec = i;
      }
    }
    if (rec == -1)
      break;
    closest[rec] = mi;
    close[rec] = MAX;
    i = 2;
    while (i < n) {
      for (j = 0; j < n; j++) {
        if (closest[j] == -1 && mi + matrix[rec][j] < close[j])
          close[j] = mi + matrix[rec][j];
      }
    }
  }
}

int main() {
  memset(close, MAX, sizeof(close));
  memset(closest, -1, sizeof(closest));
  memset(matrix, MAX, sizeof(matrix));
  int i, j;
  cin >> n;
  for (i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    for (j = 0; j < b; j++) {
      int c, d;
      cin >> c >> d;
      matrix[a][c] = d;
    }
  }
  closest[0] = 0;
  djst();
  for (i = 0; i < n; i++) {
    cout << i << " " << closest[i] << endl;
  }
  return 0;
}
Problem p02242 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <limits>

constexpr int MAXN = 100;
int G[MAXN][MAXN];
int P[MAXN];
int D[MAXN];
int C[MAXN];

constexpr int MAXINT = std::numeric_limits<int>::max();

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

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

  for (int i = 0; i < n; i++) {
    D[i] = MAXINT;
  }

  for (int i = 0; i < n; i++) {
    int id, e_num;
    std::cin >> id >> e_num;
    for (int j = 0; j < e_num; j++) {
      int t;
      std::cin >> t;
      std::cin >> G[id][t];
    }
  }

  int r = 0;
  D[r] = 0;
  while (true) {
    int mincost = MAXINT;
    int m;
    for (int i = 0; i < n; i++) {
      if ((C[i] == 0) && (D[i] < mincost)) {
        mincost = D[i];
        m = i;
      }
    }

    if (mincost == MAXINT) {
      break;
    }

    C[m] = 1;
    for (int i = 0; i < n; i++) {
      if ((C[i] != 1) && (G[m][i] != MAXINT) && ((D[m] + G[m][i]) < D[i])) {
        P[i] = m;
        D[i] = D[m] + G[m][i];
      }
    }
  }

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


Error: TLE

Fixed submission:

#include <iostream>
#include <limits>

constexpr int MAXN = 100;
int G[MAXN][MAXN];
int P[MAXN];
int D[MAXN];
int C[MAXN];

constexpr int MAXINT = std::numeric_limits<int>::max();

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

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

  for (int i = 0; i < n; i++) {
    D[i] = MAXINT;
  }

  for (int i = 0; i < n; i++) {
    int id, e_num;
    std::cin >> id >> e_num;
    for (int j = 0; j < e_num; j++) {
      int t;
      std::cin >> t;
      std::cin >> G[id][t];
    }
  }

  int r = 0;
  D[r] = 0;
  C[r] = 1;
  while (true) {
    int mincost = MAXINT;
    int m;
    for (int i = 0; i < n; i++) {
      if (D[i] < mincost) {
        mincost = D[i];
        m = i;
      }
    }

    if (mincost == MAXINT) {
      break;
    }

    D[m] = mincost;
    C[m] = 1;
    for (int i = 0; i < n; i++) {
      if ((C[i] != 1) && ((D[m] + G[m][i]) < D[i])) {
        P[i] = m;
        D[i] = D[m] + G[m][i];
      }
    }
  }

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