prithivMLmods/Coder-Stat huggingface dataset:
Problem p00189 (C++)

Buggy submission (status: 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;

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

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;
    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;
}
Problem p00189 (C++)

Buggy submission (status: 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, 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;
}

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, 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;
}
Problem p00189 (C++)

Buggy submission (status: 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 < 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;
  }
}

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 < 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;
  }
}
Problem p00189 (C++)

Buggy submission (status: 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;
  }
}


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 p00190 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int to[13][5] = {{2, -1},           {2, 5, -1},        {0, 1, 3, 6, -1},
                 {2, 7, -1},        {5, -1},           {1, 4, 6, 9, -1},
                 {2, 5, 7, 10, -1}, {3, 6, 8, 11, -1}, {7, -1},
                 {5, 10, -1},       {6, 9, 11, 12},    {7, 10, -1},
                 {10, -1}};
int X[] = {2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2};
int Y[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4};

int solve(vector<int> data) {
  queue<vector<int>> Q;
  map<vector<int>, int> done;
  Q.push(data);
  done[Q.front()] = 0;

  while (Q.size()) {
    vector<int> q = Q.front();
    Q.pop();
    int dif = 0;
    rep(i, 13) if (q[i]) dif += abs(X[q[i]] - X[i]) + abs(Y[q[i]] - Y[i]);
    if (dif == 0)
      return done[q];
    ;
    if (dif / 2 + done[q] >= 20)
      continue;
    int curcost = done[q];
    rep(x, 13) {
      if (q[x] == 0) {
        rep(i, 5) {
          if (!~to[x][i])
            break;
          swap(q[x], q[to[x][i]]);
          if (done.find(q) == done.end()) {
            done[q] = curcost + 1;
            Q.push(q);
          }
          swap(q[x], q[to[x][i]]);
        }
      }
    }
  }
  return 21;
}
int main() {
  vector<int> data(13);
  while (1) {
    rep(i, 13) if (!(cin >> data[i])) return 0;
    int ans = solve(data);
    if (ans > 20)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int to[13][5] = {{2, -1},           {2, 5, -1},        {0, 1, 3, 6, -1},
                 {2, 7, -1},        {5, -1},           {1, 4, 6, 9, -1},
                 {2, 5, 7, 10, -1}, {3, 6, 8, 11, -1}, {7, -1},
                 {5, 10, -1},       {6, 9, 11, 12},    {7, 10, -1},
                 {10, -1}};
int X[] = {2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2};
int Y[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4};

int solve(vector<int> data) {
  queue<vector<int>> Q;
  map<vector<int>, int> done;
  Q.push(data);
  done[Q.front()] = 0;

  while (Q.size()) {
    vector<int> q = Q.front();
    Q.pop();
    int dif = 0;
    rep(i, 13) if (q[i]) dif += abs(X[q[i]] - X[i]) + abs(Y[q[i]] - Y[i]);
    if (dif == 0)
      return done[q];
    ;
    if (dif + done[q] > 20)
      continue;
    int curcost = done[q];
    rep(x, 13) {
      if (q[x] == 0) {
        rep(i, 5) {
          if (!~to[x][i])
            break;
          swap(q[x], q[to[x][i]]);
          if (done.find(q) == done.end()) {
            done[q] = curcost + 1;
            Q.push(q);
          }
          swap(q[x], q[to[x][i]]);
        }
      }
    }
  }
  return 21;
}
int main() {
  vector<int> data(13);
  while (1) {
    rep(i, 13) if (!(cin >> data[i])) return 0;
    int ans = solve(data);
    if (ans > 20)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00191 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
double g[100][100], dp[100][100];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n || m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        scanf("%lf", &g[i][j]);

    fill_n(dp[0], 100, 1.0);
    fill_n(dp[1], 9900, 0.0);
    for (int i = 1; i < m; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++)
          dp[i][j] = max(dp[i][j], dp[i - 1][k] * g[k][j]);

    printf("%.2f\n", *max_element(dp[m - 1], dp[m - 1] + n));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
double g[100][100], dp[100][100];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n || m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        scanf("%lf", &g[i][j]);

    fill_n(dp[0], 100, 1.0);
    fill_n(dp[1], 9900, 0.0);
    for (int i = 1; i < m; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < n; k++)
          dp[i][j] = max(dp[i][j], dp[i - 1][k] * g[k][j]);

    printf("%.2f\n", *max_element(dp[m - 1], dp[m - 1] + n));
  }
  return 0;
}
Problem p00191 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
double key[100][100];
double table[100][100];

void Init(int n, int m) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      table[i][j] = 0;
}

double memo(int cur, int count, int limit, int kind) {
  if (table[cur][count])
    return table[cur][count];
  if (count == limit)
    return 1.0;
  double Max = 0;
  int prev = 0;
  for (int i = 0; i < kind; i++) {
    double tmp = memo(i, count + 1, limit, kind);
    if (Max <= tmp) {
      // if(Max*key[prev][cur] < tmp*key[i][cur]){
      Max = tmp;
      prev = i;
      //}
    }
  }
  table[cur][count] = Max * key[prev][cur];
  return table[cur][count];
}

int main() {
  int n, m;
  while (cin >> n >> m && ((n != 0) || (m != 0))) {
    Init(n, m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cin >> key[i][j];
    for (int i = 0; i < n; i++)
      table[i][0] = memo(i, 0, m - 1, n);
    double max = 0;
    for (int i = 0; i < n; i++)
      if (max < table[i][0])
        max = table[i][0];
    printf("%0.2f\n", max);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
double key[100][100];
double table[100][100];

void Init(int n, int m) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      table[i][j] = 0;
}

double memo(int cur, int count, int limit, int kind) {
  if (table[cur][count])
    return table[cur][count];
  if (count == limit)
    return 1.0;
  for (int i = 0; i < kind; i++)
    table[cur][count] =
        max(table[cur][count], memo(i, count + 1, limit, kind) * key[i][cur]);
  return table[cur][count];
}

int main() {
  int n, m;
  while (cin >> n >> m && ((n != 0) || (m != 0))) {
    Init(n, m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cin >> key[i][j];
    for (int i = 0; i < n; i++)
      table[i][0] = memo(i, 0, m - 1, n);
    double max = 0;
    for (int i = 0; i < n; i++)
      if (max < table[i][0])
        max = table[i][0];
    printf("%0.2f\n", max);
  }

  return 0;
}
Problem p00191 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  cout << setprecision(2);
  cout << setiosflags(ios::fixed);
  int n, m, i, j, k;
  long double M;
  while (cin >> n >> m, n | m) {
    vector<vector<long double>> manure(n, vector<long double>(n)),
        dp(m, vector<long double>(n, 0.0));
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
        cin >> manure[i][j];
    for (j = 0; j < n; j++)
      dp[0][j] = 1.0;
    for (i = 1; i < m; i++) {
      for (j = 0; j < n; j++) {
        for (k = 0; k < n; k++) {
          dp[i][j] = max(dp[i][j], dp[i - 1][k] * manure[k][j]);
        }
      }
    }
    M = 0.0;
    for (j = 0; j < n; j++)
      M = max(M, dp[n - 1][j]);
    cout << M << endl;
  }
}

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  cout << setprecision(2);
  cout << setiosflags(ios::fixed);
  int n, m, i, j, k;
  long double M;
  while (cin >> n >> m, n | m) {
    vector<vector<long double>> manure(n, vector<long double>(n)),
        dp(m, vector<long double>(n, 0.0));
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
        cin >> manure[i][j];
    for (j = 0; j < n; j++)
      dp[0][j] = 1.0;
    for (i = 1; i < m; i++) {
      for (j = 0; j < n; j++) {
        for (k = 0; k < n; k++) {
          dp[i][j] = max(dp[i][j], dp[i - 1][k] * manure[k][j]);
        }
      }
    }
    M = 0.0;
    for (j = 0; j < n; j++)
      M = max(M, dp[m - 1][j]);
    cout << M << endl;
  }
}
Problem p00193 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

struct st {
  int x, y, c;
};
int d[100][100], b[100][100], x[11], y[11];
int dx[][6]{{
                0,
                -1,
                0,
                1,
                1,
                1,
            },
            {-1, -1, -1, 0, 1, 0}};
int dy[]{-1, 0, 1, 1, 0, -1};
int main() {
  int m, n, s;
  while (scanf("%d%d", &m, &n), m) {
    scanf("%d", &s);
    rep(i, s) scanf("%d%d", &x[i], &y[i]), x[i]--, y[i]--;
    int t;
    scanf("%d", &t);
    int Max = 0;
    rep(i, t) {
      scanf("%d%d", &x[s], &y[s]);
      x[s]--;
      y[s]--;
      queue<st> que;
      memset(d, -1, sizeof(d));
      rep(j, s + 1) {
        d[x[j]][y[j]] = 0;
        b[x[j]][y[j]] = j;
        que.push({x[j], y[j], j});
      }
      while (!que.empty()) {
        st p = que.front();
        que.pop();
        rep(j, 6) {
          int nx = p.x + dx[!(p.y & 1)][j], ny = p.y + dy[j];
          if (0 <= nx && nx < m && 0 <= ny && ny < n) {
            if (d[nx][ny] == -1) {
              d[nx][ny] = d[p.x][p.y] + 1;
              b[nx][ny] = p.c;
              que.push({nx, ny, p.c});
            } else if (d[nx][ny] == d[p.x][p.y] + 1) {
              b[nx][ny] = -1;
              que.push({nx, ny, p.c});
            }
          }
        }
      }
      int cnt = 0;
      rep(i, m) rep(j, n) {
        if (b[i][j] == s)
          cnt++;
      }
      Max = max(Max, cnt);
    }
    printf("%d\n", Max);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

struct st {
  int x, y, c;
};
int d[100][100], b[100][100], x[11], y[11];
int dx[][6]{{
                0,
                -1,
                0,
                1,
                1,
                1,
            },
            {-1, -1, -1, 0, 1, 0}};
int dy[]{-1, 0, 1, 1, 0, -1};
int main() {
  int m, n, s;
  while (scanf("%d%d", &m, &n), m) {
    scanf("%d", &s);
    rep(i, s) scanf("%d%d", &x[i], &y[i]), x[i]--, y[i]--;
    int t;
    scanf("%d", &t);
    int Max = 0;
    rep(i, t) {
      scanf("%d%d", &x[s], &y[s]);
      x[s]--;
      y[s]--;
      queue<st> que;
      memset(d, -1, sizeof(d));
      rep(j, s + 1) {
        d[x[j]][y[j]] = 0;
        b[x[j]][y[j]] = j;
        que.push({x[j], y[j], j});
      }
      while (!que.empty()) {
        st p = que.front();
        que.pop();
        rep(j, 6) {
          int nx = p.x + dx[!(p.y & 1)][j], ny = p.y + dy[j];
          if (0 <= nx && nx < m && 0 <= ny && ny < n) {
            if (d[nx][ny] == -1) {
              d[nx][ny] = d[p.x][p.y] + 1;
              b[nx][ny] = p.c;
              que.push({nx, ny, p.c});
            } else if (d[nx][ny] == d[p.x][p.y] + 1 && b[nx][ny] != p.c) {
              b[nx][ny] = -1;
              que.push({nx, ny, p.c});
            }
          }
        }
      }
      int cnt = 0;
      rep(i, m) rep(j, n) {
        if (b[i][j] == s)
          cnt++;
      }
      Max = max(Max, cnt);
    }
    printf("%d\n", Max);
  }
}
Problem p00193 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> grid;

const int INF = 1e9;
const int dx[6] = {0, 1, 1, 1, 0, -1}, dy[6] = {-1, -1, 0, 1, 1, 0};
int h, w;

grid bfs(int y, int x) {
  grid res(h, vi(w, INF));
  res[y][x] = 0;
  queue<int> q;
  q.push(y * w + x);

  while (q.size()) {
    y = q.front() / w;
    x = q.front() % w;
    q.pop();
    int c = res[y][x];

    for (int i = 0; i < 6; i++) {
      int ny = y + dy[i],
          nx = x + dx[i] + ((y % 2 == 0 && i % 3 != 2) ? -1 : 0);
      if (ny < 0 || nx < 0 || ny >= h || nx >= w)
        continue;
      if (res[ny][nx] > c + 1) {
        res[ny][nx] = c + 1;
        q.push(ny * w + nx);
      }
    }
  }
  return res;
}

int main() {
  while (cin >> h >> w, h) {
    grid g(h, vi(w, INF));

    int s;
    cin >> s;
    for (int z = 0; z < s; z++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      grid tmp = bfs(b, a);

      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++)
          g[i][j] = min(g[i][j], tmp[i][j]);
      }
    }

    int t, res = 0;
    cin >> t;
    for (int z = 0; z < t; z++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      grid tmp = bfs(b, a);
      int cover = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (g[i][j] > tmp[i][j])
            cover++;
        }
      }
      res = max(res, cover);
    }

    cout << res << endl;
  }
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> grid;

const int INF = 1e9;
const int dx[6] = {0, 1, 1, 1, 0, -1}, dy[6] = {-1, -1, 0, 1, 1, 0};
int h, w;

grid bfs(int y, int x) {
  grid res(h, vi(w, INF));
  res[y][x] = 0;
  queue<int> q;
  q.push(y * w + x);

  while (q.size()) {
    y = q.front() / w;
    x = q.front() % w;
    q.pop();
    int c = res[y][x];

    for (int i = 0; i < 6; i++) {
      int ny = y + dy[i],
          nx = x + dx[i] + ((y % 2 == 0 && i % 3 != 2) ? -1 : 0);
      if (ny < 0 || nx < 0 || ny >= h || nx >= w)
        continue;
      if (res[ny][nx] > c + 1) {
        res[ny][nx] = c + 1;
        q.push(ny * w + nx);
      }
    }
  }
  return res;
}

int main() {
  while (cin >> w >> h, w) {
    grid g(h, vi(w, INF));

    int s;
    cin >> s;
    for (int z = 0; z < s; z++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      grid tmp = bfs(b, a);

      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++)
          g[i][j] = min(g[i][j], tmp[i][j]);
      }
    }

    int t, res = 0;
    cin >> t;
    for (int z = 0; z < t; z++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      grid tmp = bfs(b, a);
      int cover = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (g[i][j] > tmp[i][j])
            cover++;
        }
      }
      res = max(res, cover);
    }

    cout << res << endl;
  }
}
Problem p00195 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#define buf 10000
using namespace std;
int B[10];
void BubbleSort(void);

