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

#include <bits/stdc++.h>

using namespace std;

#define llong long long
typedef pair<long int, int> P;
long int d[10001];
bool Dec[10001];
vector<P> edges[10001];
priority_queue<P, vector<P>, greater<P>> q;

void solve() {
  fill_n(d, 10001, 9999999);
  d[0] = 0;
  fill_n(Dec, 10001, false);
  q.push(P(0, 0));

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (Dec[p.second] == true)
      continue;
    Dec[p.second] = true;
    d[p.second] = p.first;

    for (int i = 0; i < edges[p.second].size(); i++) {
      P e = edges[p.second][i];
      if (d[e.second] > p.first + e.first) {
        q.push(P(p.first + e.first, e.second));
      }
    }
  }

  return;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int num;
    int howedge;
    cin >> num >> howedge;
    for (int j = 0; j < howedge; j++) {
      int to;
      long int cost;
      cin >> to >> cost;
      edges[num].push_back(P(cost, to));
    }
  }

  solve();

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define llong long long
typedef struct data {
  vector<int> way;
  vector<int> waycost;
  int useway;
  int cost;
} data;

data d[10001];

void solve(int N, int before) {
  vector<int> nsolve;
  for (int i = 0; i < d[N].way.size(); i++) {
    int nway = d[N].way[i];
    if (d[N].cost + d[N].waycost[i] < d[nway].cost) {
      d[nway].cost = min(d[d[N].way[i]].cost, d[N].cost + d[N].waycost[i]);
      d[nway].useway = N;
      nsolve.push_back(nway);
    }
  }
  for (int i = 0; i < nsolve.size(); i++) {
    solve(nsolve[i], 0);
  }
}

int main() {
  int hownum;
  cin >> hownum;
  for (int i = 0; i < hownum; i++) {
    int num;
    int howway;
    cin >> num >> howway;
    for (int j = 0; j < howway; j++) {
      int w, c;
      cin >> w >> c;
      d[num].way.push_back(w);
      d[num].waycost.push_back(c);
    }

    if (num == 0)
      d[num].cost = 0;
    else
      d[num].cost = 10000000;
  }

  for (int i = 0; i < hownum; i++) {
    solve(i, -1);
  }

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

#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> Edge;
int main() {
  int n;
  cin >> n;
  vector<vector<Edge>> lists;

  for (int i = 0; i < n; i++) {
    vector<Edge> list;
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      Edge edge;
      cin >> edge.second >> edge.first;
      list.push_back(edge);
    }
    lists.push_back(list);
  }

  vector<int> costs(n, -1);
  costs[0] = 0;
  priority_queue<Edge, vector<Edge>, greater<Edge>> queue;
  queue.push(Edge(0, 0));

  while (!queue.empty()) {
    Edge edge = queue.top();
    queue.pop();
    int dest = edge.second;
    int cost = edge.first;
    if (costs[dest] < cost)
      continue;
    for (Edge next : lists[dest]) {
      next.first += cost;
      if (costs[next.second] > next.first || costs[next.second] == -1) {
        costs[next.second] = next.first;
        queue.push(next);
      }
    }
  }
  for (int i = 0; i < n; i++)
    cout << i << " " << costs[i] << endl;

  char c;
  cin >> c;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> Edge;
int main() {
  int n;
  cin >> n;
  vector<vector<Edge>> lists;

  for (int i = 0; i < n; i++) {
    vector<Edge> list;
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      Edge edge;
      cin >> edge.second >> edge.first;
      list.push_back(edge);
    }
    lists.push_back(list);
  }

  vector<int> costs(n, -1);
  costs[0] = 0;
  priority_queue<Edge> queue;
  queue.push(Edge(0, 0));

  while (!queue.empty()) {
    Edge edge = queue.top();
    queue.pop();
    int dest = edge.second;
    int cost = edge.first;
    if (costs[dest] < cost)
      continue;
    for (Edge next : lists[dest]) {
      next.first += cost;
      if (costs[next.second] > next.first || costs[next.second] == -1) {
        costs[next.second] = next.first;
        queue.push(next);
      }
    }
  }
  for (int i = 0; i < n; i++)
    cout << i << " " << costs[i] << endl;

  char c;
  cin >> c;

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

//
// Created by ?????°?????? on 2016/05/19.
//

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 10000;
const int INFTY = (1 << 20);
const int WHITE = 0;
const int GRAY = 1;
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] = 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;
}

Error: 0

Fixed submission:

//
// Created by ?????°?????? on 2016/05/19.
//

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 100;
const int INFTY = (1 << 21);
const int WHITE = 0;
const int GRAY = 1;
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] = 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, verdict: Runtime Error

//
//  main.cpp
//
//
//  Created by ???????????? on 2017/10/23.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>

#define n_max 10000
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1 << 20

using namespace std;

int n, color[n_max], d[n_max], p[n_max];

