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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define foreach(t, p, tit)                                                     \\
  for (t::iterator tit = p.begin(); tit != p.end(); ++tit)
#define rforeach(t, p)                                                         \\
  for (t::reverse_iterator it = p.rbegin(); it != p.rend(); ++it)
#define all(p) p.begin(), p.end()
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int costs[1010][1010];
int times[1010][1010];
int d[1010];
#define INF 10000
struct E {
  int a, b, c;
  E(){};
  E(int a, int b, int c) : a(a), b(b), c(c){};
};
#define MONEY 0
#define EKI 1

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    memset(costs, INF, sizeof(costs));
    memset(times, INF, sizeof(times));
    vector<E> cs;
    vector<E> ts;
    int a, b, c, t;
    rep(i, n) {
      cin >> a >> b >> c >> t;
      costs[a][b] = c;
      costs[b][a] = c;
      times[a][b] = t;
      times[b][a] = t;
      cs.push_back(E(a, b, c));
      cs.push_back(E(b, a, c));
      ts.push_back(E(a, b, t));
      ts.push_back(E(b, a, t));
    }
    rep(i, m) {
      costs[i][i] = 0;
      times[i][i] = 0;
    }
    int k, p, q, r;
    cin >> k;
    rep(i, k) {
      memset(d, INF, sizeof(d));
      cin >> p >> q >> r;
      d[p] = 0;
      if (r == MONEY) {
        // rep(j,n*2){if(cs[j].a == p) d[cs[j].b] = cs[j].c;}
        while (true) {
          bool update = false;
          rep(j, n * 2) {
            // cout <<"edge"<< j<<cs[j].a<<"to"<<cs[j].b << endl;

            if ((d[cs[j].a] != INF) && d[cs[j].b] > d[cs[j].a] + cs[j].c) {
              d[cs[j].b] = d[cs[j].a] + cs[j].c;
              update = true;
              // cout << "updated" <<d[cs[j].b] <<endl;
            }
          }
          if (!update)
            break;
        }
        cout << d[q] << endl;
      } else {
        // rep(j,n*2){if(ts[j].a == p) d[ts[j].b] = ts[j].c;}
        while (true) {
          bool update = false;
          rep(j, n * 2) {
            if (d[ts[j].a] != INF && d[ts[j].b] > d[ts[j].a] + ts[j].c) {
              d[ts[j].b] = d[ts[j].a] + ts[j].c;
              update = true;
            }
          }
          if (!update)
            break;
        }
        cout << d[q] << endl;
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <vector>
#define INF 10100
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
struct E {
  unsigned int a, b, c;
  E(){};
  E(unsigned int a, unsigned int b, unsigned int c) : a(a), b(b), c(c){};
};
E cs[610];
E ts[610];

unsigned int d[110];
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    int a, b, c, t;
    rep(i, n) {
      cin >> a >> b >> c >> t;
      cs[i * 2] = E(a, b, c);
      cs[i * 2 + 1] = E(b, a, c);

      ts[i * 2] = E(a, b, t);
      ts[i * 2 + 1] = E(b, a, t);
    }
    int p, q, r, k;
    cin >> k;
    rep(i, k) {
      cin >> p >> q >> r;
      memset(d, INF, sizeof(d));
      d[p] = 0;
      bool update = false;
      if (r == 0) {
        while (1) {
          update = false;
          rep(j, n * 2) {
            if ((d[cs[j].b] != INF) && d[cs[j].b] > d[cs[j].a] + cs[j].c) {
              d[cs[j].b] = d[cs[j].a] + cs[j].c;
              update = true;
            }
          }
          if (!update)
            break;
        }
        cout << d[q] << endl;
      } else {
        while (1) {
          update = false;
          rep(j, n * 2) {
            if (d[ts[j].b] != INF && d[ts[j].b] > d[ts[j].a] + ts[j].c) {
              d[ts[j].b] = d[ts[j].a] + ts[j].c;
              update = true;
            }
          }
          if (!update)
            break;
        }
        cout << d[q] << endl;
      }
    }
  }
  return 0;
}