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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
typedef pair<int, int> pii;
namespace MyLib {

class Dijkstra {
  struct edge {
    int to, cost;
    edge(int to_, int cost_) : to(to_), cost(cost_) {}
  };
  bool isdirected;
  vector<int> d;
  vector<vector<edge>> G;
  int V, E;

public:
  Dijkstra(int _V, bool _isdirected = true)
      : isdirected(_isdirected), V(_V), E(0), d(V), G(V) {}
  void add(int a, int b, int c) {
    G[a].push_back(edge(b, c));
    if (!isdirected)
      G[b].push_back(edge(a, c));
    E++;
  }
  void calc(int start) {
    fill(d.begin(), d.end(), INF);
    priority_queue<pii, vector<pii>, greater<pii>> que;
    d[start] = 0;
    que.push(pii(0, start));

    while (!que.empty()) {
      pii p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (size_t i = 0; i < G[v].size(); i++) {
        edge e = G[v][i];
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          que.push(pii(d[e.to], e.to));
        }
      }
    }
  }
  int dist(int to) { return d[to]; }
};
} // namespace MyLib
using namespace MyLib;
int main() {
  int v, e, r;
  cin >> v >> e >> r;
  Dijkstra dk(v);
  for (int i = 0, s, t, d; i < e; i++) {
    cin >> s >> t >> d;
    dk.add(s, t, d);
  }
  dk.calc(r);
  for (int i = 0; i < v; i++) {
    if (dk.dist(i) == INF)
      cout << "INF" << endl;
    else
      cout << dk.dist(i) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
typedef pair<int, int> pii;
namespace MyLib {

class Dijkstra {
  struct edge {
    int to, cost;
    edge(int to_, int cost_) : to(to_), cost(cost_) {}
  };
  bool isdirected;
  vector<int> d;
  vector<vector<edge>> G;
  int V, E;

public:
  Dijkstra(int _V, bool _isdirected = true)
      : isdirected(_isdirected), V(_V), E(0), d(_V), G(_V) {}
  void add(int a, int b, int c) {
    G[a].push_back(edge(b, c));
    if (!isdirected)
      G[b].push_back(edge(a, c));
    E++;
  }
  void calc(int start) {
    fill(d.begin(), d.end(), INF);
    priority_queue<pii, vector<pii>, greater<pii>> que;
    d[start] = 0;
    que.push(pii(0, start));

    while (!que.empty()) {
      pii p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (size_t i = 0; i < G[v].size(); i++) {
        edge e = G[v][i];
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          que.push(pii(d[e.to], e.to));
        }
      }
    }
  }
  int dist(int to) { return d[to]; }
};
} // namespace MyLib
using namespace MyLib;
int main() {
  int v, e, r;
  cin >> v >> e >> r;
  Dijkstra dk(v);
  for (int i = 0, s, t, d; i < e; i++) {
    cin >> s >> t >> d;
    dk.add(s, t, d);
  }
  dk.calc(r);
  for (int i = 0; i < v; i++) {
    if (dk.dist(i) == INF)
      cout << "INF" << endl;
    else
      cout << dk.dist(i) << endl;
  }
  return 0;
}
Problem p02362 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

struct Node {
  int to, from, cost;
};

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  vector<int> dist(n, inf);
  vector<Node> node(m);
  for (int i = 0; i < m; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    node[i].to = t;
    node[i].from = s;
    node[i].cost = d;
  }

  dist[r] = 0;
  int l = 0;
  while (true) {
    if (l > 1000000) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
    bool f = false;
    for (int i = 0; i < node.size(); i++) {
      Node d = node[i];
      if (dist[d.from] != inf && dist[d.to] > dist[d.from] + d.cost) {
        dist[d.to] = dist[d.from] + d.cost;
        f = true;
      }
    }
    if (!f)
      break;
    l++;
  }

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

  return 0;
}

// EOF

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

struct Node {
  int to, from, cost;
};

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  vector<int> dist(n, inf);
  vector<Node> node(m);
  for (int i = 0; i < m; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    node[i].to = t;
    node[i].from = s;
    node[i].cost = d;
  }

  dist[r] = 0;
  int l = 0;
  while (true) {
    if (l > 100000) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
    bool f = false;
    for (int i = 0; i < node.size(); i++) {
      Node d = node[i];
      if (dist[d.from] != inf && dist[d.to] > dist[d.from] + d.cost) {
        dist[d.to] = dist[d.from] + d.cost;
        f = true;
      }
    }
    if (!f)
      break;
    l++;
  }

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

  return 0;
}

// EOF
Problem p02362 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef pair<long long, int> P;

#define MAX_N 150000
#define INF 100000000000000
priority_queue<P, vector<P>, greater<P>> Q;
vector<pair<int, long long>> X[MAX_N];
int N, M, A, B, C, O;
long long dist[MAX_N];
void dijkstra() {
  for (int i = 0; i < MAX_N; i++) {
    dist[i] = INF;
  }
  Q.push(make_pair(0, O));
  dist[O] = 0;
  int cnt = 0;
  while (!Q.empty()) {
    cnt++;
    if (cnt >= 8000000 || Q.size() >= 5000000) {
      cout << "NEGATIVE CYCLE" << endl;
      return;
    }
    P pa = Q.top();
    Q.pop();
    long long a1 = pa.first;
    int a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      int to = X[a2][i].first;
      if (dist[to] > a1 + X[a2][i].second) {
        dist[to] = a1 + X[a2][i].second;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (dist[i] == INF) {
      cout << "INF" << endl;
    } else {
      cout << dist[i] << endl;
    }
  }
  return;
}
int main() {
  cin >> N >> M >> O;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
  }
  dijkstra();
  return 0;
}

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

typedef pair<long long, int> P;

