alexjercan/bugnet huggingface dataset:
Problem p00189 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int n;
    cin >> n;

    if (n == 0) {
      break;
    }

    int *start = new int[n];
    int *goal = new int[n];
    int *cost = new int[n];

    for (int i = 0; i < n; i++) {
      cin >> start[i] >> goal[i] >> cost[i];
    }

    int town = 0;

    for (int i = 0; i < n; i++) {
      town = max(town, max(start[i], goal[i]));
    }

    town++;

    vector<vector<int>> dist(town, vector<int>(town, INT_MAX / 2));

    for (int i = 0; i < n; i++) {
      dist[start[i]][goal[i]] = cost[i];
      dist[goal[i]][start[i]] = cost[i];
    }

    for (int i = 0; i < town; i++) {
      dist[i][i] = 0;
    }

    for (int k = 0; k < town; k++) {
      for (int i = 0; i < town; i++) {
        for (int j = 0; j < town; j++) {
          dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
        }
      }
    }
    int *total = new int[town];
    int TOTAL = INT_MAX / 2;
    int S = 0;

    for (int i = 0; i < town; i++) {
      total[i] = 0;
    }

    for (int i = 0; i < town; i++) {
      for (int j = 0; j < town; j++) {
        total[i] += dist[i][j];
      }
      if (TOTAL > total[i]) {
        TOTAL = min(TOTAL, total[i]);
        S = i;
      }
    }

    cout << S << " " << TOTAL << endl;
    delete[] start;
    delete[] goal;
    delete[] cost;
    delete[] total;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVi;

int main() {
  while (true) {
    int n;
    cin >> n;

    if (n == 0) {
      break;
    }

    VI start;
    VI goal;
    VI cost;

    int s, g, c;

    for (int i = 0; i < n; i++) {
      cin >> s >> g >> c;
      start.push_back(s);
      goal.push_back(g);
      cost.push_back(c);
    }

    int town;

    for (int i = 0; i < n; i++) {
      town = max(start[i], goal[i]);
    }
    town++;

    VVI map(town, VI(town, INT_MAX / 2));

    for (int i = 0; i < n; i++) {
      map[start[i]][goal[i]] = cost[i];
      map[goal[i]][start[i]] = cost[i];
    }

    for (int i = 0; i < town; i++) {
      map[i][i] = 0;
    }

    for (int k = 0; k < town; k++) {
      for (int i = 0; i < town; i++) {
        for (int j = 0; j < town; j++) {
          map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
        }
      }
    }

    int num;
    int total = INT_MAX / 4;

    for (int i = 0; i < town; i++) {
      int sum = 0;
      for (int j = 0; j < town; j++) {
        sum += map[i][j];
      }

      if (sum < total) {
        num = i;
        total = sum;
      }
    }

    cout << num << " " << total << endl;
  }
}
Problem p00189 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

const int INF = 1 << 29;
const int N = 10;

int main() {
  int n;
  while (true) {
    cin >> n;
    if (!n)
      break;

    int dp[N][N];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        dp[i][j] = INF;

    int a, b, c;
    while (n--) {
      cin >> a >> b >> c;
      dp[a][b] = c;
      dp[b][a] = c;
    }

    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          if (dp[i][j] > dp[i][k] + dp[k][j])
            dp[i][j] = dp[i][k] + dp[k][j];
        }
      }
    }

    int sum[N];
    for (int i = 0; i < N; i++) {
      sum[i] = 0;
      for (int j = 0; j < N; j++) {
        if (i != j && dp[i][j] != INF) {
          sum[i] += dp[i][j];
        }
      }
    }

    int ans_i = 0;
    for (int i = 0; i < N; i++) {
      if (sum[i] < sum[ans_i] && sum[i] != 0) {
        ans_i = i;
      }
    }

    cout << ans_i << " " << sum[ans_i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const int INF = 1 << 29;
const int N = 10;

int main() {
  int n;
  while (true) {
    cin >> n;
    if (!N)
      break;

    int dp[N][N];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        dp[i][j] = INF;

    int a, b, c;
    while (n--) {
      cin >> a >> b >> c;
      dp[a][b] = c;
      dp[b][a] = c;
    }

    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          if (dp[i][j] > dp[i][k] + dp[k][j])
            dp[i][j] = dp[i][k] + dp[k][j];
        }
      }
    }

    int sum[N];
    for (int i = 0; i < N; i++) {
      sum[i] = 0;
      for (int j = 0; j < N; j++) {
        if (i != j && dp[i][j] != INF) {
          sum[i] += dp[i][j];
        }
      }
    }

    int ans_i = 0;
    for (int i = 0; i < N; i++) {
      if (sum[i] < sum[ans_i] && sum[i] != 0) {
        ans_i = i;
      }
    }

    cout << ans_i << " " << sum[ans_i] << endl;
  }
}
Problem p00189 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, x, n) for (int i = x; i < n; i++)
#define pf push_front
#define pb push_back
#define int long long
using namespace std;
const int INF = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int N = 11;

