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

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int s = 0, const int d = 0, const Weight w = 0)
      : src(s), dst(d), weight(w) {}
};

using Edges = std::vector<Edge>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

class Graph {
  std::vector<Edges> g;
  using iterator = std::vector<Edges>::iterator;
  using const_iterator = std::vector<Edges>::const_iterator;

public:
  Graph(const int size = 0) : g(size) {}
  int size() const { return g.size(); }
  const Edges &operator[](const int i) const { return g[i]; }
  void addArc(const int src, const int dst, const Weight w = 1) {
    g[src].emplace_back(src, dst, w);
  }
  void addEdge(const int node1, const int node2, const Weight w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
  iterator begin() { return g.begin(); }
  const_iterator begin() const { return g.begin(); }
  iterator end() { return g.end(); }
  const_iterator end() const { return g.end(); }
};

Graph toRootedTree(const Graph &g, const int root = 0) {
  int n = g.size();
  Graph tree(n);
  std::vector<int> ord(n, -1);
  std::queue<int> q;
  q.push(root);
  int k = 0;
  ord[root] = k++;
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (auto &e : g[u]) {
      int v = e.dst;
      if (ord[v] == -1) {
        ord[v] = k++;
        q.push(v);
        tree.addArc(u, v, e.weight);
      }
    }
  }
  return tree;
}

template <int inf = std::numeric_limits<Weight>::max() / 8>
std::pair<std::vector<Weight>, std::vector<int>> dijkstra(const Graph &g,
                                                          const int src) {
  using state = std::pair<Weight, int>;
  std::priority_queue<state, std::vector<state>, std::greater<state>> q;
  std::vector<Weight> dist(g.size(), inf);
  std::vector<int> via(g.size());
  dist[src] = 0;
  q.emplace(0, src);
  while (q.size()) {
    Weight d;
    int v;
    std::tie(d, v) = q.top();
    q.pop();
    if (dist[v] < d)
      continue;
    for (auto &e : g[v]) {
      if (cmin(dist[e.dst], dist[v] + e.weight)) {
        via[e.dst] = v;
        q.emplace(dist[e.dst], e.dst);
      }
    }
  }
  return {dist, via};
}

main {
  int n;
  while (cin >> n, n) {
    Graph g(n);
    rep(_, n - 1) {
      int a, b, t;
      cin >> a >> b >> t;
      g.addEdge(--a, --b, t);
    }
    Graph h = toRootedTree(g, 0);
    int d = 0;
    for (auto &es : h) {
      for (auto &e : es) {
        if (h[e.dst].size() != 0)
          d += e.weight * 2;
      }
    }
    V<Weight> dist = dijkstra(h, 0).first;
    {
      int maxi = -INF;
      rep(i, dist.size()) if (h[i].size() != 0) cmax(maxi, dist[i]);
      d -= maxi;
    }
    cout << d << endl;
  }
}

Error: -6
0db5ceb9-cbf8-4675-aa74-def03efaee60.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00235/C++/s682565608.cpp:130: int main(): Assertion `maxi < 1' failed.


Fixed submission:

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int s = 0, const int d = 0, const Weight w = 0)
      : src(s), dst(d), weight(w) {}
};

using Edges = std::vector<Edge>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

class Graph {
  std::vector<Edges> g;
  using iterator = std::vector<Edges>::iterator;
  using const_iterator = std::vector<Edges>::const_iterator;

public:
  Graph(const int size = 0) : g(size) {}
  int size() const { return g.size(); }
  const Edges &operator[](const int i) const { return g[i]; }
  void addArc(const int src, const int dst, const Weight w = 1) {
    g[src].emplace_back(src, dst, w);
  }
  void addEdge(const int node1, const int node2, const Weight w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
  iterator begin() { return g.begin(); }
  const_iterator begin() const { return g.begin(); }
  iterator end() { return g.end(); }
  const_iterator end() const { return g.end(); }
};

Graph toRootedTree(const Graph &g, const int root = 0) {
  int n = g.size();
  Graph tree(n);
  std::vector<int> ord(n, -1);
  std::queue<int> q;
  q.push(root);
  int k = 0;
  ord[root] = k++;
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (auto &e : g[u]) {
      int v = e.dst;
      if (ord[v] == -1) {
        ord[v] = k++;
        q.push(v);
        tree.addArc(u, v, e.weight);
      }
    }
  }
  return tree;
}

template <int inf = std::numeric_limits<Weight>::max() / 8>
std::pair<std::vector<Weight>, std::vector<int>> dijkstra(const Graph &g,
                                                          const int src) {
  using state = std::pair<Weight, int>;
  std::priority_queue<state, std::vector<state>, std::greater<state>> q;
  std::vector<Weight> dist(g.size(), inf);
  std::vector<int> via(g.size());
  dist[src] = 0;
  q.emplace(0, src);
  while (q.size()) {
    Weight d;
    int v;
    std::tie(d, v) = q.top();
    q.pop();
    if (dist[v] < d)
      continue;
    for (auto &e : g[v]) {
      if (cmin(dist[e.dst], dist[v] + e.weight)) {
        via[e.dst] = v;
        q.emplace(dist[e.dst], e.dst);
      }
    }
  }
  return {dist, via};
}

main {
  int n;
  cin >> n;
  Graph g(n);
  rep(_, n - 1) {
    int a, b, t;
    cin >> a >> b >> t;
    g.addEdge(--a, --b, t);
  }
  Graph h = toRootedTree(g, 0);
  int d = 0;
  for (auto &es : h) {
    for (auto &e : es) {
      if (h[e.dst].size() != 0)
        d += e.weight * 2;
    }
  }
  V<Weight> dist = dijkstra(h, 0).first;
  {
    int maxi = -INF;
    rep(i, dist.size()) if (h[i].size() != 0) cmax(maxi, dist[i]);
    assert(maxi < 1);
    d -= maxi;
  }
  cout << d << endl;
}