#define MAX_N 150000
#define INF 100000000000000
priority_queue<P, vector<P>, greater<P>> Q;
vector<pair<int, long long>> X[MAX_N];
int N, M, A, B, C, O;
long long dist[MAX_N];
void dijkstra() {
  for (int i = 0; i < MAX_N; i++) {
    dist[i] = INF;
  }
  Q.push(make_pair(0, O));
  dist[O] = 0;
  int cnt = 0;
  while (!Q.empty()) {
    cnt++;
    if (cnt >= 8000000 || Q.size() >= 2000000) {
      cout << "NEGATIVE CYCLE" << endl;
      return;
    }
    P pa = Q.top();
    Q.pop();
    long long a1 = pa.first;
    int a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      int to = X[a2][i].first;
      if (dist[to] > a1 + X[a2][i].second) {
        dist[to] = a1 + X[a2][i].second;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (dist[i] == INF) {
      cout << "INF" << endl;
    } else {
      cout << dist[i] << endl;
    }
  }
  return;
}
int main() {
  cin >> N >> M >> O;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
  }
  dijkstra();
  return 0;
}
Problem p02362 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
#define int ll

signed main() {
  using pii = pair<int, int>;
  using piii = pair<pii, int>;
  int v, e, s;
  cin >> v >> e >> s;
  vector<int> d(v, LLONG_MAX);
  vector<piii> edge;
  rep(i, e) {
    int from, to, cost;
    cin >> from >> to >> cost;
    edge.push_back(piii(pii(from, to), cost));
  }
  d[s] = 0;
  rep(i, v) {
    bool update = false;
    rep(j, edge.size()) {
      int to = edge[j].first.second;
      int from = edge[j].first.first;
      int cost = edge[j].second;
      if (d[from] == LLONG_MAX)
        continue;
      if (d[to] > d[from] + cost) {
        d[to] = d[from] + cost;
        update = true;
      }
    }
    if (i == v - 1 && update)
      return puts("NEGATIVE CYCLE");
  }
  rep(i, v) if (d[i] != LLONG_MAX) cout << d[i] << endl;
  else cout << "INF" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
#define int ll

signed main() {
  using pii = pair<int, int>;
  using piii = pair<pii, int>;
  int v, e, s;
  cin >> v >> e >> s;
  vector<int> d(v, LLONG_MAX);
  vector<piii> edge;
  rep(i, e) {
    int from, to, cost;
    cin >> from >> to >> cost;
    edge.push_back(piii(pii(from, to), cost));
  }
  d[s] = 0;
  rep(i, v) {
    bool update = false;
    rep(j, edge.size()) {
      int to = edge[j].first.second;
      int from = edge[j].first.first;
      int cost = edge[j].second;
      if (d[from] == LLONG_MAX)
        continue;
      if (d[to] > d[from] + cost) {
        d[to] = d[from] + cost;
        update = true;
      }
    }
    if (i == v - 1 && update)
      return puts("NEGATIVE CYCLE") * 0;
  }
  rep(i, v) if (d[i] != LLONG_MAX) cout << d[i] << endl;
  else cout << "INF" << endl;
}
Problem p02362 (C++)

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

#define INF 999999999
#define ll long long

using namespace std;

const int MAX_N = 1000;
const int MAX_M = 2000;

struct edge {
  int from; //?????????
  int to;   //??°??????
  int cost; //??????
};

int d[MAX_N];
edge es[MAX_M];
bool flag; //???????????????????????????

void shortest_path(
    int n, int m,
    int s) // s???????????????????????????????????????????????¢????±???????
{
  int cnt = 0;
  fill(d, d + n, INF);
  d[s] = 0;
  while (true && cnt < n) {
    bool update = false;
    for (int i = 0; i < m; i++) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
  if (cnt == n) {
    flag = true;
  }
}

int main() {
  int i, n, m, s;
  cin >> n; //????????°
  cin >> m; //?????°
  cin >> s; //???????????????
  for (int i = 0; i < m; i++) {
    scanf("%d", &es[i].from);
    scanf("%d", &es[i].to);
    scanf("%d", &es[i].cost);
  }
  flag = false;
  shortest_path(n, m, s);
  if (!flag) {
    for (int i = 0; i < n; i++) {
      if (d[i] == INF) {
        cout << "INF\n";
      } else {
        cout << d[i] << "\n";
      }
    }
  } else {
    cout << "NEGATIVE CYCLE\n";
  }
}

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

#define INF 999999999
#define ll long long

using namespace std;

const int MAX_N = 1000;
const int MAX_M = 2000;

struct edge {
  int from; //?????????
  int to;   //??°??????
  int cost; //??????
};

int d[MAX_N];
edge es[MAX_M];
bool flag; //???????????????????????????

void shortest_path(
    int n, int m,
    int s) // s???????????????????????????????????????????????¢????±???????
{
  int cnt = 0;
  fill(d, d + n, INF);
  d[s] = 0;
  while (true && cnt < n) {
    bool update = false;
    for (int i = 0; i < m; i++) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (!update)
      break;
    cnt++;
  }
  if (cnt == n) {
    flag = true;
  }
}

int main() {
  int i, n, m, s;
  cin >> n; //????????°
  cin >> m; //?????°
  cin >> s; //???????????????
  for (int i = 0; i < m; i++) {
    scanf("%d", &es[i].from);
    scanf("%d", &es[i].to);
    scanf("%d", &es[i].cost);
  }
  flag = false;
  shortest_path(n, m, s);
  if (!flag) {
    for (int i = 0; i < n; i++) {
      if (d[i] == INF) {
        cout << "INF\n";
      } else {
        cout << d[i] << "\n";
      }
    }
  } else {
    cout << "NEGATIVE CYCLE\n";
  }
}
Problem p02362 (C++)

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

using namespace std;

const int MAXN = 1005, INF = 1e9;
int d[MAXN], x[MAXN], y[MAXN], w[MAXN];
int n, m, r;

bool bellman_ford(int r) {
  fill(d, d + n, INF);
  d[r] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      d[y[j]] = min(d[y[j]], d[x[j]] + w[j]);
  for (int j = 0; j < m; j++)
    if (d[y[j]] > d[x[j]] + w[j] && d[y[j]] < INF / 10)
      return true;
  return false;
}

int main() {
  cin >> n >> m >> r;
  for (int i = 0; i < m; i++)
    cin >> x[i] >> y[i] >> w[i];
  if (bellman_ford(r))
    cout << "NEGATIVE CYCLE" << endl;
  else
    for (int i = 0; i < n; i++)
      if (d[i] > INF / 10)
        cout << "INF" << endl;
      else
        cout << d[i] << endl;
  return 0;
}

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

using namespace std;

const int MAXN = 2005, INF = 1e9;
int d[MAXN], x[MAXN], y[MAXN], w[MAXN];
int n, m, r;

bool bellman_ford(int r) {
  fill(d, d + n, INF);
  d[r] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      d[y[j]] = min(d[y[j]], d[x[j]] + w[j]);
  for (int j = 0; j < m; j++)
    if (d[y[j]] > d[x[j]] + w[j] && d[y[j]] < INF / 10)
      return true;
  return false;
}

int main() {
  cin >> n >> m >> r;
  for (int i = 0; i < m; i++)
    cin >> x[i] >> y[i] >> w[i];
  if (bellman_ford(r))
    cout << "NEGATIVE CYCLE" << endl;
  else
    for (int i = 0; i < n; i++)
      if (d[i] > INF / 10)
        cout << "INF" << endl;
      else
        cout << d[i] << endl;
  return 0;
}
Problem p02362 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1005;

struct edge {
  int to, cost;
};

int n, m, s, cnt[maxn];
int d[maxn];
int qh, qt, q[maxn * maxn];
bool ok = 1, inq[maxn];
vector<edge> G[maxn];

int main() {
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    G[u].pb(edge{v, w});
  }
  memset(d, 0x3f, sizeof(d));
  d[s] = 0;
  q[qt++] = s;
  inq[s] = 1;
  while (qh < qt) {
    int u = q[qh++];
    inq[u] = 0;
    for (edge e : G[u]) {
      if (d[e.to] > d[u] + e.cost) {
        d[e.to] = d[u] + e.cost;
        if (!inq[e.to]) {
          q[qt++] = e.to;
          inq[e.to] = 1;
          ++cnt[e.to];
          if (cnt[u] > m + 5) {
            ok = 0;
            break;
          }
        }
      }
    }
  }
  if (!ok) {
    cout << "NEGATIVE CYCLE" << '\n';
    return 0;
  } else
    for (int i = 0; i < n; i++)
      if (d[i] == INF)
        cout << "INF" << '\n';
      else
        cout << d[i] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1005;

struct edge {
  int to, cost;
};

int n, m, s, cnt[maxn];
int d[maxn];
int qh, qt, q[maxn * maxn];
bool ok = 1, inq[maxn];
vector<edge> G[maxn];

int main() {
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    G[u].pb(edge{v, w});
  }
  memset(d, 0x3f, sizeof(d));
  d[s] = 0;
  q[qt++] = s;
  inq[s] = 1;
  while (qh < qt) {
    int u = q[qh++];
    inq[u] = 0;
    for (edge e : G[u]) {
      if (d[e.to] > d[u] + e.cost) {
        d[e.to] = d[u] + e.cost;
        if (!inq[e.to]) {
          q[qt++] = e.to;
          inq[e.to] = 1;
          ++cnt[e.to];
          if (cnt[u] > m + 5) {
            ok = 0;
            break;
          }
        }
      }
    }
    if (cnt[u] > m + 5) {
      ok = 0;
      break;
    }
  }
  if (!ok) {
    cout << "NEGATIVE CYCLE" << '\n';
    return 0;
  } else
    for (int i = 0; i < n; i++)
      if (d[i] == INF)
        cout << "INF" << '\n';
      else
        cout << d[i] << '\n';
}
Problem p02362 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
#define MAX_V 1000
#define MAX_E 2000
#define INF 1e9
struct edge {
  int from, to, cost;
};
int V, E;
int d[MAX_V];
edge es[MAX_E];
bool find_negative_loop(int s) {
  for (int i = 0; i < MAX_V; i++) {
    d[i] = INF;
  }
  d[s] = 0;
  int count = 0;
  // bool update = false;
  while (true) {
    bool update = true;
    count++;
    for (int i = 0; i < E; i++) {
      if ((d[es[i].from] != INF) &&
          (d[es[i].to] > d[es[i].from] + es[i].cost)) {
        if (count == V) {
          return true;
        }
        d[es[i].to] = d[es[i].from] + es[i].cost;
        update = true;
      }
    }
    if (!update) {
      break;
    }
  }
  return false;
}

int main() {
  int r;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    cin >> es[i].from >> es[i].to >> es[i].cost;
  }
  if (find_negative_loop(r)) {
    cout << "NEGATIVE CYCLE" << endl;
  } else {
    for (int i = 0; i < V; i++) {
      if (d[i] == INF) {
        cout << "INF" << endl;
      } else {
        cout << d[i] << endl;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define MAX_V 1000
#define MAX_E 2000
#define INF 1e9
struct edge {
  int from, to, cost;
};
int V, E;
int d[MAX_V];
edge es[MAX_E];
bool find_negative_loop(int s) {
  for (int i = 0; i < MAX_V; i++) {
    d[i] = INF;
  }
  d[s] = 0;
  int count = 0;
  // bool update = false;
  while (true) {
    bool update = false;
    count++;
    for (int i = 0; i < E; i++) {
      if ((d[es[i].from] != INF) &&
          (d[es[i].to] > d[es[i].from] + es[i].cost)) {
        if (count == V) {
          return true;
        }
        d[es[i].to] = d[es[i].from] + es[i].cost;
        update = true;
      }
    }
    if (!update) {
      break;
    }
  }
  return false;
}

int main() {
  int r;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    cin >> es[i].from >> es[i].to >> es[i].cost;
  }
  if (find_negative_loop(r)) {
    cout << "NEGATIVE CYCLE" << endl;
  } else {
    for (int i = 0; i < V; i++) {
      if (d[i] == INF) {
        cout << "INF" << endl;
      } else {
        cout << d[i] << endl;
      }
    }
  }
  return 0;
}
Problem p02363 (C++)

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

using namespace std;

using s64 = int64_t;

constexpr s64 INF = 1'010'000'000'000'000'000LL;

template <typename T, typename U> bool chmin(T &xmin, const U &x) {
  if (x < xmin) {
    xmin = x;
    return true;
  } else {
    return false;
  }
}

#define FOR(i, start, end) for (s64 i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)

s64 N;
s64 M;
vector<vector<s64>> G;

void print_g(ostream &out) {
  REP(i, N) {
    REP(j, N) {
      s64 c = G[i][j];
      if (c == INF)
        out << "INF";
      else
        out << c;
      out << (j == N - 1 ? '\n' : ' ');
    }
  }
}

bool floyd() {
  REP(k, N) {
    REP(i, N) {
      if (G[i][k] == INF)
        continue;
      REP(j, N) {
        if (G[k][j] == INF)
          continue;
        chmin(G[i][j], G[i][k] + G[k][j]);
        if (i == j && G[i][j] < 0)
          return false;
      }
    }
  }

  return true;
}

void solve() {
  floyd();
  // print_g(cerr);
  REP(i, N) { cerr << G[i][i] << "\n"; }

  REP(i, N) {
    if (G[i][i] < 0) {
      cout << "NEGATIVE CYCLE\n";
      return;
    }
  }

  print_g(cout);
}

int main() {
  cin >> N;
  G.assign(N, vector<s64>(N, INF));
  REP(i, N) { G[i][i] = 0; }

  cin >> M;
  REP(_, M) {
    s64 a, b, c;
    cin >> a >> b >> c;
    G[a][b] = c;
  }

  solve();

  return 0;
}

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

using namespace std;

using s64 = int64_t;

constexpr s64 INF = 1'010'000'000'000'000'000LL;

template <typename T, typename U> bool chmin(T &xmin, const U &x) {
  if (x < xmin) {
    xmin = x;
    return true;
  } else {
    return false;
  }
}

#define FOR(i, start, end) for (s64 i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)

s64 N;
s64 M;
vector<vector<s64>> G;

void print_g(ostream &out) {
  REP(i, N) {
    REP(j, N) {
      s64 c = G[i][j];
      if (c == INF)
        out << "INF";
      else
        out << c;
      out << (j == N - 1 ? '\n' : ' ');
    }
  }
}

bool floyd() {
  REP(k, N) {
    REP(i, N) {
      if (G[i][k] == INF)
        continue;
      REP(j, N) {
        if (G[k][j] == INF)
          continue;
        chmin(G[i][j], G[i][k] + G[k][j]);
        if (i == j && G[i][j] < 0)
          return false;
      }
    }
  }

  return true;
}

void solve() {
  if (!floyd()) {
    cout << "NEGATIVE CYCLE\n";
    return;
  }

  print_g(cout);
}

int main() {
  cin >> N;
  G.assign(N, vector<s64>(N, INF));
  REP(i, N) { G[i][i] = 0; }

  cin >> M;
  REP(_, M) {
    s64 a, b, c;
    cin >> a >> b >> c;
    G[a][b] = c;
  }

  solve();

  return 0;
}
Problem p02363 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef pair<long long, long long> P;
#define MAX_N 1000
#define INF 1000000000000000LL

vector<P> X[MAX_N];
priority_queue<P, vector<P>, greater<P>> Q;
long long dist[MAX_N];
long long N, M, A, B, C;

int solve(int P) {
  while (!Q.empty()) {
    Q.pop();
  }
  for (int i = 0; i < MAX_N; i++) {
    dist[i] = INF;
  }
  dist[P] = 0;
  Q.push(make_pair(0, P));
  int cnt = 0;
  while (!Q.empty()) {
    cnt++;
    if (cnt >= 1000000) {
      return 1;
    }
    pair<long long, long long> pa = Q.top();
    Q.pop();
    long long a1 = pa.first, a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      long long to = X[a2][i].first;
      if (dist[to] > a1 + X[a2][i].second) {
        dist[to] = a1 + X[a2][i].second;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (i >= 1) {
      cout << ' ';
    }
    if (dist[i] == INF) {
      cout << "INF";
    } else {
      cout << dist[i];
    }
  }
  cout << endl;
  return 0;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
  }
  for (int i = 0; i < N; i++) {
    int B = solve(i);
    if (B == 1) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }
  return 0;
}

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

typedef pair<long long, long long> P;
#define MAX_N 1000
#define INF 1000000000000000LL

vector<P> X[MAX_N];
priority_queue<P, vector<P>, greater<P>> Q;
long long dist[MAX_N];
long long N, M, A, B, C;

int solve(int P) {
  while (!Q.empty()) {
    Q.pop();
  }
  for (int i = 0; i < MAX_N; i++) {
    dist[i] = INF;
  }
  dist[P] = 0;
  Q.push(make_pair(0, P));
  int cnt = 0;
  while (!Q.empty()) {
    cnt++;
    if (cnt >= 500000) {
      return 1;
    }
    pair<long long, long long> pa = Q.top();
    Q.pop();
    long long a1 = pa.first, a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      long long to = X[a2][i].first;
      if (dist[to] > a1 + X[a2][i].second) {
        dist[to] = a1 + X[a2][i].second;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (i >= 1) {
      cout << ' ';
    }
    if (dist[i] == INF) {
      cout << "INF";
    } else {
      cout << dist[i];
    }
  }
  cout << endl;
  return 0;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
  }
  for (int i = 0; i < N; i++) {
    int B = solve(i);
    if (B == 1) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02364 (C++)

Buggy submission (status: Runtime Error):
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
struct side {
  int n;
  int m;
  int l;
};
side sides[100000];
void mergesort(int l, int r) {
  if (l == r)
    return;
  side temp;
  if (l == r - 1) {
    if (sides[l].l > sides[r].l) {
      temp = sides[l];
      sides[l] = sides[r];
      sides[r] = temp;
    }
    return;
  }
  int mid = (l + r) / 2, now = 0;
  side b[r - l + 1];
  mergesort(l, mid - 1);
  mergesort(mid, r);
  for (int i = 0; i <= r - l; i++) {
    if (sides[i + l - now].l < sides[mid + now].l) {
      b[i] = sides[i + l - now];
      if (i + l - now == mid - 1) {
        for (i++; i <= r - l; i++) {
          b[i] = sides[mid + now];
          now++;
        }
      }
    } else {
      b[i] = sides[mid + now];
      now++;
      if (mid + now > r) {
        for (i++; i <= r - l; i++)
          b[i] = sides[i + l - now];
      }
    }
  }
  for (int i = 0; i <= r - l; i++)
    sides[i + l] = b[i];
}
int main() {
  int n, m, a, ans = 0, c = 1;
  bool tree[10000];
  scanf("%d%d", &n, &m);
  tree[0] = true;
  for (int i = 1; i < n; i++) {
    tree[i] = false;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &sides[i].n, &sides[i].m, &sides[i].l);
  }
  mergesort(0, m - 1);
  while (c < n) {
    a = 0;
    while (tree[sides[a].n] == tree[sides[a].m])
      a++;
    ans += sides[a].l;
    tree[sides[a].n] = tree[sides[a].m] = true;
    c++;
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
struct side {
  int n;
  int m;
  int l;
};
side sides[100000];
void mergesort(int l, int r) {
  if (l == r)
    return;
  side temp;
  if (l == r - 1) {
    if (sides[l].l > sides[r].l) {
      temp = sides[l];
      sides[l] = sides[r];
      sides[r] = temp;
    }
    return;
  }
  int mid = (l + r) / 2, now = 0;
  side b[r - l + 1];
  mergesort(l, mid - 1);
  mergesort(mid, r);
  for (int i = 0; i <= r - l; i++) {
    if (sides[i + l - now].l < sides[mid + now].l) {
      b[i] = sides[i + l - now];
      if (i + l - now == mid - 1) {
        for (i++; i <= r - l; i++) {
          b[i] = sides[mid + now];
          now++;
        }
      }
    } else {
      b[i] = sides[mid + now];
      now++;
      if (mid + now > r) {
        for (i++; i <= r - l; i++)
          b[i] = sides[i + l - now];
      }
    }
  }
  for (int i = 0; i <= r - l; i++)
    sides[i + l] = b[i];
}
int main() {
  int n, m, a, ans = 0, c = 1;
  bool tree[10000];
  scanf("%d%d", &n, &m);
  if (m == 0) {
    printf("0\n");
    return 0;
  }
  tree[0] = true;
  for (int i = 1; i < n; i++) {
    tree[i] = false;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &sides[i].n, &sides[i].m, &sides[i].l);
  }
  mergesort(0, m - 1);
  while (c < n) {
    a = 0;
    while (tree[sides[a].n] == tree[sides[a].m])
      a++;
    ans += sides[a].l;
    tree[sides[a].n] = tree[sides[a].m] = true;
    c++;
  }
  printf("%d\n", ans);
}
Problem p02364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using edge = tuple<int, int, int>;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  UnionFind uf(V);
  vector<edge> edges;
  while (E--) {
    int s, t, w;
    cin >> s >> t >> w;
    edges.emplace_back(w, s, t);
  }
  sort(begin(edges), end(edges));
  int total_weight = 0;
  int pos = 0;
  int u, v, w;
  tie(w, u, v) = edges[pos];
  for (int i = 1; i < V; ++i) {
    while (uf.root(u) == uf.root(v)) {
      ++pos;
      w = get<0>(edges[pos]);
      u = get<1>(edges[pos]);
      v = get<2>(edges[pos]);
    }
    uf.unite(u, v);
    total_weight += w;
  }
  cout << total_weight << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using edge = tuple<int, int, int>;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  if (V == 1) {
    cout << 0 << endl;
    return 0;
  }
  UnionFind uf(V);
  vector<edge> edges;
  while (E--) {
    int s, t, w;
    cin >> s >> t >> w;
    edges.emplace_back(w, s, t);
  }
  sort(begin(edges), end(edges));
  int total_weight = 0;
  int pos = 0;
  int u, v, w;
  tie(w, u, v) = edges[pos];
  for (int i = 1; i < V; ++i) {
    while (uf.root(u) == uf.root(v)) {
      ++pos;
      w = get<0>(edges[pos]);
      u = get<1>(edges[pos]);
      v = get<2>(edges[pos]);
    }
    uf.unite(u, v);
    total_weight += w;
  }
  cout << total_weight << endl;
  return 0;
}
Problem p02364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

struct UF {
  int N, par[100], sz[100];

  void init(int _N) {
    N = _N;
    for (int i = 0; i < N; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }

  int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] < sz[y])
      swap(x, y);
    par[y] = x;
    sz[x] += sz[y];
  }

  bool isSame(int x, int y) { return find(x) == find(y); }
};

typedef pair<int, int> P;
typedef pair<int, P> PP;
int main() {
  int N, M;
  cin >> N >> M;

  vector<PP> es;

  for (int i = 0; i < M; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    es.push_back(PP(w, P(s, t)));
  }

  sort(es.begin(), es.end());

  UF uf;
  uf.init(N);
  int ans = 0;
  for (int i = 0; i < M; i++) {
    PP p = es[i];
    if (uf.isSame(p.second.first, p.second.second))
      continue;
    ans += p.first;
    uf.unite(p.second.first, p.second.second);
  }

  cout << ans << endl;
  return 0;
}

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

struct UF {
  int N, par[10000], sz[10000];

  void init(int _N) {
    N = _N;
    for (int i = 0; i < N; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }

  int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] < sz[y])
      swap(x, y);
    par[y] = x;
    sz[x] += sz[y];
  }

  bool isSame(int x, int y) { return find(x) == find(y); }
};

