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

#include <bits/stdc++.h>
using namespace std;
struct EDGE {
  int from, to, cost;
};
bool operator<(const EDGE &a, const EDGE &b) { return a.cost < b.cost; }

vector<EDGE> g;
int par[10000];

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

bool _union(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return false;
  par[x] = y;
  return true;
}

int main() {
  int V, E;
  scanf("%d %d", &V, &E);

  for (int i = 0; i < V; i++)
    par[i] = i;

  for (int j = 0; j < E; j++) {
    int s, t, w;
    cin >> s >> t >> w;
    g.push_back(EDGE{s, t, w});
  }

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

  int res = 0;
  for (int i = 0; i < g.size(); i++) {
    EDGE &e = g[i];
    if (_union(e.from, e.to))
      res += e.cost;
  }

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

  return 0;
}

Error: 0

Fixed submission:

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

const int MAX_V = 10000;
const int MAX_E = 100000;
int finde[MAX_V];

struct Edge {
  int first;
  int second;
  int weight;
};
vector<Edge> e;

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

bool Judge(int m, int n) {
  while (finde[m] != m)
    m = finde[m];
  while (finde[n] != n)
    n = finde[n];
  if (m == n) {
    return false;
  }
  m = finde[n];
  return true;
}

int min_span_tree(int n, int m) {

  for (int i = 0; i < n; i++)
    finde[i] = i;
  int count = 0;
  int ret = 0;
  int f, s;
  for (int i = 0; count < m - 1; i++) {
    f = e[i].first;
    s = e[i].second;
    if (Judge(f, s)) {
      ret += e[i].weight;
      count++;
    }
  }
  return ret;
}
int main() {
  int n, m;
  cin >> m >> n;
  for (int i = 0; i < n; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    e.push_back(Edge{s, t, w});
  }
  sort(e.begin(), e.end());
  int ret = min_span_tree(n, m);
  cout << ret << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define PI 3.14159265359

#define INF 99999999;

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

typedef long long ll;

using namespace std;

/*
class LightSwitchingPuzzle
{
public:
        int minFlips(string s)
        {

        }
}
*/

int rank[100000];
int par[10000];

void init(int n) {
  REP(n) {
    rank[i] = 0;
    par[i] = i;
  }
}

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 (rank[x] < rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
  }
}

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

struct edge {
  int u, v, cost;
};

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

edge es[100000];

int main() {
  int V, E;

  cin >> V >> E;
  REP(E) { cin >> es[i].u >> es[i].v >> es[i].cost; }

  sort(es, es + E, comp);
  init(V);
  int res = 0;
  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }

  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define PI 3.14159265359

#define INF 99999999;

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

typedef long long ll;

using namespace std;

/*
class LightSwitchingPuzzle
{
public:
        int minFlips(string s)
        {

        }
}
*/

int main() {
  int V, E;

  cin >> V >> E;
  ll cost[V][V];
  ll mincost[V];
  bool used[V];

  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      cost[i][j] = INF;
    }
  }
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    cost[s][t] = w;
    cost[t][s] = w;
  }

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

  mincost[0] = 0;
  ll res = 0;

  while (1) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];

    for (int u = 0; u < V; u++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }

  cout << res << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;

int uni[100100];

int oya(int a) {
  if (uni[a] < 0) {
    return a;
  } else {
    uni[a] = oya(uni[a]);
    return oya(uni[a]);
  }
}

bool ren(int a, int b) {
  a = oya(a);
  b = oya(b);
  if (a == b) {
    return false;
  } else {

    return true;
  }
}

