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

#include <algorithm>
#include <array>
#include <iostream>
#include <queue>

using namespace std;

int const Inf = 10000000;
int const N = 100;

typedef array<int, 2> line;

struct tg {
  int c, v;
};
bool operator>(tg const &lhs, tg const &rhs) { return lhs.c > rhs.c; }

int main() {
  int n, m;
  priority_queue<tg, vector<tg>, greater<tg>> w;

  while (cin >> n >> m, n || m) {
    line table[N][N] = {};

    for (int i = 0; i < n; ++i) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;

      int const from = a - 1, to = b - 1;
      line const in = {{cost, time}};
      table[from][to] = (table[to][from] = in);
    }

    int k;
    cin >> k;
    for (int i = 0; i < k; ++i) {
      int p, q, r;
      cin >> p >> q >> r;
      int const from = p - 1, to = q - 1;

      bool v[N] = {};
      int d[N];
      fill(d, d + N, Inf);

      tg const val = {0, from};
      w.push(val);
      d[from] = 0;

      while (!w.empty()) {
        int const cur = w.top().v;
        w.pop();

        if (v[cur])
          continue;
        v[cur] = true;

        for (int ii = 0; ii < m; ++ii) {
          if (table[cur][ii][0] == 0)
            continue;

          if (d[cur] + table[cur][ii][r] < d[ii]) {
            d[ii] = d[cur] + table[cur][ii][r];
            tg const val = {d[cur] + table[cur][ii][r], ii};
            w.push(val);
          }

          // cout << cur+1 << " -> " << ii+1 << " c " << d[ii] << endl;
        }
      }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <iostream>
#include <stack>

using namespace std;

int const Inf = 10000000;
int const N = 100;

typedef array<int, 2> line;

int main() {
  int n, m;
  stack<int> w;

  while (cin >> n >> m, n || m) {
    line table[N][N] = {};

    for (int i = 0; i < n; ++i) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;

      int const from = a - 1, to = b - 1;
      line const in = {{cost, time}};
      table[from][to] = (table[to][from] = in);
    }

    int k;
    cin >> k;
    for (int i = 0; i < k; ++i) {
      int p, q, r;
      cin >> p >> q >> r;
      int const from = p - 1, to = q - 1;

      bool v[N] = {};
      int d[N];
      fill(d, d + N, Inf);

      w.push(from);
      d[from] = 0;

      while (!w.empty()) {
        int const cur = w.top();
        w.pop();

        for (int ii = 0; ii < m; ++ii) {
          if (table[cur][ii][0] == 0)
            continue;

          if (d[cur] + table[cur][ii][r] < d[ii]) {
            d[ii] = d[cur] + table[cur][ii][r];
            w.push(ii);
          }

          // cout << cur+1 << " -> " << ii+1 << " c " << d[ii] << endl;
        }
      }

      cout << d[to] << endl;
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, k, n) for (int i = k; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

int N, M, K;
const int MAXN = 128;
const int INF = 1e+9;
int G1[MAXN][MAXN];
int G2[MAXN][MAXN];

int main() {
  while (cin >> M >> N && (M || N)) {
    rep(i, N) {
      rep(j, N) { G1[i][j] = G2[i][j] = (i == j ? 0 : INF); }
    }

    rep(i, M) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      --a;
      --b;
      G1[a][b] = G1[b][a] = c;
      G2[a][b] = G2[b][a] = d;
    }

    rep(point, N) rep(from, N) rep(to, N) {
      G1[from][to] = min(G1[from][to], G1[from][point] + G1[point][to]);
      G2[from][to] = min(G2[from][to], G2[from][point] + G2[point][to]);
    }

    cin >> K;
    rep(i, K) {
      int a, b, c;
      cin >> a >> b >> c;
      --a;
      --b;
      cout << (c ? G2[a][b] : G1[a][b]) << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define REP(i, k, n) for (int i = k; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

int N, M, K;
const int MAXN = 128;
const int INF = 1e9;
int G1[MAXN][MAXN];
int G2[MAXN][MAXN];

int main() {
  while (cin >> M >> N && (M || N)) {
    fill((int *)G1, (int *)&G1[MAXN][MAXN], INF);
    fill((int *)G2, (int *)&G2[MAXN][MAXN], INF);
    // rep(i,N){ rep(j,N){ G1[i][j]=INF; G2[i][j]=INF; } }

    rep(i, M) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      --a;
      --b;
      G1[a][b] = G1[b][a] = c;
      G2[a][b] = G2[b][a] = d;
    }

    rep(from, N) rep(to, N) rep(point, N) {
      G1[from][to] = min(G1[from][to], G1[from][point] + G1[point][to]);
      G2[from][to] = min(G2[from][to], G2[from][point] + G2[point][to]);
    }

    cin >> K;
    rep(i, K) {
      int a, b, c;
      cin >> a >> b >> c;
      --a;
      --b;
      cout << (c ? G2[a][b] : G1[a][b]) << endl;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int a, b, c, d;
    int co[m][m];
    int ti[m][m];
    for (int i = 0; i < m; i++)
      for (int j = 0; j < m; j++) {
        co[i][j] = 100000000;
        ti[i][j] = 100000000;
      }
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> d;
      a--;
      b--;
      co[a][b] = c;
      co[b][a] = c;
      ti[a][b] = d;
      ti[b][a] = d;
    }
    vector<int> used;
    vector<int> dist;
    int k, p, q, r;
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> p >> q >> r;
      p--;
      q--;
      used = vector<int>(m, 100000000);
      dist = vector<int>(m, 100000000);
      dist[p] = 0;
      while (1) {
        int va = -1;
        for (int j = 0; j < m; j++)
          if (used[j] == 100000000 && (va == -1 || dist[va] > dist[j]))
            va = j;
        if (va == -1)
          break;
        used[va] = 1;
        for (int j = 0; j < m; j++)
          dist[j] = min(dist[j], (r == 0 ? co[j][va] : ti[j][va]) + dist[va]);
      }
      cout << dist[q] << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int a, b, c, d;
    int co[n][n];
    int ti[n][n];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        co[i][j] = 100000000;
        ti[i][j] = 100000000;
      }
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> d;
      a--;
      b--;
      co[a][b] = c;
      co[b][a] = c;
      ti[a][b] = d;
      ti[b][a] = d;
    }
    vector<int> used;
    vector<int> dist;
    int k, p, q, r;
    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> p >> q >> r;
      p--;
      q--;
      used = vector<int>(n, 100000000);
      dist = vector<int>(n, 100000000);
      dist[p] = 0;
      while (1) {
        int va = -1;
        for (int j = 0; j < n; j++)
          if (used[j] == 100000000 && (va == -1 || dist[va] > dist[j]))
            va = j;
        if (va == -1)
          break;
        used[va] = 1;
        for (int j = 0; j < n; j++)
          dist[j] = min(dist[j], (r == 0 ? co[j][va] : ti[j][va]) + dist[va]);
      }
      cout << dist[q] << endl;
    }
  }
  return 0;
}
Problem p00200 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

const int INF = 1000000;
int C[101][101], T[101][101];

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      return 0;
    for (int i = 1; i <= m; i++)
      for (int j = 1; j <= m; j++)
        C[i][j] = T[i][j] = INF;
    for (int i = 0; i < n; i++) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;
      C[a][b] = C[b][a] = cost;
      T[a][b] = T[b][a] = time;
    }
    for (int k = 1; k <= m; k++)
      for (int i = 1; i <= m; i++)
        for (int j = 1; j <= m; j++) {
          C[i][j] = min(C[i][j], C[i][k] + C[k][j]);
          T[i][j] = min(T[i][j], T[i][k] + T[k][j]);
        }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      cout << (!r ? C[p][q] : T[p][q]) << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

const int INF = 1000000;
int C[101][101], T[101][101];

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      return 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        C[i][j] = T[i][j] = INF;
    for (int i = 0; i < n; i++) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;
      C[a][b] = C[b][a] = cost;
      T[a][b] = T[b][a] = time;
    }
    for (int k = 1; k <= n; k++)
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
          C[i][j] = min(C[i][j], C[i][k] + C[k][j]);
          T[i][j] = min(T[i][j], T[i][k] + T[k][j]);
        }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      cout << (!r ? C[p][q] : T[p][q]) << endl;
    }
  }
  return 0;
}
Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