int main(void) {
  int i, n, m, A[20001];
  char str[buf];

  for (;;) {
    for (i = 0; i < 5; i++) {
      fgets(str, sizeof(str), stdin);
      sscanf(str, "%d %d", &n, &m);
      if (n == 0 && m == 0)
        break;
      B[i] = n + m;
      A[n + m] = i;
    }
    BubbleSort();
    printf("%c %d\n", 'A' + A[B[0]], B[0]);
  }
  return 0;
}

void BubbleSort(void) {
  int i, j, tmp;
  for (i = 0; i < 4; i++) {
    for (j = 4; j > i; j--) {
      if (B[j] > B[j - 1]) {
        tmp = B[j - 1];
        B[j - 1] = B[j];
        B[j] = tmp;
      }
    }
  }
}

Fixed submission:
#include <cstdio>
#define buf 10000
using namespace std;
int B[10];
void BubbleSort(void);

int main(void) {
  int i, n, m, A[20001];
  char str[buf];

  for (;;) {
    for (i = 0; i < 5; i++) {
      fgets(str, sizeof(str), stdin);
      sscanf(str, "%d %d", &n, &m);
      if (n == 0 && m == 0)
        return 0;
      B[i] = n + m;
      A[n + m] = i;
    }
    BubbleSort();
    printf("%c %d\n", 'A' + A[B[0]], B[0]);
  }
  return 0;
}