vector<pair<int, int>> adj[n_max];

void init() {
  int i, j, id, num, node, weight;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%d %d", &id, &num);
    for (j = 0; j < num; j++) {
      scanf("%d", &node);
      scanf("%d", &weight);
      adj[id].push_back(make_pair(node, weight));
    }
    color[i] = WHITE;
    d[i] = INFTY;
  }
}

void dijkstra() {
  // initializing
  d[0] = 0;
  p[0] = -1;

  priority_queue<pair<int, int>> PQ;

  PQ.push(make_pair(0, 0));

  int u;
  while (!PQ.empty()) {
    // first weight, second idx
    pair<int, int> f = PQ.top();
    PQ.pop();
    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) {
        if ((d[u] + adj[u][j].second) < d[v]) {
          d[v] = adj[u][j].second + d[u];
          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 argc, const char *argv[]) {
  init();
  dijkstra();
  return 0;
}

Error: 0

Fixed submission:

//
//  main.cpp
//  minnimum_spanning_tree
//
//  Created by ???????????? on 2017/10/23.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>

#define n_max 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1 << 21

using namespace std;

int n, M[n_max][n_max], color[n_max], d[n_max], p[n_max];

void init() {
  int i, j, id, num, node;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      M[i][j] = INFTY;
    }
  }

  for (i = 0; i < n; i++) {
    scanf("%d %d", &id, &num);
    for (j = 0; j < num; j++) {
      scanf("%d", &node);
      scanf("%d", &M[id][node]);
    }
    color[i] = WHITE;
    d[i] = INFTY;
  }
}

void dijkstra() {
  // initializing
  d[0] = 0;
  p[0] = -1;

  int u;
  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 v = 0; v < n; v++) {
      if ((color[v] != BLACK) && (M[u][v] != INFTY)) {
        if ((d[u] + M[u][v]) < d[v]) {
          d[v] = M[u][v] + d[u];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i << " " << d[i] << endl;
  }
}

int main(int argc, const char *argv[]) {
  init();
  dijkstra();
  //    std::cout << "Hello, World!\\n";
  return 0;
}
Problem p02243 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

typedef pair<int, int> pii;

struct Edge {
  int t, c;
  Edge(int t, int c) : t(t), c(c) {}
};

vector<Edge> edges[10000];
int d[10000];

void dijkstra(int n) {
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  d[0] = 0;
  pq.push(make_pair(0, 0));

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

    if (d[tp.second] < tp.first) {
      continue;
    }

    for (int i = 0; i != edges[tp.second].size(); i++) {
      if (d[tp.second] + edges[tp.second][i].c < d[edges[tp.second][i].t]) {
        d[edges[tp.second][i].t] = d[tp.second] + edges[tp.second][i].c;
        pq.push(make_pair(d[edges[tp.second][i].t], edges[tp.second][i].t));
      }
    }
  }
}

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

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

  for (int i = 0; i < n; i++) {
    int id, k;
    cin >> id >> k;
    for (int j = 0; j < k; j++) {
      int v, c;
      cin >> v >> c;

      edges[id].push_back(Edge(v, c));
    }
  }

  dijkstra(n);

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

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int d[10000][10000];
int weight[10000];
bool checked[10000];

void prim(int n) {
  weight[0] = 0;

  while (true) {
    int t = -1;

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

    if (t == -1) {
      break;
    }

    checked[t] = true;

    for (int i = 0; i < n; i++) {
      if (!checked[i]) {
        weight[i] = min(weight[i], weight[t] + d[t][i]);
      }
    }
  }
}

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

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

  for (int i = 0; i < n; i++) {
    d[i][i] = 0;
    weight[i] = 100000000;
    checked[i] = false;
  }

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

  prim(n);

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

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

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

const int MAX = 10005;
int state[MAX], N, D[MAX];
vector<pair<int, int>> S[MAX];
priority_queue<pair<int, int>> Q;

void dijkstra() {
  int cnt = 1;
  for (int i = 0; i < N; i++) {
    state[i] = 0;
    D[i] = 10000000;
  }
  state[0] = 1;
  D[0] = 0;
  for (int i = 0; i < S[0].size(); i++) {
    Q.push(make_pair(-S[0][i].second, S[0][i].first));
    D[S[0][i].first] = min(D[S[0][i].first], D[0] + S[0][i].second);
  }
  while (1) {
    int a = Q.top().second;
    if (state[a] == 0) {
      Q.pop();
      state[a] = 1;
      for (int i = 0; i < S[a].size(); i++) {
        if (state[S[a][i].first] == 0) {
          Q.push(make_pair(-(D[a] + S[a][i].second), S[a][i].first));
          D[S[a][i].first] = min(D[S[a][i].first], D[a] + S[a][i].second);
        }
      }
      cnt++;
    } else
      Q.pop();
    if (cnt == N)
      break;
  }
  return;
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    int l, a;
    cin >> l >> a;
    for (int j = 0; j < a; j++) {
      int b, c;
      cin >> b >> c;
      S[i].push_back(make_pair(b, c));
    }
  }
  dijkstra();
  for (int i = 0; i < N; i++) {
    cout << i << " " << D[i] << endl;
  }
}