int main() {
  int co = 0;

  vector<pair<int, int>> be;
  int e, v;
  cin >> v >> e;
  for (int y = 0; y < v; y++) {
    uni[y] = -1;
  }
  for (int u = 0; u < e; u++) {
    int z, x, c;
    cin >> z >> x >> c;
    be.push_back(pair<int, int>(c, z * 100100 + x));
  }
  sort(be.begin(), be.end());

  for (int y = 0; y < e; y++) {
    int z, x, c;
    c = be[y].first;
    z = (be[y].second) / 100100;
    x = (be[y].second) % 100100;
    if (ren(z, x) == true) {
      co = co + c;
      uni[oya(z)] = x;
    }
  }
  cout << co << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;

int uni[1000];

int oya(int a) {
  if (uni[a] < 0) {
    return a;
  } else {
    uni[a] = oya(uni[a]);
    return oya(uni[a]);
  }
}

bool ren(int a, int b) {
  a = oya(a);
  b = oya(b);
  if (a == b) {
    return false;
  } else {

    return true;
  }
}

int main() {
  int co = 0;

  vector<pair<int, int>> be;
  int e, v;
  cin >> v >> e;
  for (int y = 0; y < v; y++) {
    uni[y] = -1;
  }
  for (int u = 0; u < e; u++) {
    int z, x, c;
    cin >> z >> x >> c;
    be.push_back(pair<int, int>(c, z * 1001 + x));
  }
  sort(be.begin(), be.end());

  for (int y = 0; y < e; y++) {
    int z, x, c;
    c = be[y].first;
    z = (be[y].second) / 1001;
    x = (be[y].second) % 1001;
    if (ren(z, x) == true) {
      co = co + c;
      uni[oya(z)] = x;
    }
  }
  cout << co << endl;

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 100000000000000000LL
#define MAX_N 500000
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    Q;
vector<pair<long long, long long>> X[MAX_N];
long long WHITE = 0, GRAY = 1, BLACK = 2;
long long COLOR[MAX_N], DIST[MAX_N], POINT[MAX_N], N, M, A, B, C;
long long classcal() {
  for (int i = 0; i < MAX_N; i++) {
    POINT[i] = INF;
    DIST[i] = INF;
    COLOR[i] = WHITE;
  }
  DIST[0] = 0;
  COLOR[0] = GRAY;
  Q.push(make_pair(0, 0));
  while (!Q.empty()) {
    pair<long long, long long> pa = Q.top();
    Q.pop();
    long long a1 = pa.first, a2 = pa.second;
    COLOR[a2] = BLACK;
    for (int i = 0; i < X[a2].size(); i++) {
      long long to = X[a2][i].first, dist = X[a2][i].second;
      if (DIST[to] > dist && COLOR[to] < BLACK) {
        for (int j = 0; j < X[to].size(); j++) {
          if (X[to][j].first == a2) {
            POINT[to] = j;
          }
        }
        DIST[to] = dist;
        Q.push(make_pair(DIST[to], to));
      }
    }
  }
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    if (POINT[i] < INF) {
      sum += X[i][POINT[i]].second;
    }
  }
  return sum;
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
    X[B].push_back(make_pair(A, C));
  }
  cout << classcal() << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 100000000000000000LL
#define MAX_N 500000
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    Q;
vector<pair<long long, long long>> X[MAX_N];
long long WHITE = 0, GRAY = 1, BLACK = 2;
long long COLOR[MAX_N], DIST[MAX_N], POINT[MAX_N], N, M, A, B, C;
long long classcal() {
  for (int i = 0; i < MAX_N; i++) {
    POINT[i] = INF;
    DIST[i] = INF;
    COLOR[i] = WHITE;
  }
  DIST[0] = 0;
  COLOR[0] = GRAY;
  Q.push(make_pair(0, 0));
  while (!Q.empty()) {
    pair<long long, long long> pa = Q.top();
    Q.pop();
    long long a1 = pa.first, a2 = pa.second;
    COLOR[a2] = BLACK;
    for (int i = 0; i < X[a2].size(); i++) {
      int to = X[a2][i].first, dist = X[a2][i].second;
      if (DIST[to] > a1 + dist && COLOR[to] < BLACK) {
        DIST[to] = a1 + dist;
        POINT[i] = i;
        Q.push(make_pair(DIST[to], to));
      }
    }
  }
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    sum += X[i][POINT[i]].second;
  }
  return sum;
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
    X[B].push_back(make_pair(A, C));
  }
  cout << classcal() << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;

class UnionFind {
private:
  int N;
  vector<int> par;
  vector<int> rank;

  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }

public:
  UnionFind(int n) {
    N = n;
    par.resize(n);
    rank.resize(n);
    init(n);
  }
  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y]) {
        rank[x]++;
      }
    }
  }
};

struct edge {
  int u, v, cost;
};
bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

#define MAX_E 100000
edge es[MAX_E];

int main() {
  ios::sync_with_stdio(false);

  int V, E;
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    int u, v, cost;
    cin >> u >> v >> cost;
    edge e = {u, v, cost};
    es[i] = e;
  }

  sort(es, es + E, comp);
  UnionFind uf(V);
  int res = 0;

  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (not uf.same(e.u, e.v)) {
      uf.unite(e.u, e.v);
      res += e.cost;
    }
  }

  cout << res << "\\n";

  return 0;
}

Error: MLE

Fixed submission:

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

#define debug(x) cout << #x << "==" << x << endl;

typedef long long ll;

#define MAX_V 1000
#define INF INT_MAX / 2