int mp[N][N];

int bfs(int n, int p) {
  vector<int> time(n);
  queue<int> que;
  rep(i, n) time[i] = INF;
  time[p] = 0;
  que.push(p);
  int u;
  while (!que.empty()) {
    u = que.front(), que.pop();
    rep(i, n) {
      if (time[i] > time[u] + mp[u][i]) {
        time[i] = time[u] + mp[u][i];
        que.push(i);
      }
    }
  }
  int sum = 0;
  rep(i, time.size()) sum += time[i];
  return sum;
}

signed main() {
  int n;
  while (cin >> n, n != 0) {
    rep(i, N) rep(j, N) mp[i][j] = i == j ? 0 : INF;
    int city[N] = {};
    rep(i, n) {
      int a, b, c;
      cin >> a >> b >> c;
      mp[a][b] = mp[b][a] = c;
      city[a]++;
      city[b]++;
    }
    vector<int> d;
    rep(i, N) if (city[i] > 0) d.pb(i);
    int e = d.size();
    int ans1, ans2 = INF;
    rep(i, e) {
      int temp = bfs(e, d[i]);
      if (ans2 > temp) {
        ans2 = temp;
        ans1 = i;
      }
    }
    cout << ans1 << ' ' << ans2 << endl;
  }
  return 0;
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, x, n) for (int i = x; i < n; i++)
#define pf push_front
#define pb push_back
#define int long long
using namespace std;
const int INF = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int N = 11;

int mp[N][N];

int bfs(int n, int p) {
  vector<int> time(n);
  queue<int> que;
  rep(i, n) time[i] = INF;
  time[p] = 0;
  que.push(p);
  int u;
  while (!que.empty()) {
    u = que.front(), que.pop();
    rep(i, n) {
      if (time[i] > time[u] + mp[u][i]) {
        time[i] = time[u] + mp[u][i];
        que.push(i);
      }
    }
  }
  int sum = 0;
  rep(i, time.size()) sum += time[i];
  return sum;
}

signed main() {
  int n;
  while (cin >> n, n != 0) {
    rep(i, N) rep(j, N) mp[i][j] = (i == j ? 0 : INF);
    int machi[N] = {};
    rep(i, n) {
      int a, b, c;
      cin >> a >> b >> c;
      mp[a][b] = mp[b][a] = c;
      machi[a]++;
      machi[b]++;
    }
    int c = 0;
    rep(i, N) {
      if (machi[i] > 0)
        c++;
    }
    int minsum = INF, city;
    rep(i, n) {
      int tmp = bfs(c, i);

      if (minsum > tmp) {
        minsum = tmp;
        city = i;
      }
    }
    cout << city << ' ' << minsum << endl;
  }
  return 0;
}
Problem p00189 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int INF = 1 << 28;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    int m = 0;
    vector<vector<int>> dp(10, vector<int>(10, INF));
    for (int i = 0; i < n; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      dp[a][b] = dp[b][a] = c;
      m = max(m, max(a, b));
    }
    ++m;

    for (int i = 0; i < 10; ++i)
      dp[i][i] = 0;

    for (int k = 0; k < m; ++k) {
      for (int i = 0; i < m; ++i) {
        for (int j = 0; j < m; ++j)
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }

    int index = 0, sum = INF;
    for (int i = 0; i < m; ++i) {
      int tmp = 0;
      for (int j = 0; j < m; ++j)
        tmp += dp[i][j];

      if (sum > tmp) {
        sum = tmp;
        index = i;
      }
    }

    cout << index << " " << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int INF = 1 << 28;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    int m = 0;
    vector<vector<int>> dp(10, vector<int>(10, INF));
    for (int i = 0; i < n; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      dp[a][b] = dp[b][a] = c;
      dp[i][i] = 0;
      m = max(m, max(a, b));
    }
    ++m;

    for (int k = 0; k < m; ++k) {
      for (int i = 0; i < m; ++i) {
        for (int j = 0; j < m; ++j)
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }

    int index = 0, sum = INF;
    for (int i = 0; i < m; ++i) {
      int tmp = 0;
      for (int j = 0; j < m; ++j)
        tmp += dp[i][j];

      if (sum > tmp) {
        sum = tmp;
        index = i;
      }
    }

    cout << index << " " << sum << endl;
  }
  return 0;
}
Problem p00189 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>