void BubbleSort(void) {
  int i, j, tmp;
  for (i = 0; i < 4; i++) {
    for (j = 4; j > i; j--) {
      if (B[j] > B[j - 1]) {
        tmp = B[j - 1];
        B[j - 1] = B[j];
        B[j] = tmp;
      }
    }
  }
}
Problem p00197 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main() {
  int a, b, n, step = 1;
  while (scanf("%d %d", &a, &b) != EOF) {
    if (a <= b) {
      n = a;
      a = b;
      b = n;
    }
    if (a % b == 0)
      n = b;
    while (a % b != 0) {
      step++;
      if (a % b == 0) {
        n = b;
        break;
      }
      a = a % b;
      n = a;
      a = b;
      b = n;
    }
    printf("%d %d\n", n, step);
    step = 1;
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int a, b, n, step = 1;
  while (scanf("%d %d", &a, &b) != 0) {
    if (a == 0 || b == 0)
      break;
    if (a <= b) {
      n = a;
      a = b;
      b = n;
    }
    if (a % b == 0)
      n = b;
    while (a % b != 0) {
      step++;
      if (a % b == 0) {
        n = b;
        break;
      }
      a = a % b;
      n = a;
      a = b;
      b = n;
    }
    printf("%d %d\n", n, step);
    step = 1;
  }
  return 0;
}
Problem p00197 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>

using namespace std;

void change(int *, int *);

int main(void) {
  int x, y;

  while (scanf("%d %d", &x, &y)) {
    int count = 0;
    if (x < y)
      change(&x, &y);
    while (y) {
      count++;
      x %= y;
      change(&x, &y);
    }
    cout << x << ' ' << count << endl;
  }
  return 0;
}

void change(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}

Fixed submission:
#include <cstdio>
#include <iostream>

using namespace std;

void change(int *, int *);

int main(void) {
  int x, y;

  while (1) {
    scanf("%d %d", &x, &y);
    if (x == 0 && y == 0)
      break;
    int count = 0;
    if (x < y)
      change(&x, &y);
    while (y) {
      count++;
      x %= y;
      change(&x, &y);
    }
    cout << x << ' ' << count << endl;
  }
  return 0;
}

void change(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}
Problem p00197 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  while (1) {
    long long x, y, cnt = 0;
    cin >> x >> y;
    if (x < y)
      swap(x, y);
    while (1) {
      cnt++;
      x %= y;
      swap(x, y);
      if (y == 0)
        break;
    }
    printf("%lld %lld\n", x, cnt);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  while (1) {
    long long x, y, cnt = 0;
    cin >> x >> y;
    if (x + y == 0)
      break;
    if (x < y)
      swap(x, y);
    while (1) {
      cnt++;
      x %= y;
      swap(x, y);
      if (y == 0)
        break;
    }
    printf("%lld %lld\n", x, cnt);
  }
  return 0;
}
Problem p00200 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class S {
public:
  S(int pos, int cost) {
    p = pos;
    c = cost;
  }
  int p, c;
};
bool operator<(S a, S b) { return a.c > b.c; }
int m;
vector<S> edge[100][2];
void add_edge(int a, int b, int c, int t) {
  edge[a][0].push_back(S(b, c));
  edge[a][1].push_back(S(b, t));
  edge[b][0].push_back(S(a, c));
  edge[b][1].push_back(S(a, t));
}

int solve(int p, int q, int r) {
  int ans = 0;
  bool use[100];
  for (int i = 0; i < 100; i++) {
    use[i] = 0;
  }

  priority_queue<S> que;
  que.push(S(p, 0));

  while (!que.empty()) {
    S n = que.top();
    que.pop();

    if (n.p == q) {
      ans = n.c;
      break;
    }

    if (use[n.p]) {
      continue;
    }
    use[n.p] = 1;

    for (int i = 0; i < edge[n.p][r].size(); i++) {
      if (!use[edge[n.p][r][i].p]) {
        que.push(S(edge[n.p][r][i].p, n.c + edge[n.p][r][i].c));
      }
    }
  }
  return ans;
}

int main() {
  int n;
  while (cin >> n >> m, n != 0 || m != 0) {
    for (int i = 0; i < n; i++) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      a--;
      b--;
      add_edge(a, b, c, t);
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--;
      q--;
      cout << solve(p, q, r) << endl;
    }
  }
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class S {
public:
  S(int pos, int cost) {
    p = pos;
    c = cost;
  }
  int p, c;
};
bool operator<(S a, S b) { return a.c > b.c; }
int m;
vector<S> edge[100][2];
void add_edge(int a, int b, int c, int t) {
  edge[a][0].push_back(S(b, c));
  edge[a][1].push_back(S(b, t));
  edge[b][0].push_back(S(a, c));
  edge[b][1].push_back(S(a, t));
}

int solve(int p, int q, int r) {
  int ans = 0;
  bool use[100];
  for (int i = 0; i < 100; i++) {
    use[i] = 0;
  }

  priority_queue<S> que;
  que.push(S(p, 0));

  while (!que.empty()) {
    S n = que.top();
    que.pop();

    if (n.p == q) {
      ans = n.c;
      break;
    }

    if (use[n.p]) {
      continue;
    }
    use[n.p] = 1;

    for (int i = 0; i < edge[n.p][r].size(); i++) {
      if (!use[edge[n.p][r][i].p]) {
        que.push(S(edge[n.p][r][i].p, n.c + edge[n.p][r][i].c));
      }
    }
  }
  return ans;
}

int main() {
  int n;
  while (cin >> n >> m, n != 0 || m != 0) {
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 2; j++) {
        edge[i][j].clear();
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      a--;
      b--;
      add_edge(a, b, c, t);
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--;
      q--;
      cout << solve(p, q, r) << endl;
    }
  }
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

struct Edge {
  int to, cost, time;
};

typedef pair<int, int> P;

const int MAX_V = 110;
const int INF = 1 << 30;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  while (cin >> n >> m && n != 0 && m != 0) {

    vector<Edge> vec[MAX_V];
    int d[MAX_V];

    for (int i = 0; i < n; ++i) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      --a, --b;
      vec[a].push_back(Edge{b, c, t});
      vec[b].push_back(Edge{a, c, t});
    }

    int k;
    cin >> k;

    while (k > 0) {
      --k;
      priority_queue<P, vector<P>, greater<P>> que;
      fill(d, d + n, INF);

      int p, q, r;
      cin >> p >> q >> r;
      --p, --q;

      d[p] = 0;
      que.push(P(0, p));

      while (!que.empty()) {
        P p0 = que.top();
        que.pop();
        int v = p0.second;
        if (d[v] < p0.first)
          continue;
        for (int i = 0; i < vec[v].size(); ++i) {
          Edge e = vec[v][i];
          if (r == 0 && d[e.to] > d[v] + e.cost) {
            d[e.to] = d[v] + e.cost;
            que.push(P(d[e.to], e.to));
          }
          if (r == 1 && d[e.to] > d[v] + e.time) {
            d[e.to] = d[v] + e.time;
            que.push(P(d[e.to], e.to));
          }
        }
      }

      cout << d[q] << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

struct Edge {
  int to, cost, time;
};

typedef pair<int, int> P;

const int MAX_V = 3010;
const int INF = 1 << 30;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  while (cin >> n >> m && n != 0 && m != 0) {

    vector<Edge> vec[MAX_V];
    int d[MAX_V];

    for (int i = 0; i < n; ++i) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      --a, --b;
      vec[a].push_back(Edge{b, c, t});
      vec[b].push_back(Edge{a, c, t});
    }

    int k;
    cin >> k;

    while (k > 0) {
      --k;
      priority_queue<P, vector<P>, greater<P>> que;
      fill(d, d + n, INF);

      int p, q, r;
      cin >> p >> q >> r;
      --p, --q;

      d[p] = 0;
      que.push(P(0, p));

      while (!que.empty()) {
        P p0 = que.top();
        que.pop();
        int v = p0.second;
        if (d[v] < p0.first)
          continue;
        for (int i = 0; i < vec[v].size(); ++i) {
          Edge e = vec[v][i];
          if (r == 0 && d[e.to] > d[v] + e.cost) {
            d[e.to] = d[v] + e.cost;
            que.push(P(d[e.to], e.to));
          }
          if (r == 1 && d[e.to] > d[v] + e.time) {
            d[e.to] = d[v] + e.time;
            que.push(P(d[e.to], e.to));
          }
        }
      }

      cout << d[q] << endl;
    }
  }

  return 0;
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
// #include "bipartite-graph.hpp"
// #include <vector>
// using namespace std;
// int main(){
//	vector<vector<int> > G(4);
//	cout << bipartite_graph(G) << endl;
// }

// AOJ0200
#include <iostream>
#include <vector>
using namespace std;

struct edge {
  edge() {}
  edge(int to, int from, int cost, int time)
      : to(to), from(from), cost(cost), time(time) {}
  int to, from, cost, time;
};

const int INF = 100000000;

int d[101];
edge es[2000];
int n, m;

// 頂点sから各頂点への最短経路を求める
void bellman_ford(int s, bool r) {
  for (int i = 0; i < m; ++i)
    d[i] = INF;
  d[s] = 0;
  while (1) {
    bool update = false;
    for (int i = 0; i < n * 2; ++i) {
      edge e = es[i];
      int cost;
      if (r)
        cost = e.time;
      else
        cost = e.cost;
      if (d[e.from] != INF && d[e.to] > d[e.from] + cost) {
        d[e.to] = d[e.from] + cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

int main() {
  while (cin >> n >> m, n + m) {
    for (int i = 0; i < n; ++i) {
      int to, from, cost, time;
      cin >> to >> from >> cost >> time;
      es[i] = edge(to - 1, from - 1, cost, time);
      es[n + i] = edge(from - 1, to - 1, cost, time);
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; ++i) {
      int p, q, r;
      cin >> p >> q >> r;
      bellman_ford(p - 1, r);
      cout << d[q - 1] << endl;
    }
  }
}

Fixed submission:
// #include "bipartite-graph.hpp"
// #include <vector>
// using namespace std;
// int main(){
//	vector<vector<int> > G(4);
//	cout << bipartite_graph(G) << endl;
// }

// AOJ0200
#include <iostream>
#include <vector>
using namespace std;

struct edge {
  edge() {}
  edge(int to, int from, int cost, int time)
      : to(to), from(from), cost(cost), time(time) {}
  int to, from, cost, time;
};

const int INF = 100000000;

int d[101];
edge es[7000];
int n, m;

// 頂点sから各頂点への最短経路を求める
void bellman_ford(int s, bool r) {
  for (int i = 0; i < m; ++i)
    d[i] = INF;
  d[s] = 0;
  while (1) {
    bool update = false;
    for (int i = 0; i < n * 2; ++i) {
      edge e = es[i];
      int cost;
      if (r)
        cost = e.time;
      else
        cost = e.cost;
      if (d[e.from] != INF && d[e.to] > d[e.from] + cost) {
        d[e.to] = d[e.from] + cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

int main() {
  while (cin >> n >> m, n + m) {
    for (int i = 0; i < n; ++i) {
      int to, from, cost, time;
      cin >> to >> from >> cost >> time;
      es[i] = edge(to - 1, from - 1, cost, time);
      es[n + i] = edge(from - 1, to - 1, cost, time);
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; ++i) {
      int p, q, r;
      cin >> p >> q >> r;
      bellman_ford(p - 1, r);
      cout << d[q - 1] << endl;
    }
  }
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 211;
int dpcost[N][N];
int dptime[N][N];

int main() {

A:;
  int n, m;
  cin >> n >> m;
  if (n == 0)
    return 0;

  const int INF = 1000000000;

  rep(i, N) rep(j, N) dpcost[i][j] = dptime[i][j] = INF;
  rep(i, N) dpcost[i][i] = dptime[i][i] = 0;

  rep(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    dpcost[a][b] = c;
    dpcost[b][a] = c;
    dptime[a][b] = d;
    dptime[b][a] = d;
  }

  reps(k, 1, n + 1) reps(i, 1, n + 1) reps(j, 1, n + 1) {
    dpcost[i][j] = min(dpcost[i][j], dpcost[i][k] + dpcost[k][j]);
    dptime[i][j] = min(dptime[i][j], dptime[i][k] + dptime[k][j]);
  }

  int r;
  cin >> r;

  rep(i, r) {
    int a, b, c;
    cin >> a >> b >> c;
    if (c == 0)
      printf("%d\n", dpcost[a][b]);
    if (c == 1)
      printf("%d\n", dptime[a][b]);
  }

  goto A;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

const int N = 211;
int dpcost[N][N];
int dptime[N][N];

int main() {

A:;
  int n, m;
  cin >> n >> m;
  if (n == 0)
    return 0;

  const int INF = 1000000000;

  rep(i, N) rep(j, N) dpcost[i][j] = dptime[i][j] = INF;
  rep(i, N) dpcost[i][i] = dptime[i][i] = 0;

  rep(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    dpcost[a][b] = c;
    dpcost[b][a] = c;
    dptime[a][b] = d;
    dptime[b][a] = d;
  }

  reps(k, 1, m + 1) reps(i, 1, m + 1) reps(j, 1, m + 1) {
    dpcost[i][j] = min(dpcost[i][j], dpcost[i][k] + dpcost[k][j]);
    dptime[i][j] = min(dptime[i][j], dptime[i][k] + dptime[k][j]);
  }

  int r;
  cin >> r;

  rep(i, r) {
    int a, b, c;
    cin >> a >> b >> c;
    if (c == 0)
      printf("%d\n", dpcost[a][b]);
    if (c == 1)
      printf("%d\n", dptime[a][b]);
  }

  goto A;
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define sintttt(i, j, k, m) scanf("%d%d%d%d", &i, &j, &k, &m);

#define INF 1010000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  while (1) {
    int n, m;
    int time[101][101];
    int cost[101][101];

    rep(i, 101) rep(j, 101) time[i][j] = cost[i][j] = INF;

    sintt(n, m);
    if (n == 0)
      break;
    rep(i, n) {
      int a, b, c, d;
      sintttt(a, b, c, d);

      time[a][b] = d;
      time[b][a] = d;

      cost[a][b] = c;
      cost[b][a] = c;
    }

    reps(k, n) {
      reps(i, n) {
        reps(j, n) {
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
          time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
        }
      }
    }

    int q;
    sint(q);
    rep(i, q) {
      int a, b, c;
      sinttt(a, b, c);
      if (c == 1) {
        printf("%d\n", time[a][b]);
      } else {
        printf("%d\n", cost[a][b]);
      }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);
#define sintttt(i, j, k, m) scanf("%d%d%d%d", &i, &j, &k, &m);

#define INF 1010000000
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  while (1) {
    int n, m;
    int time[101][101];
    int cost[101][101];

    rep(i, 101) rep(j, 101) time[i][j] = cost[i][j] = INF;

    sintt(n, m);
    if (n == 0)
      break;
    rep(i, n) {
      int a, b, c, d;
      sintttt(a, b, c, d);

      time[a][b] = d;
      time[b][a] = d;

      cost[a][b] = c;
      cost[b][a] = c;
    }

    reps(k, m) {
      reps(i, m) {
        reps(j, m) {
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
          time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
        }
      }
    }

    int q;
    sint(q);
    rep(i, q) {
      int a, b, c;
      sinttt(a, b, c);
      if (c == 1) {
        printf("%d\n", time[a][b]);
      } else {
        printf("%d\n", cost[a][b]);
      }
    }
  }
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
const int INF = 1 << 20;
const int N = 101;
int cost[N][N];
int tim[N][N];
using namespace std;
int main() {
  int n, m, a, b, co, ti, r;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < m + 2; i++) {
      for (int i2 = 0; i2 < m + 2; i2++) {
        cost[i][i2] = INF;
        tim[i][i2] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> co >> ti;
      cost[a][b] = co;
      cost[b][a] = co;
      tim[a][b] = ti;
      tim[b][a] = ti;
    }
    for (int k = 0; k < m + 1; ++k)
      for (int i = 0; i < m + 1; ++i)
        for (int j = 0; j < m + 1; ++j) {
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
          tim[i][j] = min(tim[i][j], tim[i][k] + tim[k][j]);
        }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> r;
      if (r == 0) {
        cout << cost[a][b] << endl;
      } else
        cout << tim[a][b] << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
const int INF = 1 << 20;
const int N = 101;
int cost[N][N];
int tim[N][N];
using namespace std;
int main() {
  int n, m, a, b, co, ti, r;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < N; i++) {
      for (int i2 = 0; i2 < N; i2++) {
        cost[i][i2] = INF;
        tim[i][i2] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> co >> ti;
      cost[a][b] = co;
      cost[b][a] = co;
      tim[a][b] = ti;
      tim[b][a] = ti;
    }
    for (int k = 0; k < m + 1; ++k)
      for (int i = 0; i < m + 1; ++i)
        for (int j = 0; j < m + 1; ++j) {
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
          tim[i][j] = min(tim[i][j], tim[i][k] + tim[k][j]);
        }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> r;
      if (r == 0) {
        cout << cost[a][b] << endl;
      } else
        cout << tim[a][b] << endl;
    }
  }
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int road1[111][111];
  int road2[111][111];
  int n, m;
  int a, b, cost, time;
  int p, q, r;
  int x;
  while (1) {
    for (int i = 0; i < 111; i++) {
      for (int j = 0; j < 111; j++) {
        road1[i][j] = road2[i][j] = 111111111;
      }
    }
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> cost >> time;
      road1[a][b] = road1[b][a] = cost;
      road2[a][b] = road2[b][a] = time;
    }

    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          if (road1[i][j] > road1[i][k] + road1[k][j]) {
            road1[i][j] = road1[i][k] + road1[k][j];
          }
          if (road2[i][j] > road2[i][k] + road2[k][j]) {
            road2[i][j] = road2[i][k] + road2[k][j];
          }
        }
      }
    }

    cin >> x;
    for (int i = 0; i < x; i++) {
      cin >> p >> q >> r;
      cout << (r == 0 ? road1[p][q] : road2[p][q]) << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int road1[111][111];
  int road2[111][111];
  int n, m;
  int a, b, cost, time;
  int p, q, r;
  int x;
  while (1) {
    for (int i = 0; i < 111; i++) {
      for (int j = 0; j < 111; j++) {
        road1[i][j] = road2[i][j] = 111111111;
      }
    }
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> cost >> time;
      road1[a][b] = road1[b][a] = cost;
      road2[a][b] = road2[b][a] = time;
    }

    for (int k = 1; k <= m; k++) {
      for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= m; j++) {
          if (road1[i][j] > road1[i][k] + road1[k][j]) {
            road1[i][j] = road1[i][k] + road1[k][j];
          }
          if (road2[i][j] > road2[i][k] + road2[k][j]) {
            road2[i][j] = road2[i][k] + road2[k][j];
          }
        }
      }
    }

    cin >> x;
    for (int i = 0; i < x; i++) {
      cin >> p >> q >> r;
      cout << (r == 0 ? road1[p][q] : road2[p][q]) << endl;
    }
  }
}
Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define _m_ 1
const int INF = 1e9;

int cost[101][101];
int tim[101][101];
int d[101];
bool used[101];
int V;

void dijkstra(int s, int o) {
  std::fill(d, d + V, INF);
  std::fill(used, used + V, false);
  d[s] = 0;
  while (_m_) {
    int v = -1;
    for (int u = 0; u < V; ++u) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;
    if (o == 0) {
      for (int u = 0; u < V; ++u) {
        d[u] = std::min(d[u], d[v] + cost[v][u]);
      }
    } else {
      for (int u = 0; u < V; ++u) {
        d[u] = std::min(d[u], d[v] + tim[v][u]);
      }
    }
  }
}

int main() {
  int n, m;
  while (scanf("%d %d", &n, &m), n + m) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cost[i][j] = INF;
        tim[i][j] = INF;
      }
    }
    for (int i = 0; i < n; ++i) {
      int a, b, co, ti;
      scanf("%d %d %d %d", &a, &b, &co, &ti);
      a--, b--;
      cost[a][b] = co, cost[b][a] = co;
      tim[a][b] = ti, tim[b][a] = ti;
    }
    V = m;
    int k;
    scanf("%d", &k);
    for (int i = 0; i < k; ++i) {
      int p, q, r;
      scanf("%d %d %d", &p, &q, &r);
      p--, q--;
      dijkstra(p, r);
      printf("%d\n", d[q]);
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define _m_ 1
const int INF = 1e9;

int cost[101][101];
int tim[101][101];
int d[101];
bool used[101];
int V;

void dijkstra(int s, int o) {
  std::fill(d, d + V, INF);
  std::fill(used, used + V, false);
  d[s] = 0;
  while (_m_) {
    int v = -1;
    for (int u = 0; u < V; ++u) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;
    if (o == 0) {
      for (int u = 0; u < V; ++u) {
        d[u] = std::min(d[u], d[v] + cost[v][u]);
      }
    } else {
      for (int u = 0; u < V; ++u) {
        d[u] = std::min(d[u], d[v] + tim[v][u]);
      }
    }
  }
}

int main() {
  int n, m;
  while (scanf("%d %d", &n, &m), n + m) {
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < m; ++j) {
        cost[i][j] = INF;
        tim[i][j] = INF;
      }
    }
    for (int i = 0; i < n; ++i) {
      int a, b, co, ti;
      scanf("%d %d %d %d", &a, &b, &co, &ti);
      a--, b--;
      cost[a][b] = co, cost[b][a] = co;
      tim[a][b] = ti, tim[b][a] = ti;
    }
    V = m;
    int k;
    scanf("%d", &k);
    for (int i = 0; i < k; ++i) {
      int p, q, r;
      scanf("%d %d %d", &p, &q, &r);
      p--, q--;
      dijkstra(p, r);
      printf("%d\n", d[q]);
    }
  }
}