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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
#define int ll
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
class edge {
public:
  int to;
  int cost;
  void Init() {
    to = 0;
    cost = 0;
  }
  edge(int t, int c) : to(t), cost(c){};
};
vi d(100, INF);
vector<edge> Edge[100];
void dijkstra() {
  d[1] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> que;
  que.push(pii(0, 1));
  while (!que.empty()) {
    pii now = que.top();
    que.pop();
    int to_v = now.second;
    if (d[to_v] < now.first)
      continue;
    rep(i, Edge[to_v].size()) {
      edge e = Edge[to_v][i];
      if (d[e.to] > d[to_v] + e.cost) {
        d[e.to] = d[to_v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}
signed main() {
  int n;
  while (cin >> n, n) {
    vector<pair<pii, int>> data;
    rep(i, 100) Edge[i].clear();
    rep(i, n - 1) {
      int from, to, cost;
      cin >> from >> to >> cost;
      Edge[from].push_back(edge(to, cost));
      Edge[to].push_back(edge(from, cost));
      data.push_back(make_pair(pii(from, to), cost));
    }
    int sum = 0;
    rep(i, n + 1) d[i] = INF;
    dijkstra();
    int maxi = 0;
    for (int i = 0; i < data.size(); i++) {
      if (data[i].first.first == 1) {
        if (Edge[data[i].first.second].size() == 1)
          continue;
      } else if (data[i].first.second == 1) {
        if (Edge[data[i].first.first].size() == 1)
          continue;
      } else if (Edge[data[i].first.first].size() == 1 ||
                 Edge[data[i].first.second].size() == 1)
        continue;
      sum += data[i].second;
    }
    sum *= 2;
    for (int i = 1; i <= n; i++) {
      if (i == 1)
        continue;
      if (Edge[i].size() == 1)
        continue;
      cmax(maxi, d[i]);
    }
    cout << sum - maxi << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
#define int ll
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
class edge {
public:
  int to;
  int cost;
  void Init() {
    to = 0;
    cost = 0;
  }
};
vector<edge> Edge[100];
int dijkstra(int g) {
  int ans;
  int d[100];
  rep(i, 100) d[i] = INF;
  d[1] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> que;
  que.push(pii(0, 1));
  while (!que.empty()) {
    pii now = que.top();
    que.pop();
    int to_v = now.second;
    if (d[to_v] < now.first)
      continue;
    rep(i, Edge[to_v].size()) {
      edge e = Edge[to_v][i];
      if (d[e.to] > d[to_v] + e.cost) {
        d[e.to] = d[to_v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
  return d[g];
}
signed main() {
  int n;
  while (cin >> n, n) {
    int ans = 0;
    vi d(n, INF);
    d[1] = 0;
    rep(i, 100) Edge[i].clear();
    rep(i, n - 1) {
      edge hoge;
      int from;
      cin >> from >> hoge.to >> hoge.cost;
      Edge[from].push_back(hoge);
      edge e;
      e.to = from;
      e.cost = hoge.cost;
      Edge[hoge.to].push_back(e);
    }
    vector<bool> used(100, true);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < Edge[i].size(); j++) {
        edge e = Edge[i][j];
        if (Edge[e.to].size() == 1)
          continue;
        if (!used[Edge[e.to][j].to])
          continue;
        else
          sum += e.cost;
      }
      used[i] = false;
    }
    sum *= 2;
    int maxi = 0;
    rep(i, n) {
      if (Edge[i + 1].size() == 1 || i == 0)
        continue;
      else {
        int val = dijkstra(i + 1);
        // cout<<i+1<<" "<<val<<endl;
        ans += val * 2;
        cmax(maxi, val);
      }
    }
    cout << sum - maxi << endl;
  }
}