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

#include <iostream>

#define N 101
#define INF 1e+9

using namespace std;
int main(void) {
  int n, m;
  while (cin >> n >> m, n || m) {
    int cost[N][N], times[N][N];
    for (int i = 1; i <= m; ++i)
      for (int j = 1; j <= m; ++j) {
        cost[i][j] = times[i][j] = INF;
      }
    for (int i = 0, a, b, c, t; i < n; ++i) {
      cin >> a >> b >> c >> t;
      cost[a][b] = cost[b][a] = c;
      times[a][b] = times[b][a] = t;
    }
    int k;
    cin >> k;
    for (int i = 0, p, q, r; i < k; ++i) {
      cin >> p >> q >> r;
      if (r) { // time
        int d[N];
        for (int j = 1; j <= m; ++j)
          d[j] = INF;
        d[p] = 0;
        bool f = false;
        for (;;) {
          for (int j = 1; j <= m; ++j) {   // from
            for (int k = 1; k <= m; ++k) { // to
              if (d[j] != INF && d[j] + times[j][k] < d[k]) {
                f = true;
                d[k] = d[j] + times[j][k];
              }
            }
          }
          if (f == false)
            break;
          f = false;
        }
        cout << d[q] << endl;
      } else { // cost
        int d[N];
        for (int j = 1; j <= m; ++j)
          d[j] = INF;
        d[p] = 0;
        bool f = false;
        for (;;) {
          for (int j = 1; j <= m; ++j) {   // from
            for (int k = 1; k <= m; ++k) { // to
              if (d[j] != INF && d[j] + cost[j][k] < d[k]) {
                f = true;
                d[k] = d[j] + cost[j][k];
              }
            }
          }
          if (f == false)
            break;
          f = false;
        }
        cout << d[q] << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

#define N 101
#define INF 1e+9

using namespace std;

int cost[N][N], times[N][N];

int main(void) {
  int n, m;
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) {
        cost[i][j] = times[i][j] = INF;
      }
    for (int i = 0, a, b, c, t; i < n; ++i) {
      cin >> a >> b >> c >> t;
      cost[a][b] = cost[b][a] = c;
      times[a][b] = times[b][a] = t;
    }
    int k;
    cin >> k;
    for (int i = 0, p, q, r; i < k; ++i) {
      cin >> p >> q >> r;
      if (r) { // time
        int d[N];
        for (int j = 0; j < n; ++j)
          d[j] = INF;
        d[p] = 0;
        bool f = false;
        for (;;) {
          for (int j = 0; j < n; ++j) {   // from
            for (int k = 0; k < n; ++k) { // to
              if (d[j] != INF && d[j] + times[j][k] < d[k]) {
                f == true;
                d[k] = d[j] + times[j][k];
              }
            }
          }
          if (f == false)
            break;
          f = false;
        }
        cout << d[q] << endl;
      } else { // cost
        int d[N];
        for (int j = 0; j < n; ++j)
          d[j] = INF;
        d[p] = 0;
        bool f = false;
        for (;;) {
          for (int j = 0; j < n; ++j) {   // from
            for (int k = 0; k < n; ++k) { // to
              if (d[j] != INF && d[j] + cost[j][k] < d[k]) {
                f == true;
                d[k] = d[j] + cost[j][k];
              }
            }
          }
          if (f == false)
            break;
          f = false;
        }
        cout << d[q] << endl;
      }
    }
  }
  return 0;
}