alexjercan/bugnet huggingface dataset:
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  while (cin >> n, n) {
    int need, ans = 0;
    vector<int> d(n);
    rep(i, n) cin >> d[i];

    cin >> need;
    int l = 0, r = n - 1, m;
    do {
      ans++;
      m = (l + r) / 2;
      if (d[m] > need)
        r = m - 1;
      else if (d[m] < need)
        l = m + 1;
    } while (need != d[m] && m != r && m != l);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  while (cin >> n, n) {
    int need, ans = 0;
    vector<int> d(n);
    rep(i, n) cin >> d[i];

    cin >> need;
    int l = 0, r = n - 1;
    while (l != r) {
      ans++;
      int m = (l + r) / 2;
      if (d[m] > need)
        r = m - 1;
      else if (d[m] < need)
        l = m + 1;
      else
        break;
    }
    cout << ans + 1 << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int main() {
  for (int n; cin >> n && n;) {
    int target, ans = 0;
    vint v(n);

    rep(i, n) cin >> v[i];

    cin >> target;

    for (int lb = 0, ub = n - 1; lb <= ub; ans++) {
      int mid = (ub + lb) / 2;
      if (v[mid] < target)
        lb = mid + 1;
      if (v[mid] > target)
        ub = mid - 1;
      if (v[mid] == target) {
        ans++;
        break;
      }
    }

    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int main() {
  for (int n; cin >> n;) {
    int target, ans = 1;
    vint v(n);

    rep(i, n) cin >> v[i];

    cin >> target;

    for (int lb = 0, ub = n; lb <= ub; ans++) {
      int mid = (ub + lb) / 2;
      if (v[mid] < target)
        lb = mid + 1;
      if (v[mid] > target)
        ub = mid - 1;
      if (v[mid] == target)
        break;
    }

    cout << ans << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    int a[101], key;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    cin >> key;

    int low = 0, high = n - 1, mid, cnt = 0;
    while (low <= high) {
      cnt++;
      mid = (low + high) / 2;

      if (a[mid] == key) {
        break;
      } else if (a[mid] < key) {
        if (low == mid + 1)
          break;
        low = mid + 1;
      } else {
        if (high == mid - 1)
          break;
        high = mid - 1;
      }
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, key, a[101];
  while (cin >> n, n) {
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    cin >> key;
    bool flag = true;
    int begin = 0;
    int end = n - 1;
    int cnt = 1;
    for (; flag && begin != end; ++cnt) {
      int pos = (begin + end) / 2;
      int val = a[pos];
      if (key == val) {
        flag = false;
      } else {
        if (key < val) {
          end = pos - 1;
        } else {
          begin = pos + 1;
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, search;
  while (cin >> n && n) {
    int inp[101];
    for (int i = 0; i < n; i++)
      cin >> inp[i];
    cin >> search;

    int ans = 0;
    int e = n - 1, s = 0;
    int p = (e - s) / 2;

    while (s <= e) {
      ans++;
      if (inp[p] == search)
        break;
      if (inp[p] > search)
        e = p - 1;
      else
        s = p + 1;

      p = (s + e) / 2;
    }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, search;
  while (cin >> n && n) {
    int inp[101];
    for (int i = 0; i < n; i++)
      cin >> inp[i];
    cin >> search;

    int *s = inp, *e = inp + n - 1, *p = inp + (n - 1) / 2;
    int ans = 0;
    int b = 0;
    while (1) {
      ans++;
      if (p == s && p == e)
        break;
      if (*p == search)
        break;
      else if (*p > search) {
        e = p - 1;
        p = s + (e - s) / 2;
      } else if (*p < search) {
        s = p + 1;
        p = s + (e - s) / 2;
      }
    }

    cout << ans << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, a[101], X;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    cin >> X;
    int high = n - 1, low = 0, cnt = 0;
    while (high >= low) {
      cnt++;
      int center = (low + high) / 2;
      if (a[center] == X)
        break;
      else if (a[center] < X)
        low = center + 1;
      else
        high = center - 1;
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
  int set[101], n, k, cnt, a, b;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> set[i];
    }

    cin >> k;
    cnt = 0;
    n--;
    for (int i = 0, j = n; i != j; cnt++) {
      a = (i + j) / 2;
      if (set[a] == k)
        break;
      else if (set[a] < k)
        i = a + 1;
      else
        j = a - 1;
    }
    cout << cnt + 1;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;

int main() {

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<int> vec;
    rep(i, n) {
      int in;
      cin >> in;
      vec.pb(in);
    }
    int key, cnt = 0;
    cin >> key;
    int l, r, m;
    l = 0, r = n - 1;
    while (l <= r) {
      m = (l + r) / 2;
      cnt++;
      if (vec[m] == key)
        break;
      if (vec[m] > key)
        r = m - 1;
      else
        l = m + 1;
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;

int main() {

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<int> vec;
    rep(i, n) {
      int in;
      cin >> in;
      vec.pb(in);
    }
    int key, cnt = 0;
    cin >> key;
    int l, r, m;
    l = 0, r = n - 1;
    while (l <= r) {
      m = (l + r) / 2;
      cnt++;
      if (vec[m] == key)
        break;
      if (vec[m] > key)
        r = m;
      else
        l = m + 1;
    }
    cout << cnt << endl;
  }

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

// Warshall-floyd
#include <algorithm>
#include <iostream>
using namespace std;
#define INF 114514810

int n, a, b, c, v, d[11][11]; // v: num of nodes

void init() {
  for (int i = 0; i < 11; i++) {
    for (int j = 0; j < 11; j++) {
      if (i == j) // If not exist cost, cost=INF
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
  }
}

int main(void) {
  ios::sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    init();

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

    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]);
        }
      }
    }

    int city, dist = INF;
    for (int i = 0; i < v; i++) {
      int sum = 0;
      for (int j = 0; j < v; j++) {
        sum += d[i][j];
      }
      if (sum < dist) {
        dist = sum;
        city = i;
      }
    }
    cout << city << " " << dist << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// Warshall-floyd
#include <algorithm>
#include <iostream>
using namespace std;
#define INF (1 << 21)

int n, a, b, c, v, d[11][11]; // v: num of nodes

void init() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) // If not exist cost, cost=INF
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
  }
}

int main(void) {
  ios::sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    init();

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

    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]);
        }
      }
    }

    int city, dist = INF;
    for (int i = 0; i < v; i++) {
      int sum = 0;
      for (int j = 0; j < v; j++) {
        sum += d[i][j];
      }
      if (sum < dist) {
        dist = sum;
        city = i;
      }
    }
    cout << city << " " << dist << endl;
  }

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

#include <algorithm>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define pb push_back

typedef long long ll;

using namespace std;

const int mod = 1e9 + 7;
const int INF = 1 << 25;

void solve(int n) {
  int dp[11][11];
  fill(dp[0], dp[10], INF);
  rep(i, 9) dp[i][i] = 0;
  int a, b, c;
  int first = 10, last = 0;
  int ans1;
  ll ans2 = INF, temp;
  rep(i, n) {
    cin >> a >> b >> c;
    first = min(first, min(a, b));
    last = max(last, max(a, b));
    dp[a][b] = dp[b][a] = c;
  }
  REP(k, first, last + 1) REP(i, first, last + 1) REP(j, first, last + 1) {
    dp[j][i] = dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
  }
  REP(i, first, last + 1) {
    temp = 0;
    REP(j, first, last + 1) { temp += dp[i][j]; }
    if (temp < ans2) {
      ans2 = temp;
      ans1 = i;
    }
  }
  cout << ans1 << " " << ans2 << endl;

  return;
}
int main() {
  int n;
  while (true) {
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define pb push_back

typedef long long ll;

using namespace std;

const int mod = 1e9 + 7;
const int INF = 1 << 25;

void solve(int n) {
  int dp[11][11];
  fill(dp[0], dp[10], INF);
  rep(i, 9) dp[i][i] = 0;
  int a, b, c;
  int first = 10, last = 0;
  int ans1;
  ll ans2 = INF, temp;
  rep(i, n) {
    cin >> a >> b >> c;
    first = min(first, min(a, b));
    last = max(last, max(a, b));
    dp[a][b] = dp[b][a] = c;
  }
  rep(k, n) rep(i, n) rep(j, n) {
    dp[j][i] = dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
  }
  REP(i, first, last + 1) {
    temp = 0;
    REP(j, first, last + 1) { temp += dp[i][j]; }
    if (temp < ans2) {
      ans2 = temp;
      ans1 = i;
    }
  }
  cout << ans1 << " " << ans2 << endl;

  return;
}
int main() {
  int n;
  while (true) {
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}
Problem p00189 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

enum { maxn = 20, INF = 10000000000000LL };

typedef long long LL;
LL G[maxn][maxn];
int vis[maxn];
LL d[maxn][maxn];
int inq[maxn];
int m;

void spfa(int src) {
  memset(inq, 0, sizeof inq);
  for (int i = 0; i < maxn; i++)
    d[src][i] = INF;
  d[src][src] = 0LL;
  queue<int> q;
  while (!q.empty())
    q.pop();
  q.push(src);
  inq[src] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < maxn; i++) {
      if (G[u][i] != -1) {
        if (d[src][u] + G[u][i] < d[src][i]) {
          d[src][i] = d[src][u] + G[u][i];
          if (!inq[i]) {
            inq[i] = 1;
            q.push(i);
          }
        }
      }
    }
    inq[u] = 0;
  }
}

int main() {
  while (~scanf("%d", &m), m) {
    memset(vis, 0, sizeof(vis));
    memset(G, -1, sizeof(G));
    memset(d, 0, sizeof(d));
    for (int i = 0; i < m; i++) {
      int a, b;
      LL c;
      scanf("%d%d%lld", &a, &b, &c);
      G[a][b] = G[a][b] == -1 ? c : min(G[a][b], c);
      G[b][a] = G[b][a] == -1 ? c : min(c, G[b][a]);
      vis[a] = vis[b] = 1;
    }
    for (int i = 0; i < maxn; i++)
      if (vis[i])
        spfa(i);
    LL ans = INF;
    int pt = -1;
    for (int i = 0; i < maxn; i++) {
      LL sum = 0;
      if (vis[i]) {
        for (int j = 0; j < maxn; j++) {
          if (vis[j]) {
            sum += d[i][j];
          }
        }
        if (sum < ans) {
          ans = sum;
          pt = i;
        }
      }
    }
    printf("%d %lld\\n", pt, ans);
  }
  return 0;
}
/*

6
0 1 80
1 2 20
0 2 60
2 3 50
3 4 60
1 4 90
2
0 1 1
1 2 1
0


*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

enum { maxn = 50, INF = 1000000000 };

int G[maxn][maxn];
int vis[maxn];
int d[maxn][maxn];
int inq[maxn];
int m;

void spfa(int src) {
  memset(inq, 0, sizeof inq);
  for (int i = 0; i < maxn; i++)
    d[src][i] = INF;
  d[src][src] = 0;
  queue<int> q;
  while (!q.empty())
    q.pop();
  q.push(src);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < maxn; i++) {
      if (G[u][i]) {
        if (d[src][u] + G[u][i] < d[src][i]) {
          d[src][i] = d[src][u] + G[u][i];
        }
        if (!inq[i]) {
          inq[i] = 1;
          q.push(i);
        }
      }
    }
    inq[u] = 0;
  }
}

int main() {
  while (~scanf("%d", &m), m) {
    memset(vis, 0, sizeof(vis));
    memset(G, 0, sizeof(G));
    memset(d, 0, sizeof(d));
    for (int i = 0; i < m; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      G[a][b] = G[b][a] = c;
      vis[a] = vis[b] = 1;
    }
    for (int i = 0; i < maxn; i++)
      if (vis[i])
        spfa(i);
    int ans = INF, pt = -1;
    for (int i = 0; i < maxn; i++) {
      int sum = 0;
      if (vis[i]) {
        for (int j = 0; j < maxn; j++) {
          if (vis[j]) {
            sum += d[i][j];
          }
        }
        if (sum < ans) {
          ans = sum;
          pt = i;
        }
      }
    }
    printf("%d %d\\n", pt, ans);
  }
  return 0;
}
Problem p00189 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

#define INF 10000
#define MAX_V 10

int d[MAX_V][MAX_V];
int n, m, a, b, c, num, t;

void Init() {
  t = INF;
  m = 0; // ツ陳ャツづ個青板。
  for (int i = 0; i < MAX_V; i++)
    for (int j = 0; j < MAX_V; j++)
      i == j ? d[i][j] = 0 : d[i][j] = INF;
}

void Input() {
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    m = max(a, max(b, m)); // ツ陳ャツづ個青板。
    d[a][b] = d[b][a] = c;
  }
}

void wf() {
  int tmp = 0;
  for (int k = 0; k <= m; k++)
    for (int i = 0; i <= m; i++)
      for (int j = 0; j <= m; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= m; j++) {
      if (d[i][j] != INF || i != j)
        tmp += d[i][j];
    }
    if (tmp < t)
      t = tmp, num = i;
    tmp = 0;
  }
  return;
}

int main() {
  while (cin >> n, n) {
    Init();
    Input();
    wf();
    printf("%d %d\\n", num, t);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string.h>
#include <vector>
#define INF 999999999
using namespace std;

struct edge {
  int From;
  int To;
  int Dist;
};

void solve() {
  int n;
  while (cin >> n, n) {
    vector<vector<int>> D(10, vector<int>(10));
    vector<edge> Edge(n);
    for (int i = 0; i < n; ++i) {
      cin >> Edge[i].From >> Edge[i].To >> Edge[i].Dist;
    }
    int city_num = 0;
    for (int i = 0; i < n; ++i) {
      city_num = max(city_num, Edge[i].From);
      city_num = max(city_num, Edge[i].To);
    }
    city_num++;
    for (int i = 0; i < city_num; ++i) {
      for (int j = 0; j < city_num; ++j) {
        D[i][j] = INF;
      }
    }
    for (int i = 0; i < n; ++i) {
      D[i][i] = 0;
      D[Edge[i].From][Edge[i].To] = Edge[i].Dist;
      D[Edge[i].To][Edge[i].From] = Edge[i].Dist;
    }

    bool update = true;
    while (update) {
      update = false;
      for (int i = 0; i < city_num; ++i) {
        for (int j = 0; j < city_num; ++j) {
          for (int k = 0; k < city_num; ++k) {
            if (D[i][k] != INF && D[k][j] != INF &&
                (D[i][k] + D[k][j] < D[i][j])) {
              D[i][j] = D[i][k] + D[k][j];
              update = true;
            }
          }
        }
      }
    }
    int min = INF;
    int index = 0;
    for (int i = 0; i < city_num; ++i) {
      int sum = 0;
      for (int j = 0; j < city_num; ++j) {
        sum += D[i][j];
      }
      if (min > sum) {
        min = sum;
        index = i;
      }
    }
    cout << index << " " << min << endl;
  }
}

int main() {
  solve();
  return (0);
}
Problem p00189 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

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

    vector<int> a(n), b(n), c(n);

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

    vector<vector<int>> map(10, vector<int>(10, INT_MAX / 4));

    for (int i = 0; i < n; i++) {
      map[a[i]][b[i]] = c[i];
      map[b[i]][a[i]] = c[i];
    }
    for (int i = 0; i < 10; i++) {
      map[i][i] = 0;
    }

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

    int total[10];

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

    int min_cost = INT_MAX / 2, town;

    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (map[i][j] >= INT_MAX / 4) {
          continue;
        }
        total[i] += map[i][j];
      }
    }

    for (int i = 0; i < 10; i++) {
      if (total[i] == 0) {
        continue;
      }
      if (min_cost > total[i]) {
        min_cost = total[i];
        town = i;
      }
    }

    cout << town << " " << total[town] << endl;
  }
}

Error: MLE

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(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;
  }
}