typedef pair<int, int> P;
typedef pair<int, P> PP;
int main() {
  int N, M;
  cin >> N >> M;

  vector<PP> es;

  for (int i = 0; i < M; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    es.push_back(PP(w, P(s, t)));
  }

  sort(es.begin(), es.end());

  UF uf;
  uf.init(N);
  int ans = 0;
  for (int i = 0; i < M; i++) {
    PP p = es[i];
    if (uf.isSame(p.second.first, p.second.second))
      continue;
    ans += p.first;
    uf.unite(p.second.first, p.second.second);
  }

  cout << ans << endl;
  return 0;
}
Problem p02364 (C++)

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

using namespace std;
#define MV 10000
#define INF 1e9

struct edge {
  int to, cost;
};
vector<edge> G[MV];
bool used[MV];
int V, E;
int d[MV];
int prim() {

  for (int i = 0; i < V; i++) {
    d[i] = INF;
    used[i] = false;
  }
  d[0] = 0;
  used[0] = true;

  for (int i = 0; i < G[0].size(); i++)
    d[G[0][i].to] = G[0][i].cost;

  int num = 0;

  for (int j = 0; j < V; j++) {
    int m = -1;
    for (int i = 0; i < V; i++) {
      if (used[i] == false && (m == -1 || d[m] > d[i]))
        m = i;
    }
    /*if (m == -1)
      break;*/
    used[m] = true;
    num += d[m];
    for (int i = 0; i < G[m].size(); i++)
      d[G[m][i].to] = min(d[G[m][i].to], G[m][i].cost);
  }
  return num;
}