Error: 0

Fixed submission:

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

const int MAX = 105;
int state[MAX], N, D[MAX];
vector<pair<int, int>> S[MAX];
priority_queue<pair<int, int>> Q;

void dijkstra() {
  int cnt = 1;
  for (int i = 0; i < N; i++) {
    state[i] = 0;
    D[i] = 10000000;
  }
  state[0] = 1;
  D[0] = 0;
  for (int i = 0; i < S[0].size(); i++) {
    Q.push(make_pair(-S[0][i].second, S[0][i].first));
    D[S[0][i].first] = min(D[S[0][i].first], D[0] + S[0][i].second);
  }
  while (1) {
    int a = Q.top().second;
    if (state[a] == 0) {
      Q.pop();
      state[a] = 1;
      for (int i = 0; i < S[a].size(); i++) {
        if (state[S[a][i].first] == 0) {
          Q.push(make_pair(-(D[a] + S[a][i].second), S[a][i].first));
          D[S[a][i].first] = min(D[S[a][i].first], D[a] + S[a][i].second);
        }
      }
      cnt++;
    } else
      Q.pop();
    if (cnt == N)
      break;
  }
  return;
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    int l, a;
    cin >> l >> a;
    for (int j = 0; j < a; j++) {
      int b, c;
      cin >> b >> c;
      S[i].push_back(make_pair(b, c));
    }
  }
  dijkstra();
  for (int i = 0; i < N; i++) {
    cout << i << " " << D[i] << endl;
  }
}
Problem p02243 (C++) — buggy submission, verdict: 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[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;
}

Error: 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[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;
}
Problem p02243 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
static const int MAX = 10000;
static const int INFTY = (1 << 20);
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] = 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;
        // priority_queueはデフォルトで大きい値を優先するため-1をかける
        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, 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 <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 p02243 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, u, k, v1, c1;
  const int INF = 1000000;

  cin >> n;

  vector<vector<int>> v(n), c(n);
  vector<int> w(n, INF);
  vector<bool> f(n, false);

  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;

  while (true) {
    int k = -1;
    for (int i = 0; i < n; i++)
      if (!f[i] && (k == -1 || w[i] < w[k]))
        k = i;

    if (k == -1)
      break;

    f[k] = true;

    for (int i = 0; i < (int)v[k].size(); i++)
      w[v[k][i]] = min(w[v[k][i]], w[k] + c[k][i]);
  }

  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 p02243 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int n;
int res = 0;
struct node {
  int id;
  int dist;
  bool searched;
};

node v[10000];
vector<pair<int, int>> edge[10000];

int main() {
  cin >> n;
  REP(i, n) {
    int u, k;
    cin >> u >> k;
    v[u].id = u;
    v[u].dist = INF;
    v[u].searched = false;
    REP(j, k) {
      int to, cost;
      cin >> to >> cost;
      edge[i].push_back(make_pair(to, cost));
    }
  }
  v[0].dist = 0;
  int prev = 0;
  REP(i, n) {
    int minimum = INF;
    REP(j, n) {
      if (!v[j].searched && minimum >= v[j].dist) {
        prev = j;
        minimum = v[j].dist;
      }
    }
    while (!edge[prev].empty()) {
      int s = edge[prev].size() - 1;
      int j = edge[prev][s].first;
      v[j].dist = min(v[j].dist, v[prev].dist + edge[prev][s].second);
      edge[prev].pop_back();
    }
    v[prev].searched = true;
  }
  REP(i, n) cout << i << " " << v[i].dist << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int n;
int res = 0;
struct node {
  int id;
  int dist;
  bool searched;
};

node v[10000];
int edge[10000][10000];

int main() {
  cin >> n;
  REP(i, n) {
    int u, k;
    cin >> u >> k;
    v[u].id = u;
    v[u].dist = INF;
    v[u].searched = false;
    REP(j, n) edge[i][j] = INF;
    REP(j, k) {
      int to, cost;
      cin >> to >> cost;
      edge[i][to] = cost;
    }
  }
  v[0].dist = 0;
  int prev = 0;
  REP(i, n) {
    int minimum = INF;
    REP(j, n) {
      if (!v[j].searched && minimum >= v[j].dist) {
        prev = j;
        minimum = v[j].dist;
      }
    }
    REP(j, n) {
      if (!v[j].searched && edge[prev][j] != INF) {
        v[j].dist = min(v[j].dist, v[prev].dist + edge[prev][j]);
      }
    }
    v[prev].searched = true;
  }
  REP(i, n) cout << i << " " << v[i].dist << endl;
  return 0;
}