Problem p02364 (C++) — buggy submission, verdict: Runtime Error

// ????????????????????°??????: ??£??\\?????????

// Library
#include <algorithm>
#include <cassert>
#include <functional>
#include <queue>
#include <vector>
// Type
typedef int Vertex;
typedef int Weight;
struct Edge {
  Vertex from, to;
  Weight weight;
};
bool operator<(const Edge &lhs, const Edge &rhs) {
  if (lhs.weight != rhs.weight)
    return lhs.weight < rhs.weight;
  if (lhs.from != rhs.from)
    return lhs.from < rhs.from;
  return lhs.to < rhs.to;
}
bool operator>(const Edge &lhs, const Edge &rhs) {
  if (lhs.weight != rhs.weight)
    return lhs.weight > rhs.weight;
  if (lhs.from != rhs.from)
    return lhs.from > rhs.from;
  return lhs.to > rhs.to;
}
// Class
class Graph {
public:
  explicit Graph(int size);
  void add(const Vertex &from, const Vertex &to, const Weight &weight = 1);
  const std::vector<Edge> minimum_spanning_tree() const;

private:
  int size_;
  std::vector<std::vector<Edge>> edge_;
};
// Implement
Graph::Graph(int size) : size_(size), edge_(size) {}
void Graph::add(const Vertex &from, const Vertex &to, const Weight &weight) {
  edge_.at(from).push_back({from, to, weight});
  edge_.at(to).push_back({to, from, weight});
}
// Minimum Spanning Tree: Prim's algorithm, O(|E| + |V| log |V|)
const std::vector<Edge> Graph::minimum_spanning_tree() const {
  if (size_ == 1)
    return {};
  assert(
      std::all_of(std::begin(edge_), std::end(edge_),
                  [](const std::vector<Edge> &list) { return !list.empty(); }));
  std::vector<Edge> result;
  std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> q(
      std::begin(edge_.front()), std::end(edge_.front()));
  std::vector<bool> visited(size_, false);
  visited.front() = true;
  while (!q.empty()) {
    auto current = q.top();
    q.pop();
    if (visited.at(current.to))
      continue;
    visited.at(current.to) = true;
    result.push_back(current);
    for (const auto &e : edge_.at(current.to))
      if (!visited[e.to])
        q.push(e);
  }
  assert(result.size() == size_ - 1);
  return result;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int V, E;
  cin >> V >> E;
  Graph g(V);
  for (int i = 0; i < E; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    g.add(s, t, w);
  }
  auto mst = g.minimum_spanning_tree();
  cout << accumulate(begin(mst), end(mst), 0, [](int sum, Edge e) {
    return sum + e.weight;
  }) << endl;
}

Error: 0

Fixed submission:

// ????????????????????°??????: ??£??\\?????????

// Library
#include <algorithm>
#include <cassert>
#include <functional>
#include <queue>
#include <vector>
// Type
typedef int Vertex;
typedef int Weight;
struct Edge {
  Vertex from, to;
  Weight weight;
};
bool operator<(const Edge &lhs, const Edge &rhs) {
  if (lhs.weight != rhs.weight)
    return lhs.weight < rhs.weight;
  if (lhs.from != rhs.from)
    return lhs.from < rhs.from;
  return lhs.to < rhs.to;
}
bool operator>(const Edge &lhs, const Edge &rhs) {
  if (lhs.weight != rhs.weight)
    return lhs.weight > rhs.weight;
  if (lhs.from != rhs.from)
    return lhs.from > rhs.from;
  return lhs.to > rhs.to;
}
// Class
class Graph {
public:
  explicit Graph(int size);
  void add(const Vertex &from, const Vertex &to, const Weight &weight = 1);
  const std::vector<Edge> minimum_spanning_tree() const;

private:
  int size_;
  std::vector<std::vector<Edge>> edge_;
};
// Implement
Graph::Graph(int size) : size_(size), edge_(size) {}
void Graph::add(const Vertex &from, const Vertex &to, const Weight &weight) {
  edge_.at(from).push_back({from, to, weight});
  edge_.at(to).push_back({to, from, weight});
}
// Minimum Spanning Tree: Prim's algorithm, O(|E| + |V| log |V|)
const std::vector<Edge> Graph::minimum_spanning_tree() const {
  assert(
      std::all_of(std::begin(edge_), std::end(edge_),
                  [](const std::vector<Edge> &list) { return !list.empty(); }));
  std::vector<Edge> result;
  std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> q(
      std::begin(edge_.front()), std::end(edge_.front()));
  std::vector<bool> visited(size_, false);
  visited.front() = true;
  while (!q.empty()) {
    auto current = q.top();
    q.pop();
    if (visited.at(current.to))
      continue;
    visited.at(current.to) = true;
    result.push_back(current);
    for (const auto &e : edge_.at(current.to))
      if (!visited[e.to])
        q.push(e);
  }
  assert(result.size() == size_ - 1);
  return result;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int V, E;
  cin >> V >> E;
  Graph g(V);
  for (int i = 0; i < E; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    g.add(s, t, w);
  }
  auto mst = g.minimum_spanning_tree();
  cout << accumulate(begin(mst), end(mst), 0, [](int sum, Edge e) {
    return sum + e.weight;
  }) << endl;
}