Problem p00200 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
typedef int i32;
typedef long long i64, ll;
#define BR "\n"

#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;

// config
#define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef MODE_DEBUG
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \
  DUMP(x) << " (L" << __LINE__ << ")"                                          \
          << " " << __FILE__ << endl
#define CHECK(exp, act)                                                        \
  if (exp != act) {                                                            \
    DUMP(exp);                                                                 \
    DEBUG(act);                                                                \
  }
#define STOP(e)                                                                \
  CHECK(e, true);                                                              \
  if (!(e))                                                                    \
    exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

template <typename T, typename V> inline T mod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}

typedef vector<vector<int>> Mat;
void warshall_floyd(Mat &d) {
  const int V = d.size();
  for (int k = 0; k < V; k++)
    for (int i = 0; i < V; i++)
      for (int j = 0; j < V; j++)
        if (d[i][j] > d[i][k] + d[k][j]) {
          d[i][j] = d[i][k] + d[k][j];
          // prev[i][j]=k;
        }
}

const int INF = 1 << 28;
int main() {

  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      return 0;
    Mat ds(M + 1, vector<int>(M + 1, INF));
    Mat cs(M + 1, vector<int>(M + 1, INF));
    REP(i, M + 1) ds[i][i] = cs[i][i] = 0;

    REP(i, N) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      ds[a][b] = min(ds[a][b], t);
      ds[b][a] = min(ds[b][a], t);

      cs[a][b] = min(cs[a][b], c);
      cs[b][a] = min(cs[b][a], c);
    }
    warshall_floyd(ds);
    DUMP(toString(ds));
    warshall_floyd(cs);
    DUMP(toString(cs));
    int k;
    cin >> k;
    REP(i, k) {
      int p, q, r;
      cin >> p >> q >> r;
      if (r == 0)
        cout << cs[p][q] << endl;
      else
        cout << ds[p][q] << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
typedef int i32;
typedef long long i64, ll;
#define BR "\n"

#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;

// config
// #define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef MODE_DEBUG
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \
  DUMP(x) << " (L" << __LINE__ << ")"                                          \
          << " " << __FILE__ << endl
#define CHECK(exp, act)                                                        \
  if (exp != act) {                                                            \
    DUMP(exp);                                                                 \
    DEBUG(act);                                                                \
  }
#define STOP(e)                                                                \
  CHECK(e, true);                                                              \
  if (!(e))                                                                    \
    exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

template <typename T, typename V> inline T mod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}

typedef vector<vector<int>> Mat;
void warshall_floyd(Mat &d) {
  const int V = d.size();
  for (int k = 0; k < V; k++)
    for (int i = 0; i < V; i++)
      for (int j = 0; j < V; j++)
        if (d[i][j] > d[i][k] + d[k][j]) {
          d[i][j] = d[i][k] + d[k][j];
          // prev[i][j]=k;
        }
}

const int INF = 1 << 28;
int main() {

  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      return 0;
    Mat ds(M + 1, vector<int>(M + 1, INF));
    Mat cs(M + 1, vector<int>(M + 1, INF));
    REP(i, M + 1) ds[i][i] = cs[i][i] = 0;

    REP(i, N) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      ds[a][b] = min(ds[a][b], t);
      ds[b][a] = min(ds[b][a], t);

      cs[a][b] = min(cs[a][b], c);
      cs[b][a] = min(cs[b][a], c);
    }
    warshall_floyd(ds);
    DUMP(toString(ds));
    warshall_floyd(cs);
    DUMP(toString(cs));
    int k;
    cin >> k;
    REP(i, k) {
      int p, q, r;
      cin >> p >> q >> r;
      if (r == 0)
        cout << cs[p][q] << endl;
      else
        cout << ds[p][q] << endl;
    }
  }
  return 0;
}