int main() {
  int first;
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    int f, t;
    edge e;
    cin >> f >> e.to >> e.cost;
    G[f].push_back(e);
    /*
      G[f]??????f????????§????????????????´???????????????????
     */
    t = e.to;

    e.to = f;
    G[t].push_back(e);
  }

  int fl = prim();
  cout << fl << endl;
}

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

using namespace std;
#define MV 10000
#define INF 1e9

struct edge {
  int to, cost;
};
vector<edge> G[MV];
bool used[MV];
int V, E;
int d[MV];
int prim() {

  for (int i = 0; i < V; i++) {
    d[i] = INF;
    used[i] = false;
  }
  d[0] = 0;
  used[0] = true;

  for (int i = 0; i < G[0].size(); i++)
    d[G[0][i].to] = G[0][i].cost;

  int num = 0;

  for (int j = 0; j < V; j++) {
    int m = -1;
    for (int i = 0; i < V; i++) {
      if (used[i] == false && (m == -1 || d[m] > d[i]))
        m = i;
    }
    if (m == -1)
      break;
    used[m] = true;
    num += d[m];
    for (int i = 0; i < G[m].size(); i++)
      d[G[m][i].to] = min(d[G[m][i].to], G[m][i].cost);
  }
  return num;
}

int main() {
  int first;
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    int f, t;
    edge e;
    cin >> f >> e.to >> e.cost;
    G[f].push_back(e);
    /*
      G[f]??????f????????§????????????????´???????????????????
     */
    t = e.to;

    e.to = f;
    G[t].push_back(e);
  }

  int fl = prim();
  cout << fl << endl;
}
Problem p02364 (C++)

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