#define SIZE 100
#define INF 10000000

typedef struct {
  int data;
  int pos;
} DAT;

using namespace std;

int main() {

  DAT ans;
  int i, j, k;
  int a, b, c;
  int n;
  int time[SIZE][SIZE];

  while (1) {

    int sum[SIZE] = {0};
    cin >> n;
    if (n == 0)
      break;

    for (i = 0; i < n + 1; i++) {
      for (j = 0; j < n + 1; j++) {
        if (i == j) {
          time[i][j] = 0;
        } else {
          time[i][j] = INF;
        }
      }
    }

    int max = 0;
    for (i = 0; i < n; i++) {
      cin >> a >> b >> c;
      time[a][b] = c;
      time[b][a] = c;
      if (max < a)
        max = a;
      if (max < b)
        max = b;
    }

    for (i = 0; i < max + 1; i++) {
      for (j = 0; j < max + 1; j++) {
        for (k = 0; k < max + 1; k++) {
          time[j][k] = min(time[j][k], time[j][i] + time[i][k]);
        }
      }
    }

    for (i = 0; i < max + 1; i++) {
      for (j = 0; j < max + 1; j++) {
        sum[i] += time[i][j];
      }
    }

    ans.data = INF;
    ans.pos = -1;
    for (i = 0; i < max + 1; i++) {
      if (sum[i] < ans.data) {
        ans.data = sum[i];
        ans.pos = i;
      }
    }

    printf("%d %d\\n", ans.pos, ans.data);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>

#define SIZE 100
#define INF 10000000

typedef struct {
  int data;
  int pos;
} DAT;

using namespace std;

int main() {

  DAT ans;
  int i, j, k;
  int a, b, c;
  int n;
  int time[SIZE][SIZE];

  while (1) {

    int sum[SIZE] = {0};
    cin >> n;

    for (i = 0; i < n + 1; i++) {
      for (j = 0; j < n + 1; j++) {
        if (i == j) {
          time[i][j] = 0;
        } else {
          time[i][j] = INF;
        }
      }
    }

    int max = 0;
    for (i = 0; i < n; i++) {
      cin >> a >> b >> c;
      time[a][b] = c;
      time[b][a] = c;
      if (max < a)
        max = a;
      if (max < b)
        max = b;
    }

    for (i = 0; i < max + 1; i++) {
      for (j = 0; j < max + 1; j++) {
        for (k = 0; k < max + 1; k++) {
          time[j][k] = min(time[j][k], time[j][i] + time[i][k]);
        }
      }
    }

    for (i = 0; i < max + 1; i++) {
      for (j = 0; j < max + 1; j++) {
        sum[i] += time[i][j];
      }
    }

    ans.data = INF;
    ans.pos = -1;
    for (i = 0; i < max + 1; i++) {
      if (sum[i] < ans.data) {
        ans.data = sum[i];
        ans.pos = i;
      }
    }

    printf("%d %d\\n", ans.pos, ans.data);
  }

  return 0;
}
Problem p00189 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
const int INF = 1e9;
struct edge {
  int to, cost;
};
int N, city;
vector<edge> G[100];
int dijkstra(int s) {
  int d[city];
  REP(i, city) d[i] = INF;
  d[s] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, s));
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    REP(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] == -1 || d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        Q.push(P(d[e.to], e.to));
      }
    }
  }
  int sum = 0;
  REP(i, city) sum += d[i];
  return sum;
}
int main() {
  while (cin >> N && N) {
    REP(i, 10) G[i].clear();
    city = -1;
    REP(i, N) {
      int a, b, c;
      cin >> a >> b >> c;
      G[a].PB((edge){b, c});
      G[b].PB((edge){a, c});
      city = max(city, max(a, b));
    }
    city++;
    ll ans = INF, pos = -1;
    REP(i, city) {
      int sum = dijkstra(i);
      if (ans > sum) {
        ans = sum;
        pos = i;
      }
    }
    cout << pos << " " << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define INF 10000001
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const ull B = 100000007;
struct edge {
  int to, cost;
};
int N;
int d[10];
bool f[10];
vector<edge> G[10];
void dijkstra(int s) {
  REP(i, N) d[i] = INT_MAX;
  d[s] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, s));
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    int v = p.second;
    if (d[v] > p.first)
      continue;
    REP(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        Q.push(P(d[e.to], e.to));
      }
    }
  }
}
int main() {
  while (cin >> N && N) {
    memset(f, false, sizeof(f));
    REP(i, N) {
      int a, b, c;
      cin >> a >> b >> c;
      G[a].PB((edge){b, c});
      G[b].PB((edge){a, c});
      f[a] = f[b] = true;
    }
    ll ans = INT_MAX, pos = -1;
    REP(i, 10) {
      if (!f[i])
        continue;
      int sum = 0;
      dijkstra(i);
      REP(j, 10) if (f[j]) sum += d[j];
      if (ans > sum) {
        ans = sum;
        pos = i;
      }
    }
    cout << pos << " " << ans << endl;
  }
}
Problem p00189 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