int main() {
  ios::sync_with_stdio(false);

  int V, E;
  cin >> V >> E;

  vector<vector<int>> cost(
      V, vector<int>(V, INF)); // cost[u][v] ??? e=(u,v) ??? cost
                               // (?????¨???????????´?????? INF)
  vector<int> mincost(V, INF); // ??????X????????????????????????????°?cost
  vector<bool> used(V, false); // ??????i???X?????????????????????????????????

  for (int i = 0; i < E; i++) {
    int v, w, c;
    cin >> v >> w >> c;
    cost[v][w] = c;
    cost[w][v] = c;
  }

  mincost[0] = 0;
  int res = 0;

  while (true) {
    // X
    // ????±??????????????????????X???????????????????????¨???????°????????????????????????????
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (not used[u] && (v == -1 || mincost[u] < mincost[v])) {
        v = u;
      }
    }
    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];
    for (int u = 0; u < V; u++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  cout << res << "\\n";
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

#include <bits/stdc++.h>

using namespace std;

#define MAX_V 10000
#define INF 1e9
typedef pair<int, int> pii;

struct edge {
  int to, cost;
};

int V;
bool used[MAX_V];
vector<edge> G[MAX_V];

int prim() {
  priority_queue<pii, vector<pii>, greater<pii>> Q;
  Q.push(pii(0, 0));
  memset(used, false, sizeof(used));
  int res = 0;

  while (!Q.empty()) {
    pii p = Q.top();
    Q.pop();
    int v = p.second;
    if (used[v]) {
      continue;
    }
    used[v] = true;
    res += p.first;
    for (int i = 0; i < (int)G[v].size(); i++) {
      edge e = G[v][i];
      Q.push(pii(e.cost, e.to));
    }
  }
  return res;
}

int main() {
  int E, s, t, w;
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> w;
    G[s].push_back((edge){t, w});
    G[t].push_back((edge){s, w});
  }
  cout << prim() << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX_V 10000
#define INF 1e9

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int V;

int prim() {
  for (int i = 0; i < V; i++) {
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  int res = 0;

  while (true) {
    int v = -1;

    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v])) {
        v = u;
      }
    }
    if (v == -1) {
      break;
    }
    used[v] = true;
    res += mincost[v];

    for (int u = 0; u < V; u++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  return res;
}

int main() {
  int E, s, t, w;
  cin >> V >> E;
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      cost[i][j] = INF;
    }
  }
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> w;
    cost[s][t] = cost[t][s] = w;
  }
  cout << prim() << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#define mp make_pair
#define pb push_back
using namespace std;
const int maxn = 10000 + 10;
bool mark[maxn];
vector<int> v[maxn], u[maxn];
int par[maxn];
vector<pair<int, pair<int, int>>> q;
int find(int x) {
  int k = par[x] == -1 ? x : find(par[x]);
  return k;
}
void unio(int x, int y) {
  int a = find(x), b = find(y);
  if (a < b)
    par[find(y)] = find(x);
  else
    par[find(x)] = find(y);
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    par[i] = -1;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    // a--; b--;
    v[a].pb(b);
    v[b].pb(a);
    q.pb(mp(c, mp(a, b)));
  }
  // cerr << 1 << endl;
  sort(q.begin(), q.end());
  // cerr << 2 << endl;
  int edges = 0;
  int i = 0;
  long long sum = 0;
  while (edges < n - 1) {
    int x = q[i].first;
    int a = q[i].second.first, b = q[i].second.second;
    // cerr << 3 << endl;
    if (find(a) != find(b)) {
      // cerr << 4 << endl;
      unio(a, b);
      // cerr << 5 << endl;
      sum += x;
      u[a].pb(b);
      u[b].pb(a);
      edges++;
    }
    i++;
  }
  cout << sum << endl;
  return 0;
  // cin >> n;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#define mp make_pair
#define pb push_back
using namespace std;
const int maxn = 10000 + 10;
bool mark[maxn];
vector<int> v[maxn], u[maxn];
int par[maxn];
vector<pair<int, pair<int, int>>> q;
int find(int x) {
  int k = par[x] == -1 ? x : find(par[x]);
  return k;
}
void unio(int x, int y) { par[x] = find(y); }
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    par[i] = -1;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    // a--; b--;
    v[a].pb(b);
    v[b].pb(a);
    q.pb(mp(c, mp(a, b)));
  }
  cerr << 1 << endl;
  sort(q.begin(), q.end());
  cerr << 2 << endl;
  int edges = 0;
  int i = 0;
  int sum = 0;
  while (edges < n - 1) {
    int x = q[i].first;
    int a = q[i].second.first, b = q[i].second.second;
    cerr << 3 << endl;
    if (find(a) != find(b)) {
      cerr << 4 << endl;
      unio(a, b);
      cerr << 5 << endl;
      sum += x;
      u[a].pb(b);
      u[b].pb(a);
      edges++;
    }
    i++;
  }
  cout << sum;
  // cin >> n;
}
Problem p02364 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

