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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define IREP(it, array)                                                        \\
  for (vector<int>::iterator it = array.begin(); it != array.end(); ++it)
#define SREP(it, array)                                                        \\
  for (vector<string>::iterator it = array.begin(); it != array.end(); ++it)

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()

const int INF = 1 << 29;
const double EPS = 1e-9;
double zero(double d) { return d < EPS ? 0.0 : d; }

typedef long long LL;
typedef pair<int, int> P;

int main() {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

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

    vector<vector<int>> cost(m + 1, vector<int>(m + 1, INF)),
        time(m + 1, vector<int>(m + 1, INF));

    REP(i, cost.size()) { cost[i][i] = time[i][i] = 0; }

    REP(i, n) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      cost[a][b] = cost[b][a] = c;
      time[a][b] = time[b][a] = t;
    }

    REP(i, cost.size()) {
      REP(j, cost.size()) {
        REP(k, cost.size()) {
          cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k]);
          time[j][k] = min(time[j][k], time[j][i] + time[i][k]);
        }
      }
    }

    int k;
    cin >> k;
    REP(_tmp, k) {
      int p, q, r;
      cin >> p >> q >> r;
      int res = r ? time[p][q] : cost[p][q];
      cout << res << endl;
    }
  }

  return 0;
}

Error: -6
free(): invalid next size (fast)


Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define IREP(it, array)                                                        \\
  for (vector<int>::iterator it = array.begin(); it != array.end(); ++it)
#define SREP(it, array)                                                        \\
  for (vector<string>::iterator it = array.begin(); it != array.end(); ++it)

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()

const int INF = 1 << 29;
const double EPS = 1e-9;
double zero(double d) { return d < EPS ? 0.0 : d; }

typedef long long LL;
typedef pair<int, int> P;

int main() {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int n, m;
  cin >> n >> m;

  vector<vector<int>> cost(m + 1, vector<int>(m + 1, INF)),
      time(m + 1, vector<int>(m + 1, INF));

  REP(i, cost.size()) {
    cost[i][i] = 0;
    time[i][i] = 0;
  }

  REP(i, n) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    cost[a][b] = c;
    cost[b][a] = c;
    time[a][b] = t;
    time[b][a] = t;
  }

  int k;
  cin >> k;
  REP(_tmp, k) {
    int p, q, r;
    cin >> p >> q >> r;

    vector<vector<int>> data(m + 1, vector<int>(m + 1, 0));
    data = r ? time : cost;

    deque<bool> reached(m + 1, 0);
    vector<int> dis(m + 1, INF);
    dis[p] = 0;

    while (true) {
      int minn = -1;
      REP(i, reached.size()) {
        if (!reached[i]) {
          if (minn == -1 || dis[minn] > dis[i]) {
            minn = i;
          }
        }
      }
      if (minn == q)
        break;

      reached[minn] = true;
      REP(i, data[minn].size() + 1) {
        dis[i] = min(dis[minn] + data[minn][i], dis[i]);
      }
    }

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

  return 0;
}