#define INF 1000000

std::pair<int, int> list[101][101];
int cost[101];
bool used[101];
int n, m, k;

int dijkstra(int s, int g, int r) {
  used[s] = true;
  if (used[g])
    return cost[g];
  for (int i = 1; i <= m; i++) {
    if (r == 0) {
      if (list[s][i].first != INF) {
        if (cost[i] > cost[s] + list[s][i].first) {
          cost[i] = cost[s] + list[s][i].first;
        }
      }
    } else if (r == 1) {
      if (list[s][i].second != INF) {
        if (cost[i] > cost[s] + list[s][i].second) {
          cost[i] = cost[s] + list[s][i].second;
        }
      }
    }
  }
  int min = 0;
  for (int i = 1; i <= m; i++) {
    if (!used[i]) {
      if (cost[min] > cost[i])
        min = i;
    }
  }
  dijkstra(min, g, r);
}

int main() {
  while (std::cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i <= m; i++) {
      for (int j = 0; j <= m; j++) {
        list[i][j].first = INF;
        list[i][j].second = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      std::cin >> a >> b >> c >> d;
      list[a][b].first = c;
      list[a][b].second = d;
      list[b][a].first = c;
      list[b][a].second = d;
    }
    std::cin >> k;
    for (int i = 0; i < k; i++) {
      for (int j = 0; j <= m; j++)
        cost[j] = INF, used[j] = false;
      int p, q, r;
      std::cin >> p >> q >> r;
      cost[p] = 0;
      std::cout << dijkstra(p, q, r) << std::endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define INF 1000000;

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

int n, m;
int s[100];
std::vector<std::vector<line>> sta(100);

int calc(int i, int j, int r) {
  for (int k = 0; k < sta[i].size(); k++) {
    if (r == 0) {
      if (sta[i][k].cost + s[i] < s[sta[i][k].to]) {
        s[sta[i][k].to] = s[i] + sta[i][k].cost;
        calc(sta[i][k].to, j, r);
      }
    } else if (r == 1) {
      if (sta[i][k].time + s[i] < s[sta[i][k].to]) {
        s[sta[i][k].to] = s[i] + sta[i][k].time;
        calc(sta[i][k].to, j, r);
      }
    }
  }
  return s[j];
}

int main() {
  while (std::cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      line e;
      std::cin >> a >> b >> c >> d;
      e.to = b, e.cost = c, e.time = d;
      sta[a].push_back(e);
      e.to = a;
      sta[b].push_back(e);
    }
    int k;
    std::cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      std::cin >> p >> q >> r;
      for (int j = 0; j <= m; j++)
        s[j] = INF;
      s[p] = 0;
      std::cout << calc(p, q, r) << std::endl;
    }
  }
  return 0;
}
Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define INF (1000000000)
int main() {

  int n, m;
  while (cin >> n >> m && n) {
    int twf[110][110], cwf[110][110];
    for (int i = 0; i < 110; i++) {
      for (int j = 0; j < 110; j++) {
        if (i == j)
          twf[i][j] = cwf[i][j] = 0;
        else
          twf[i][j] = cwf[i][j] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;
      a--;
      b--;
      twf[a][b] = time;
      twf[b][a] = time;
      cwf[a][b] = cost;
      cwf[b][a] = cost;
    }
    for (int a = 0; a < m; a++) {
      for (int b = 0; b < m; b++) {
        for (int c = 0; c < m; c++) {
          twf[b][c] = min(twf[b][c], twf[b][a] + twf[a][c]);
          cwf[b][c] = min(cwf[b][c], cwf[b][a] + cwf[a][c]);
        }
      }
    }

    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--, q--;
      if (!r) {
        cout << cwf[p][q] << endl;
      } else {
        cout << twf[p][q] << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define INF (1000000000)
int main() {

  int n, m;
  while (cin >> n >> m && n) {
    int twf[110][110], cwf[110][110];
    for (int i = 0; i < 110; i++) {
      for (int j = 0; j < 110; j++) {
        if (i == j)
          twf[i][j] = cwf[i][j] = 0;
        else
          twf[i][j] = cwf[i][j] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;
      a--;
      b--;
      twf[a][b] = time;
      twf[b][a] = time;
      cwf[a][b] = cost;
      cwf[b][a] = cost;
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      p--, q--;
      for (int a = 0; a < m; a++) {
        for (int b = 0; b < m; b++) {
          for (int c = 0; c < m; c++) {
            twf[b][c] = min(twf[b][c], twf[b][a] + twf[a][c]);
            cwf[b][c] = min(cwf[b][c], cwf[b][a] + cwf[a][c]);
          }
        }
      }
      if (!r) {
        cout << cwf[p][q] << endl;
      } else {
        cout << twf[p][q] << endl;
      }
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

const int INF = 1 << 29;

int n, m;
int d[2][3000][3000];

int main() {
  while (std::cin >> n >> m, n | m) {
    for (int i = 0; i != n; ++i)
      for (int j = 0; j != n; ++j)
        for (int k = 0; k != 2; ++k)
          d[k][i][j] = i == j ? 0 : INF;

    for (int i = 0; i != n; ++i) {
      int a, b, c, t;
      std::cin >> a >> b >> c >> t;
      d[0][a - 1][b - 1] = d[0][b - 1][a - 1] = c;
      d[1][a - 1][b - 1] = d[1][b - 1][a - 1] = t;
    }

    for (int k = 0; k != m; ++k)
      for (int i = 0; i != m; ++i)
        for (int j = 0; j != m; ++j)
          for (int l = 0; l != 2; ++l)
            d[l][i][j] = std::min(d[l][i][j], d[l][i][k] + d[l][k][j]);

    int k;
    std::cin >> k;
    while (k--) {
      int p, q, r;
      std::cin >> p >> q >> r;

      std::cout << d[r][p - 1][q - 1] << std::endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

// AOJ 0200
#include <algorithm>
#include <iostream>

const int INF = 1 << 29;

int n, m;
int d[2][3000][3000];

int main() {
  while (std::cin >> n >> m, n | m) {
    for (int i = 0; i != n; ++i)
      for (int j = 0; j != n; ++j)
        for (int k = 0; k != 2; ++k)
          d[k][i][j] = i == j ? 0 : INF;

    for (int i = 0; i != n; ++i) {
      int a, b, c, t;
      std::cin >> a >> b >> c >> t;
      d[0][a - 1][b - 1] = d[0][b - 1][a - 1] = c;
      d[1][a - 1][b - 1] = d[1][b - 1][a - 1] = t;
    }

    for (int k = 0; k != n; ++k)
      for (int i = 0; i != n; ++i)
        for (int j = 0; j != n; ++j)
          for (int l = 0; l != 2; ++l)
            d[l][i][j] = std::min(d[l][i][j], d[l][i][k] + d[l][k][j]);

    int k;
    std::cin >> k;
    while (k--) {
      int p, q, r;
      std::cin >> p >> q >> r;

      std::cout << d[r][p - 1][q - 1] << std::endl;
    }
  }

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

#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int dp[105][105][2], n, m;

int main() {
  int l;
  int a, b, c, d;
  int st, en, de;

  while (cin >> n >> m, n || m) {
    rep(i, 105) {
      rep(j, 105) {
        rep(k, 2) { dp[i][j][k] = 2 << 24; }
      }
    }

    rep(i, n) {
      cin >> a >> b >> c >> d;
      dp[a][b][0] = dp[b][a][0] = c;
      dp[a][b][1] = dp[b][a][1] = d;
    }

    rep(k, m + 1) {
      rep(i, m + 1) {
        rep(j, m + 1) {
          dp[i][j][0] = min(dp[i][k][0] + dp[k][j][0], dp[i][j][0]);
          dp[i][j][1] = min(dp[i][k][1] + dp[k][j][1], dp[i][j][1]);
        }
      }
    }

    cin >> l;
    rep(i, l) {
      cin >> st >> en >> de;
      cout << dp[st][en][de] << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int dp[105][105][2], n, m;

int main() {
  int k;
  int a, b, c, d;
  int st, en, de;

  while (cin >> n >> m, n || m) {
    rep(i, 105) rep(j, 105) rep(k, 2) dp[i][j][k] = 2 << 24;
    rep(i, n) {
      cin >> a >> b >> c >> d;
      dp[a][b][0] = dp[b][a][0] = c;
      dp[a][b][1] = dp[b][a][1] = d;
    }
    rep(i, n) {
      rep(j, n) {
        rep(k, n) {
          dp[i][j][0] = min(dp[i][k][0] + dp[k][j][0], dp[i][j][0]);
          dp[i][j][1] = min(dp[i][k][1] + dp[k][j][1], dp[i][j][1]);
        }
      }
    }

    cin >> k;
    rep(i, k) {
      cin >> st >> en >> de;
      cout << dp[st][en][de] << endl;
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: 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 < 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 < N; ++k)
      for (int i = 0; i < N; ++i)
        for (int j = 0; j < N; ++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;
    }
  }
}

Error: 0

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, verdict: 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 < 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;
    }
  }
}

Error: 0

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