struct Edge {
  int P1;
  int P2;
  int Wei;
};

int main(void) {

  vector<Edge> EdgeCol;
  int V, E;
  cin >> V >> E;
  if (V == 1)
    cout << 1;
  vector<int> Select(V, 0);
  for (int i = 0; i < E; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    EdgeCol.push_back(Edge{s, t, w});
  }
  sort(EdgeCol.begin(), EdgeCol.end(),
       [](Edge E1, Edge E2) { return E1.Wei > E2.Wei; });
  int WeiSum = 0;
  int a = 1;
  while (1) {
    int aP1 = Select[EdgeCol.back().P1];
    int aP2 = Select[EdgeCol.back().P2];
    if (aP1 && aP2) {
      if (aP1 != aP2) {
        replace(Select.begin(), Select.end(), aP2, aP1);
        WeiSum += EdgeCol.back().Wei;
      }
    } else if (!aP1 && !aP2) {
      Select[EdgeCol.back().P1] = a;
      Select[EdgeCol.back().P2] = a;
      a++;
      WeiSum += EdgeCol.back().Wei;
    } else {
      Select[EdgeCol.back().P1] = max(aP1, aP2);
      Select[EdgeCol.back().P2] = max(aP1, aP2);
      WeiSum += EdgeCol.back().Wei;
    }
    EdgeCol.pop_back();
    if (all_of(Select.cbegin(), Select.cend(),
               [=](int s) { return s == *Select.cbegin(); })) {
      cout << WeiSum << endl;
      return 0;
    }
  }
  return 0;
}

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

