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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;

const int MAX_V = 10000;
const int INF = 1 << 28;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

int prim() {
  int res = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  for (int s = 0; s < V; ++s) {
    if (d[s] != INF)
      continue;
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      d[v] = -1;
      res += p.first;
      for (int i = 0; i < G[v].size(); i++) {
        edge e = G[v][i];
        if (d[e.to] > e.cost) {
          d[e.to] = e.cost;
          que.push(P(e.cost, e.to));
        }
      }
    }
  }
  return res;
}

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;

const int MAX_V = 10000;
const int INF = 1 << 28;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

int prim() {
  int res = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[0] = 0;
  que.push(P(0, 0));
  int cnt = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    res += p.first;
    cnt += 1;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > e.cost) {
        d[e.to] = e.cost;
        que.push(P(e.cost, e.to));
      }
    }
  }
  assert(cnt == V);
  return res;
}

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

#include <iostream>
#include <vector>
#define pb push_back
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define INF 1e11
using namespace std;
typedef pair<int, long long> pil;

int main() {
  int v, e, s, t, w;
  cin >> v >> e;
  long long mincost[v];
  vector<pil> vec[v];
  bool used[v];
  long long ans = 0;
  fill(mincost, mincost + v, INF);
  fill(used, used + v, false);
  rep(i, e) {
    cin >> s >> t >> w;
    vec[s].pb(pil(t, w));
    vec[t].pb(pil(s, w));
  }
  mincost[0] = 0;
  while (1) {
    int next = -1;
    for (int u = 0; u < v; u++) {
      if (!used[u] && (next == -1 || mincost[u] < mincost[next]))
        next = u;
    }
    if (next == -1)
      break;
    used[next] = true;
    ans += mincost[next];
    for (int i = 0; i < vec[next].size(); i++) {
      mincost[vec[next][i].first] =
          min(mincost[vec[next][i].first], vec[next][i].second);
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define INF 1e11
using namespace std;

int main() {
  int v, e, s, t, w;
  cin >> v >> e;
  long long cost[v][v], mincost[v];
  bool used[v];
  long long ans = 0;
  fill(mincost, mincost + v, INF);
  fill(used, used + v, false);
  rep(i, v) fill(cost[i], cost[i] + v, INF);
  rep(i, e) {
    cin >> s >> t >> w;
    cost[s][t] = cost[t][s] = w;
  }
  mincost[0] = 0;
  while (1) {
    int next = -1;
    for (int u = 0; u < v; u++) {
      if (!used[u] && (next == -1 || mincost[u] < mincost[next]))
        next = u;
    }
    if (next == -1)
      break;
    used[next] = true;
    ans += mincost[next];
    for (int u = 0; u < v; u++)
      mincost[u] = min(mincost[u], cost[next][u]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

#define MAX_V 10000
#define MAX_E 100000
#define INF INT_MAX / 2 - 1

typedef long long ll;

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

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

edge es[MAX_E];
int V, E;

int par[MAX_V];
int rank_[MAX_V];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rank_[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 (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); }

ll kruskal() {
  sort(es, es + E, comp);
  init(V);
  ll 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;
    }
  }
  return res;
}

int main() {
  cin >> V >> E;
  int s, t, w;
  for (int i = 0; i < E; i++) {
    cin >> es[i].u >> es[i].v >> es[i].cost;
  }
  cout << kruskal() << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

#define MAX_V 10000
#define INF INT_MAX / 2 - 1

typedef long long ll;

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

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

edge es[MAX_V];
int V, E;

int par[MAX_V];
int rank_[MAX_V];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rank_[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 (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); }

ll kruskal() {
  sort(es, es + E, comp);
  init(V);
  ll 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;
    }
  }
  return res;
}

int main() {
  cin >> V >> E;
  int s, t, w;
  for (int i = 0; i < E; i++) {
    cin >> es[i].u >> es[i].v >> es[i].cost;
  }
  cout << kruskal() << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vint;
#define mp make_pair
#define fst first
#define scn second
const int inf = 1e9;
int color[10010];
int findparent(int i) {
  return (i == color[i] ? i : color[i] = findparent(color[i]));
}
void merge(int i, int j) {
  i = findparent(color[i]);
  j = findparent(color[j]);
  color[j] = i;
}
int main() {
  int v, e;
  cin >> v >> e;
  priority_queue<pair<int, pii>> pq;
  for (int i = 0; i < e; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    pq.push(mp(-w, mp(s, t)));
  }
  for (int i = 0; i < v; i++)
    color[i] = i;
  int ret = 0;
  while (!pq.empty()) {
    auto it = pq.top();
    pq.pop();
    int w = -it.fst;
    int s = it.scn.fst, t = it.scn.scn;
    if (findparent(s) == findparent(t))
      continue;
    ret += w;
    merge(s, t);
  }
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int group[10000];
vector<tuple<int, int, int>> ls;
int ufind(int a) {
  if (group[a] == a)
    return a;
  else
    return group[a] = ufind(group[a]);
}
void merge(int a, int b) {
  int s = ufind(a);
  int t = ufind(b);
  group[s] = t;
  a = ufind(a);
}
int main() {
  int v, e;
  cin >> v >> e;
  for (int i = 0; i < v; i++)
    group[i] = i;
  for (int i = 0; i < e; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    ls.push_back(make_tuple(w, s, t));
  }
  sort(ls.begin(), ls.end());
  int ans = 0;
  while (!ls.empty()) {
    int w = get<0>(ls[0]), s = get<1>(ls[0]), t = get<2>(ls[0]);
    ls.erase(ls.begin());
    if (ufind(s) == ufind(t))
      continue;
    ans += w;
    merge(s, t);
  }
  cout << ans << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: 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[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;
}


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

// AOJ GRL_2_A 無向辺

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long

typedef int Weight;
struct Edge { // src:辺の始点,dst:辺の終点,weight:辺の重さ
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
                              : // 辺は重さが重いものを"小さい"と定義する
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

// 引数 Graph:隣接リスト,r:最小全域木の始点
// 戻値 total:最小全域木の重み,T:最小全域木の辺集合
pair<Weight, Edges> minimumSpanningTree(const Graph &g, int r = 0) {
  int n = g.size();
  Edges T;
  Weight total = 0;

  vector<bool> visited(n);
  priority_queue<Edge> Q;
  Q.push(Edge(-1, r, 0));
  while (!Q.empty()) {
    Edge e = Q.top();
    Q.pop();
    if (visited[e.dst])
      continue;
    T.push_back(e);
    total += e.weight;
    visited[e.dst] = true;
    for (auto f = g[e.dst].begin(); f != g[e.dst].end(); ++f)
      if (!visited[f->dst])
        Q.push(*f);
  }
  return pair<Weight, Edges>(total, T);
}
int main() {
  ll v, e;
  cin >> v >> e;
  Graph g(v);
  ll s, t, w;
  for (ll i = 0; i < e; i++) {
    cin >> s >> t >> w;
    g[s].push_back(Edge(s, t, w));
    g[t].push_back(Edge(t, s, w));
  }
  pair<Weight, Edges> x = minimumSpanningTree(g);
  ll ans = x.first;
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int root(int x);
void unite(int x, int y);
int same(int x, int y);
int *p;

int main() {
  int v, e;
  scanf("%d %d", &v, &e);
  vector<tuple<int, int, int>> tu(e);
  int s, t, w;
  for (int k = 0; k < e; k++) {
    scanf("%d %d %d", &s, &t, &w);
    tu[k] = make_tuple(w, s, t);
  }
  sort(tu.begin(), tu.end());
  int par[v];
  p = &par[0];
  for (int i = 0; i < v; i++) {
    par[i] = i;
  }
  unite(get<2>(tu[0]), get<1>(tu[0]));
  int ans = get<0>(tu[0]);
  int r;
  for (int k = 1; k < e; k++) {
    // printf("%d %d %d\\n",get<0>(tu[k]),get<1>(tu[k]),get<2>(tu[k]));
    if (same(get<2>(tu[k]), get<1>(tu[k])) == 0) {
      unite(get<2>(tu[k]), get<1>(tu[k]));
      ans = ans + get<0>(tu[k]);
    }
    for (r = 0; r < v; r++) {
      if (par[r] != get<1>(tu[0]))
        break;
    }
    if (r == (v - 1))
      break;
  }
  printf("%d\\n", ans);
}

int root(int x) {
  if (*(p + x) == x) { // 根
    return x;
  } else {
    return *(p + x) = root(*(p + x)); // 経路圧縮
  }
}

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
  } else {
    *(p + x) = y; // xの親をyの親にする
  }
}

int same(int x, int y) { return root(x) == root(y); }
Problem p02364 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#define MAX_V 10000
#define MAX_E 100000
#define INF 100000000
using namespace std;

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

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

edge es[MAX_E];
int V, E;

int par[MAX_E];
int rank[MAX_E];

void init_union(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rank[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 (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); }

int kruskal() {
  sort(es, es + E, comp);
  init_union(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;
    }
  }
  return res;
}

int main() {
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    es[i].u = s;
    es[i].v = t;
    es[i].cost = w;
  }

  cout << kruskal() << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define MAX_V 10000
#define MAX_E 100000
#define INF 100000000
using namespace std;

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

int prim(int v) {
  mincost[0] = 0;
  int res = 0;

  while (true) {
    int ver = -1;
    for (int i = 0; i < v; i++) {
      if (used[i] == 0 && (ver == -1 || mincost[i] < mincost[ver]))
        ver = i;
    }
    if (ver == -1)
      break;

    used[ver] = 1;
    res += mincost[ver];

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

void init() {
  for (int i = 0; i < MAX_V; i++) {
    for (int j = 0; j < MAX_V; j++) {
      cost[i][j] = INF;
    }
  }
  for (int i = 0; i < MAX_V; i++) {
    mincost[i] = INF;
    used[i] = 0;
  }
}

int main() {
  init();
  int v, e;
  cin >> v >> e;
  for (int i = 0; i < e; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    cost[s][t] = w;
    cost[t][s] = w;
  }
  cout << prim(v) << endl;
  return 0;
}
Problem p02364 (C++) — buggy submission, verdict: 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 << 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;
}

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

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

Error: 0

Fixed submission:

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