Problem p02364 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

typedef long long ll;

// ll:9187201950435737471 int:2139062143
#define setinf(x) memset((x), 127, sizeof(x))
#define MAX_V 100

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      makeSet(i);
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }

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

  void unite(int x, int y) { link(findSet(x), findSet(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

class edge {
public:
  int from, to, cost;
  edge(int from = 0, int to = 0, int cost = 0)
      : from(from), to(to), cost(cost) {}
  bool operator<(const edge &e) const { return cost > e.cost; }
};

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

int V, E;
vector<edge> graph[MAX_V];

int solve() {
  int mincost[V];
  bool used[V];
  memset(used, false, sizeof(used));
  setinf(mincost);
  mincost[0] = 0;
  int ret = 0;

  priority_queue<edge> pque;
  for (int i = 0; i < graph[0].size(); i++) {
    pque.push(graph[0][i]);
  }
  used[0] = true;
  while (true) {
    while (!pque.empty() && used[pque.top().to])
      pque.pop();
    if (pque.empty())
      break;

    int p = pque.top().to;
    used[p] = true;
    ret += pque.top().cost;
    pque.pop();
    for (int i = 0; i < graph[p].size(); i++) {
      edge e = graph[p][i];
      if (used[e.to])
        continue;
      pque.push(e);
    }
  }

  return ret;
}

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

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

typedef long long ll;

// ll:9187201950435737471 int:2139062143
#define setinf(x) memset((x), 127, sizeof(x))
#define MAX_V 10000

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      makeSet(i);
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }

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

  void unite(int x, int y) { link(findSet(x), findSet(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

class edge {
public:
  int from, to, cost;
  edge(int from = 0, int to = 0, int cost = 0)
      : from(from), to(to), cost(cost) {}
  bool operator<(const edge &e) const { return cost > e.cost; }
};

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

int V, E;
vector<edge> graph[MAX_V];

int solve() {
  int mincost[V];
  bool used[V];
  memset(used, false, sizeof(used));
  setinf(mincost);
  mincost[0] = 0;
  int ret = 0;

  priority_queue<edge> pque;
  for (int i = 0; i < graph[0].size(); i++) {
    pque.push(graph[0][i]);
  }
  used[0] = true;
  while (true) {
    while (!pque.empty() && used[pque.top().to])
      pque.pop();
    if (pque.empty())
      break;

    int p = pque.top().to;
    used[p] = true;
    ret += pque.top().cost;
    pque.pop();
    for (int i = 0; i < graph[p].size(); i++) {
      edge e = graph[p][i];
      if (used[e.to])
        continue;
      pque.push(e);
    }
  }

  return ret;
}

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