#define MAXV 10
#define INF 100000000
int main() {

  int n;
  int v;
  int a, b, c;
  int d[MAXV][MAXV];
  int sum, ans, ans_i;

  while (1) {

    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < MAXV; i++) {
      for (int j = 0; j < MAXV; j++) {
        if (i == j)
          d[i][j] = 0;
        else
          d[i][j] = INF;
      }
    }

    v = -1;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c;
      v = max(v, max(a, b));
      d[a][b] = c;
      d[b][a] = c;
    }

    for (int k = 0; k <= v; k++) {
      for (int i = 0; i <= v; i++) {
        for (int j = 0; j <= v; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }

    ans = INF, ans_i = -1;
    for (int i = 0; i <= v; i++) {
      sum = 0;
      for (int j = 0; j <= v; j++) {
        if (d[i][j] != INF)
          sum += d[i][j];
      }
      if (ans > sum && sum != 0) {
        ans = sum;
        ans_i = i;
      }
    }

    cout << ans_i << " " << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define MAXV 10
#define INF 100000000
int main() {

  int n;
  int v;
  int a, b, c;
  int d[MAXV][MAXV];
  int sum, ans, ans_i;

  while (1) {

    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          d[i][j] = 0;
        else
          d[i][j] = INF;
      }
    }

    v = -1;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c;
      v = max(v, max(a, b));
      d[a][b] = c;
      d[b][a] = c;
    }

    for (int k = 0; k <= v; k++) {
      for (int i = 0; i <= v; i++) {
        for (int j = 0; j <= v; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }

    ans = INF, ans_i = -1;
    for (int i = 0; i <= v; i++) {
      sum = 0;
      for (int j = 0; j <= v; j++) {
        if (d[i][j] != INF)
          sum += d[i][j];
      }
      if (ans > sum && sum != 0) {
        ans = sum;
        ans_i = i;
      }
    }

    cout << ans_i << " " << ans << endl;
  }
}
Problem p00189 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;

  while (cin >> n && n != 0) {
    int min_ = 1000000000; // 答えの最小値
    int min_num = 1;       // 答えの町
    int cost[100][100];
    int max_ = 0; // 町がどれだけあるか
    int from, to, time;

    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        cost[i][j] = 100000000;
      }
    }
    for (int i = 0; i < 100; i++) {
      cost[i][i] = 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> from >> to >> time;
      cost[from][to] = time;
      cost[to][from] = time;
      max_ = max(max(from, to), max_);
    }
    for (int k = 0; k <= max_; k++) {
      for (int i = 0; i <= max_; i++) {
        for (int j = 0; j <= max_; j++) {
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
        }
      }
    }
    // 答えを求める
    for (int i = 0; i <= max_; i++) {
      int total = 0;
      for (int j = 0; j <= max_; j++) {
        total += cost[i][j];
      }
      if (total < min_) {
        min_ = total;
        min_num = i;
      }
    }
    cout << min_num << " " << min_ << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef struct edge {
  int from;
  int to;
  int time;
} edge;

