Problem p00235 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int main() {
  for (int n; cin >> n, n;) {
    vector<vector<pii>> g(n);
    rep(i, n - 1) {
      int a, b, t;
      cin >> a >> b >> t;
      a--, b--;
      g[a].emplace_back(b, t);
      g[b].emplace_back(a, t);
    }

    vi leaf(n);
    repi(i, 1, n) leaf[i] = g[i].size() == 1;

    int sum = 0;
    rep(i, n) rep(j, g[i].size()) {
      if (leaf[i] || leaf[g[i][j].first])
        continue;
      sum += g[i][j].second;
    }

    vvi d(n, vi(n, INFTY));
    rep(i, n) {
      d[i][i] = 0;
      rep(j, g[i].size()) d[i][g[i][j].first] = g[i][j].second;
    }
    rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

    vi par(n);
    rep(i, n) if (leaf[i]) par[g[i][0].first] = 1;
    int mx = 0;
    rep(i, n) if (par[i]) mx = max(mx, d[0][i]);

    cout << sum - mx << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int main() {
  for (int n; cin >> n, n;) {
    vvi d(n, vi(n, INFTY));
    rep(i, n) d[i][i] = 0;
    rep(i, n - 1) {
      int a, b, t;
      cin >> a >> b >> t;
      a--, b--;
      d[a][b] = d[b][a] = t;
    }

    vi leaf(n);
    rep(i, n) leaf[i] = count(all(d[i]), INFTY) == n - 2;

    rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

    vi p(1, 0);
    repi(i, 1, n) if (!leaf[i]) p.push_back(i);
    int res = INFTY;
    do {
      int tmp = 0;
      rep(i, p.size() - 1) tmp += d[p[i]][p[i + 1]];
      res = min(res, tmp);
    } while (next_permutation(1 + all(p)));

    cout << res << endl;
  }

  return 0;
}