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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, b, n) for (int i = b; i < n; ++i)
#define REPR(i, b, n) for (int i = n - 1; i >= b; --i)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n >> b) & 1)
#define PI acos(-1.0)

#define INF 1 << 14

using namespace std;

static const double EPS = 1e-9;
typedef pair<int, int> pii;
typedef long long ll;

int N, a, b, t;
int amat[22][22] = {{0}};
int dist[22][22] = {{0}};
int brg_count[22] = {0, INF, 0};
int far_land = 0;

void warshall_floyd() {

  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= N; ++j)
      dist[i][j] = (amat[i][j] > 0 ? amat[i][j] : INF);

  for (int k = 1; k <= N; ++k)
    for (int i = 1; i <= N; ++i)
      for (int j = 1; j <= N; ++j)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

  return;
}

int solve() {

  warshall_floyd();

  int sum = 0;

  for (int i = 1; i <= N; ++i) {
    for (int j = i; j <= N; ++j) {

      if (amat[i][j] && brg_count[i] > 1 && brg_count[j] > 1)
        sum += 2 * amat[i][j];
    }
  }

  int res = sum;

  for (int k = 2; k <= N; ++k) {

    if (brg_count[k] > 1)
      res = min(res, sum - dist[1][k]);
  }

  if (res == sum)
    res = 0;

  return res;
}

int main() {

  while (cin >> N, N) {

    CLR(amat);
    CLR(dist);
    CLR(brg_count);
    brg_count[1] = INF;
    far_land = 0;

    for (int i = 0; i < N - 1; ++i) {
      cin >> a >> b >> t;
      amat[a][b] = amat[b][a] = t;
      brg_count[a]++;
      brg_count[b]++;
    }

    cout << solve() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, b, n) for (int i = b; i < n; ++i)
#define REPR(i, b, n) for (int i = n - 1; i >= b; --i)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n >> b) & 1)
#define PI acos(-1.0)

#define LIM 100001

using namespace std;

static const double EPS = 1e-9;
typedef pair<int, int> pii;
typedef long long ll;

int N, a, b, t;
int amat[21][21] = {{0}};
int dist[21][21] = {{0}};
int brg_count[21] = {0, LIM, 0};
int far_land = 0;

void warshall_floyd() {

  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= N; ++j)
      dist[i][j] = (amat[i][j] > 0 ? amat[i][j] : LIM);

  for (int k = 1; k <= N; ++k)
    for (int i = 1; i <= N; ++i)
      for (int j = 1; j <= N; ++j)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

  return;
}

int solve() {

  warshall_floyd();

  int res = 0;
  int far_dst = 0;

  for (int i = 2; i <= N; ++i) {
    if (dist[1][i] < LIM && dist[1][i] > far_dst && brg_count[i] > 1) {
      far_land = i;
      far_dst = dist[1][i];
    }
  }

  for (int i = 1; i <= N; ++i) {
    for (int j = i; j <= N; ++j) {

      if (amat[i][j] && brg_count[i] > 1 && brg_count[j] > 1)
        res += 2 * amat[i][j];
    }
  }

  return res - dist[1][far_land];
}

int main() {

  while (cin >> N, !cin.eof()) {

    for (int i = 0; i < N - 1; ++i) {
      cin >> a >> b >> t;
      amat[a][b] = amat[b][a] = t;
      brg_count[a]++;
      brg_count[b]++;
    }

    cin >> a;

    cout << solve() << endl;
  }

  return 0;
}