int main() {
  int n;

  while (cin >> n && n != 0) {
    int min_ = 1000000000;
    int min_num = 1;
    vector<edge> town;
    int cost[50][50];
    int max_ = 0;

    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        cost[i][j] = 100000000;
      }
    }
    for (int i = 0; i < 100; i++) {
      cost[i][i] = 0;
    }
    for (int i = 0; i < n; i++) {
      edge tmp;
      cin >> tmp.from >> tmp.to >> tmp.time;
      cost[tmp.from][tmp.to] = tmp.time;
      cost[tmp.to][tmp.from] = tmp.time;
      max_ = max(max(tmp.from, tmp.to), max_);
    }
    max_++;
    for (int i = 0; i < max_; i++) {
      for (int j = 0; j < max_; j++) {
        for (int k = 0; k < max_; k++) {
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
        }
      }
    }
    for (int i = 0; i < max_; i++) {
      int total = 0;
      for (int j = 0; j < max_; j++) {
        total += cost[i][j];
      }
      if (total < min_) {
        min_ = total;
        min_num = i;
      }
    }
    cout << min_num << " " << min_ << endl;
  }
  return 0;
}
Problem p00189 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int inf = 0x3f3f3f3f;
int map[15][15];
void floyd(int n) {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        map[j][k] = min(map[j][k], map[j][i] + map[i][k]);
      }
    }
  }
}
void init(int n) {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      map[i][j] = map[j][i] = inf;
    }
    map[i][i] = 0;
  }
}
int main() {
  int n;
  while (scanf("%d", &n), n) {
    init(15);
    int m = 0;
    for (int i = 0; i < n; ++i) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      map[a][b] = map[b][a] = min(map[a][b], c);
      m = max(max(m, a), b);
    }
    floyd(m + 1);
    int ans = inf, res = 0;
    for (int i = 0; i <= m; ++i) {
      int p = 0;
      for (int j = 0; j <= m; ++j) {
        if (map[i][j] != inf) {
          p += map[i][j];
        }
      }
      if (p < ans) {
        ans = p;
        res = i;
      }
    }
    printf("%d %d\\n", res, ans);
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define N 1000015
#define P pair<int, int>
#define ll long long
#define mk(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
bool vis[102];
int dis[30];
int g[30][30];
int inf = 0x3f3f3f3f;
int n, sum;
void Dijkstra(int x) {
  mem(vis, false);
  mem(dis, inf);
  dis[x] = 0;
  // dis[1] = inf;
  for (int i = 0; i <= sum; ++i) {
    int MIN = inf, u = -1;
    for (int j = 0; j <= sum; ++j) {
      if (vis[j])
        continue;
      if (dis[j] < MIN) {
        MIN = dis[j];
        u = j;
      }
    }
    if (u == -1)
      return;
    vis[u] = true;
    for (int j = 0; j <= sum; ++j) {
      if (vis[j])
        continue;
      if (dis[j] > g[u][j] + MIN) {
        dis[j] = g[u][j] + MIN;
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  while (cin >> n, n) {
    int s, e, c;
    mem(g, inf);
    sum = -1;
    for (int i = 0; i < n; ++i) {
      cin >> s >> e >> c;
      sum = max(s, sum);
      sum = max(e, sum);
      g[s][e] = g[e][s] = min(g[s][e], c);
    }
    int ans_n, ans_v = inf;
    for (int i = 0; i <= sum; ++i) {
      Dijkstra(i);
      int sum_temp = 0;
      for (int j = 0; j <= sum; ++j) {
        if (dis[j] == inf) {
          sum_temp = inf;
          break;
        }
        sum_temp += dis[j];
      }
      if (sum_temp < ans_v) {
        ans_v = sum_temp;
        ans_n = i;
      }
    }
    cout << ans_n << " " << ans_v << endl;
  }

  return 0;
}