struct Edge {
  int P1;
  int P2;
  int Wei;
};

int main(void) {

  vector<Edge> EdgeCol;
  int V, E;
  cin >> V >> E;
  if (V == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> Select(V, 0);
  for (int i = 0; i < E; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    EdgeCol.push_back(Edge{s, t, w});
  }
  sort(EdgeCol.begin(), EdgeCol.end(),
       [](Edge E1, Edge E2) { return E1.Wei > E2.Wei; });
  int WeiSum = 0;
  int a = 1;
  while (1) {
    int aP1 = Select[EdgeCol.back().P1];
    int aP2 = Select[EdgeCol.back().P2];
    if (aP1 && aP2) {
      if (aP1 != aP2) {
        replace(Select.begin(), Select.end(), aP2, aP1);
        WeiSum += EdgeCol.back().Wei;
      }
    } else if (!aP1 && !aP2) {
      Select[EdgeCol.back().P1] = a;
      Select[EdgeCol.back().P2] = a;
      a++;
      WeiSum += EdgeCol.back().Wei;
    } else {
      Select[EdgeCol.back().P1] = max(aP1, aP2);
      Select[EdgeCol.back().P2] = max(aP1, aP2);
      WeiSum += EdgeCol.back().Wei;
    }
    EdgeCol.pop_back();
    if (all_of(Select.cbegin(), Select.cend(),
               [=](int s) { return s == *Select.cbegin(); })) {
      cout << WeiSum << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02364 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <string>
#include <vector>
using namespace std;
#define inf 1000000007
#define int long long
#define rep(i, j, n) for (int i = j; i < n; i++)
typedef pair<int, int> P;
double pi = 3.141592653589793;
// ここから下
struct edge {
  int u, v, cost;
};
bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
edge es[114514];
int v, e;

int par[100005];
int lank[100005];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    lank[i] = 0;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (lank[x] < lank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (lank[x] == lank[y])
      lank[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int kruskal() {
  sort(es, es + e, comp);
  init(v);
  int res = 0;
  rep(i, 0, e) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

signed main() {
  cin >> v >> e;
  rep(i, 0, e) {
    int a, b, c;
    cin >> a >> b >> c;
    es[i] = edge{a, b, c};
    es[i + e] = edge{a, b, c};
  }
  e *= 2;
  cout << kruskal() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <string>
#include <vector>
using namespace std;
#define inf 1000000007
#define int long long
#define rep(i, j, n) for (int i = j; i < n; i++)
typedef pair<int, int> P;
double pi = 3.141592653589793;
// ここから下
struct edge {
  int u, v, cost;
};
bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }
edge es[214514];
int v, e;

int par[100005];
int lank[100005];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    lank[i] = 0;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (lank[x] < lank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (lank[x] == lank[y])
      lank[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int kruskal() {
  sort(es, es + e, comp);
  init(v);
  int res = 0;
  rep(i, 0, e) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

signed main() {
  cin >> v >> e;
  rep(i, 0, e) {
    int a, b, c;
    cin >> a >> b >> c;
    es[i] = edge{a, b, c};
    es[i + e] = edge{a, b, c};
  }
  e *= 2;
  cout << kruskal() << endl;
  return 0;
}
Problem p02364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

const int VMAX = 10010;
int V, E;
vector<vector<pair<int, int>>> edge;
bool used[VMAX];
long long MST(int start) {
  rep(i, VMAX) used[i] = 0;
  // {cost,{from,to}}
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  for (auto e : edge[start]) {
    pq.push({e.second, {start, e.first}});
  }
  long long ret = 0;
  int cnt = 1;
  while (pq.size() || cnt < V) {
    auto e = pq.top();
    pq.pop();
    int cost = e.first, from = e.second.first, to = e.second.second;
    used[from] = 1;
    if (!used[to]) {
      ret += cost;
      used[to] = 1;
      cnt++;
      for (auto e : edge[to]) {
        pq.push({e.second, {to, e.first}});
      }
    }
  }
  return (cnt == V ? ret : -1);
}
int main() {
  cin >> V >> E;
  edge.resize(V);
  int start;
  rep(i, E) {
    int from, to, cost;
    cin >> from >> to >> cost;
    start = from;
    edge[from].push_back({to, cost});
    edge[to].push_back({from, cost});
  }
  cout << MST(start) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

const int VMAX = 10010;
int V, E;
vector<vector<pair<int, int>>> edge;
bool used[VMAX];
long long MST(int start) {
  if (V == 1)
    return 0;
  rep(i, VMAX) used[i] = 0;
  // {cost,{from,to}}
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  for (auto e : edge[start]) {
    pq.push({e.second, {start, e.first}});
  }
  long long ret = 0;
  int cnt = 1;
  while (pq.size() || cnt < V) {
    auto e = pq.top();
    pq.pop();
    int cost = e.first, from = e.second.first, to = e.second.second;
    used[from] = 1;
    if (!used[to]) {
      ret += cost;
      used[to] = 1;
      cnt++;
      for (auto e : edge[to]) {
        pq.push({e.second, {to, e.first}});
      }
    }
  }
  return (cnt == V ? ret : -1);
}
int main() {
  cin >> V >> E;
  edge.resize(V);
  int start;
  rep(i, E) {
    int from, to, cost;
    cin >> from >> to >> cost;
    start = from;
    edge[from].push_back({to, cost});
    edge[to].push_back({from, cost});
  }
  cout << MST(start) << endl;
  return 0;
}
Problem p02364 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;

const int V_MAX = 100;
const int INF = 100000000;

struct Edge {
  bool operator<(const Edge &e) const { return w > e.w; }
  int to;
  int w;
};

int main() {
  int v;
  int e;
  priority_queue<Edge> edge[V_MAX];
  vector<int> tree;
  bool check[V_MAX] = {false};
  int ans = 0;

  scanf("%d%d", &v, &e);

  for (int i = 0; i < e; i++) {
    int s, t, w;
    Edge e1, e2;

    scanf("%d%d%d", &s, &t, &w);

    e1.to = t;
    e1.w = w;
    e2.to = s;
    e2.w = w;

    edge[s].push(e1);
    edge[t].push(e2);
  }

  tree.push_back(0);
  check[0] = true;

  for (int i = 1; i < v; i++) {

    int minW = INF;
    int minV = 0;
    int k = 0;

    for (int j = 0; j < tree.size(); j++) {
      int t = tree[j];
      while (!edge[t].empty() && check[edge[t].top().to]) {
        edge[t].pop();
      }

      if (edge[t].empty())
        continue;

      if (minW > edge[t].top().w) {
        minV = edge[t].top().to;
        minW = edge[t].top().w;
        k = t;
      }
    }

    edge[k].pop();

    check[minV] = true;
    tree.push_back(minV);
    ans += minW;
  }

  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;

const int V_MAX = 10000;
const int INF = 100000000;

struct Edge {
  bool operator<(const Edge &e) const { return w > e.w; }
  int to;
  int w;
};

int main() {
  int v;
  int e;
  priority_queue<Edge> edge[V_MAX];
  vector<int> tree;
  bool check[V_MAX] = {false};
  int ans = 0;

  scanf("%d%d", &v, &e);

  for (int i = 0; i < e; i++) {
    int s, t, w;
    Edge e1, e2;

    scanf("%d%d%d", &s, &t, &w);

    e1.to = t;
    e1.w = w;
    e2.to = s;
    e2.w = w;

    edge[s].push(e1);
    edge[t].push(e2);
  }

  tree.push_back(0);
  check[0] = true;

  for (int i = 1; i < v; i++) {

    int minW = INF;
    int minV = 0;
    int k = 0;

    for (int j = 0; j < tree.size(); j++) {
      int t = tree[j];
      while (!edge[t].empty() && check[edge[t].top().to]) {
        edge[t].pop();
      }

      if (edge[t].empty())
        continue;

      if (minW > edge[t].top().w) {
        minV = edge[t].top().to;
        minW = edge[t].top().w;
        k = t;
      }
    }

    edge[k].pop();

    check[minV] = true;
    tree.push_back(minV);
    ans += minW;
  }

  printf("%d\n", ans);

  return 0;
}
Problem p02366 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

int const INF = 1 << 29;

typedef vector<int> edges;
typedef vector<edges> graph;

namespace articulation {

int const MaxNum = 1000;

int prenum[MaxNum];
int lowest[MaxNum];
int parent[MaxNum];
int timer;
int V;

bool visited[MaxNum];
graph g;

void dfs(int curr) {
  prenum[curr] = timer;
  lowest[curr] = timer;
  timer++;

  visited[curr] = 1;

  rep(i, g[curr].size()) {
    int next = g[curr][i];
    if (!visited[next]) {
      parent[next] = curr;
      dfs(next);
      lowest[curr] = min(lowest[curr], lowest[next]);
    } else if (next != curr) {
      lowest[curr] = min(lowest[curr], prenum[next]);
    }
  }
}

vector<int> doit(int V_) {
  V = V_;
  rep(i, V) { visited[i] = 0; }
  timer = 1;
  dfs(0);

  vector<int> ret;
  int cnt = 0;
  rep(i, V) {
    if (i && parent[i] == 0) {
      cnt++;
    }
    if (parent[i] > 0 && lowest[i] >= prenum[parent[i]]) {
      ret.push_back(parent[i]);
    }
  }
  if (cnt >= 2) {
    ret.push_back(0);
  }
  sort(ret.begin(), ret.end());
  ret.erase(unique(ret.begin(), ret.end()), ret.end());
  return ret;
}

} // namespace articulation

int main() {

  using namespace articulation;

  int N, M;
  cin >> N >> M;
  g.resize(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vector<int> arts = doit(N);

  rep(i, arts.size()) { cout << arts[i] << endl; }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

int const INF = 1 << 29;

typedef vector<int> edges;
typedef vector<edges> graph;

namespace articulation {

int const MaxNum = 100000;

int prenum[MaxNum];
int lowest[MaxNum];
int parent[MaxNum];
int timer;
int V;

bool visited[MaxNum];
graph g;

void dfs(int curr) {
  prenum[curr] = timer;
  lowest[curr] = timer;
  timer++;

  visited[curr] = 1;

  rep(i, g[curr].size()) {
    int next = g[curr][i];
    if (!visited[next]) {
      parent[next] = curr;
      dfs(next);
      lowest[curr] = min(lowest[curr], lowest[next]);
    } else if (next != curr) {
      lowest[curr] = min(lowest[curr], prenum[next]);
    }
  }
}

vector<int> doit(int V_) {
  V = V_;
  rep(i, V) { visited[i] = 0; }
  timer = 1;
  dfs(0);

  vector<int> ret;
  int cnt = 0;
  rep(i, V) {
    if (i && parent[i] == 0) {
      cnt++;
    }
    if (parent[i] > 0 && lowest[i] >= prenum[parent[i]]) {
      ret.push_back(parent[i]);
    }
  }
  if (cnt >= 2) {
    ret.push_back(0);
  }
  sort(ret.begin(), ret.end());
  ret.erase(unique(ret.begin(), ret.end()), ret.end());
  return ret;
}

} // namespace articulation

int main() {

  using namespace articulation;

  int N, M;
  cin >> N >> M;
  g.resize(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vector<int> arts = doit(N);

  rep(i, arts.size()) { cout << arts[i] << endl; }

  return 0;
}