struct edge {
  ll u;
  ll v;
  ll cost;
  edge() {}
  edge(ll u, ll v, ll c) : u(u), v(v), cost(c) {}
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll V, E;
  cin >> V >> E;
  vector<vector<edge>> G(V);
  for (int i = 0; i < E; i++) {
    ll s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(edge(s, t, w));
    G[t].push_back(edge(t, s, w));
  }

  vector<int> checked(V, 0);
  vector<ll> min_d(V, INF);
  ll ans = 0;
  min_d[0] = 0;
  while (true) {
    ll next = -1;
    for (int i = 0; i < V; i++) {
      if (checked[i] == 1)
        continue;
      if (next == -1 || min_d[i] < min_d[next]) {
        next = i;
      }
    }
    if (next == -1)
      break;
    checked[next] = 1;
    ans += min_d[next];
    for (auto e : G[next]) {
      min_d[e.v] = min(min_d[e.v], e.cost);
    }
  }

  /*for (int i = 0; i < V;i++) {
          if (checked[i] == 0) {
                  cout << "cann't create minimum spanning tree" << endl;
                  return 0;
          }
  }*/
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

struct edge {
  ll u;
  ll v;
  ll cost;
  edge() {}
  edge(ll u, ll v, ll c) : u(u), v(v), cost(c) {}
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll V, E;
  cin >> V >> E;
  vector<vector<edge>> G(V);
  for (int i = 0; i < E; i++) {
    ll s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(edge(s, t, w));
    G[t].push_back(edge(t, s, w));
  }

  vector<int> checked(V, 0);
  checked[0] = 1;
  ll ans = 0;
  while (true) {
    ll min_cost = INF;
    ll next = -1;
    for (int i = 0; i < V; i++) {
      if (checked[i] == 0)
        continue;
      for (auto e : G[i]) {
        if (checked[e.v] == 1)
          continue;
        if (min_cost > e.cost) {
          min_cost = e.cost;
          next = e.v;
        }
      }
    }
    if (min_cost == INF)
      break;
    ans += min_cost;
    checked[next] = 1;
  }

  /*for (int i = 0; i < V;i++) {
          if (checked[i] == 0) {
                  cout << "cann't create minimum spanning tree" << endl;
                  return 0;
          }
  }*/
  cout << ans << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)

static const ll MOD = 1e9 + 7;
static const ll INF = (ll)1 << 62;

static const ll MAX_N = 100001;
static const ll MAX_M = 200001;

ll V;
ll d[MAX_N] = {};
struct edge {
  ll to, cost;
};
typedef pair<ll, ll> P;
vector<edge> G[MAX_N];
priority_queue<P, vector<P>, greater<P>> que;
bool mst[MAX_N];

ll cost = 0;

bool complete() {
  ll cnt = 0;
  rep(i, 0, V) {
    if (mst[i])
      cnt++;
  }
  return (cnt == V);
}

void prim(ll s) {
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (mst[v])
      continue;

    cost += p.first;
    mst[v] = true;

    rep(i, 0, G[v].size()) {
      edge e = G[v][i];
      if (!mst[e.to]) {
        que.push(P(e.cost, e.to));
      }
    }

    if (complete())
      break;
  }
}

int main() {
  // freopen("C://testcase//in7.txt", "r", stdin);

  ll E;
  cin >> V >> E;
  rep(i, 0, E) {
    ll s, t, d;
    cin >> s >> t >> d;
    G[s].push_back(edge{t, d});
    G[t].push_back(edge{s, d});
  }

  prim(0);

  cout << cost << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)

static const ll MOD = 1e9 + 7;
static const ll INF = (ll)1 << 62;

static const ll MAX_N = 100001;
static const ll MAX_M = 200001;

ll V;
ll d[MAX_N] = {};
struct edge {
  ll to, cost;
};
typedef pair<ll, ll> P;
vector<edge> G[MAX_N];
priority_queue<P, vector<P>, greater<P>> que;
bool mst[MAX_N];

ll cost = 0;
void prim(ll s) {
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (mst[v])
      continue;

    cost += p.first;
    mst[v] = true;

    bool complete = true;
    rep(i, 0, G[v].size()) {
      edge e = G[v][i];
      if (!mst[e.to]) { // && d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(e.cost, e.to));
        complete = false;
      }
    }
    if (complete)
      break;
  }
}

int main() {
  freopen("C://testcase//in.txt", "r", stdin);

  ll E;
  cin >> V >> E;
  rep(i, 0, E) {
    ll s, t, d;
    cin >> s >> t >> d;
    G[s].push_back(edge{t, d});
    G[t].push_back(edge{s, d});
  }

  prim(0);

  cout << cost << endl;

  return 0;
}