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

#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;

struct edge {
  int from, to, cap, cost, rev;
};

int N, M, C, S, T;
const int INF = 1 << 28;

int main() {
  while (cin >> N >> M >> C >> S >> T, N + M + C + S + T) {
    S--, T--;

    vector<mat> ccost(C, mat(N, vec(N, INF)));
    for (int c = 0; c < C; c++)
      for (int i = 0; i < N; i++)
        ccost[c][i][i] = 0;

    for (int i = 0; i < M; i++) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      x--, y--, c--;
      ccost[c][x][y] = ccost[c][y][x] = min(ccost[c][y][x], d);
    }

    for (auto &c : ccost)
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          for (int k = 0; k < N; k++)
            c[j][k] = min(c[j][k], c[j][i] + c[i][k]);

    mat lvec(C), rvec(C);
    for (int i = 0; i < C; i++) {
      int n;
      cin >> n;
      lvec[i].resize(n - 1);
      rvec[i].resize(n);
    }

    mat dcost(C, vec(100000));
    for (int i = 0; i < C; i++) {
      for (auto &j : lvec[i])
        cin >> j;
      for (auto &j : rvec[i])
        cin >> j;

      for (int j = 0, k = 0; j < 100000; j++) {
        if (k == lvec[i].size())
          dcost[i][j + 1] = dcost[i][j] + rvec[i][k];
        else {
          dcost[i][j + 1] = dcost[i][j] + rvec[i][k];
          if (j + 1 == lvec[i][k])
            k++;
        }
      }
    }

    mat G(N, vec(N, INF));
    for (int c = 0; c < C; c++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          if (ccost[c][i][j] != INF)
            G[i][j] = min(G[i][j], dcost[c][ccost[c][i][j]]);
        }
      }
    }

    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        for (int k = 0; k < N; k++)
          G[j][k] = min(G[j][k], G[j][i] + G[i][k]);

    cout << (G[S][T] == INF ? -1 : G[S][T]) << endl;
  }

  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

struct edge {
  int to, cap, cost, rev, cmp;
};

typedef vector<int> vec;
typedef vector<vec> mat;
typedef vector<edge> gvec;
typedef vector<gvec> gmat;

int N, M, C, S, T;
const int INF = 1 << 28;

int main() {
  while (cin >> N >> M >> C >> S >> T, N || M || C || S || T) {
    gmat G(N + 1);
    vec dist(N + 1);
    vec prevv(N + 1);
    vec preve(N + 1);
    mat cmp_idx(C);
    S--;
    T--;

    vec xvec(M), yvec(M), dvec(M), cvec(M);
    for (int i = 0; i < M; i++) {
      cin >> xvec[i] >> yvec[i] >> dvec[i] >> cvec[i];
      xvec[i]--;
      yvec[i]--;
      cvec[i]--;
      cmp_idx[cvec[i]].push_back(i);
    }

    mat q(C), r(C);
    for (int i = 0; i < C; i++) {
      int p;
      cin >> p;
      q[i].resize(p - 1);
      r[i].resize(p);
    }

    mat c_cost(C, vec(222, 0));

    for (int i = 0; i < C; i++) {
      for (auto &j : q[i])
        cin >> j;
      for (auto &j : r[i])
        cin >> j;

      c_cost[i][0] = 0;

      int l = 0;
      for (int j = 0; j < 222; j++) {
        if (l == q[i].size())
          c_cost[i][j + 1] = c_cost[i][j] + r[i][l];
        else {
          if (q[i][l] > j)
            c_cost[i][j + 1] = c_cost[i][j] + r[i][l];
          if (q[i][l] == j + 1)
            l++;
        }
      }
    }

    auto add_edge = [&](int from, int to, int cap, int cost) {
      G[from].push_back((edge){to, cap, cost, (int)(G[to].size())});
      G[to].push_back((edge){from, cap, cost, (int)(G[from].size()) - 1});
    };

    vector<mat> CC(C);

    for (int i = 0; i < C; i++) {
      CC[i].assign(N + 1, vec(N + 1, INF));

      for (auto j : cmp_idx[i]) {
        CC[i][xvec[j]][yvec[j]] = CC[i][yvec[j]][xvec[j]] = dvec[j];
      }

      for (int j = 0; j < N; j++) {
        for (int k = 0; k < N; k++) {
          for (int l = 0; l < N; l++) {
            CC[i][k][l] = min(CC[i][k][l], CC[i][k][j] + CC[i][j][l]);
          }
        }
      }

      for (int j = 0; j < N; j++) {
        for (int k = j; k < N; k++) {
          if ((int)CC[i][j][k] < INF) {
            add_edge(j, k, 1, c_cost[i][(int)CC[i][j][k]]);
          }
        }
      }
    }

    auto min_cost_flow = [&](int s, int t, int f) {
      int res = 0;

      while (f > 0) {
        dist.assign(N + 1, INF);
        dist[s] = 0;
        bool update = true;

        while (update) {
          update = false;
          for (int v = 0; v < N; v++) {
            if (dist[v] == INF)
              continue;

            for (int i = 0; i < G[v].size(); i++) {
              edge &e = G[v][i];
              if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
                dist[e.to] = dist[v] + e.cost;
                prevv[e.to] = v;
                preve[e.to] = i;
                update = true;
              }
            }
          }
        }

        if (dist[t] == INF)
          return -1;

        int d = f;
        for (int v = t; v != s; v = prevv[v]) {
          d = min(d, G[prevv[v]][preve[v]].cap);
        }

        f -= d;
        res += d * dist[t];
        for (int v = t; v != s; v = prevv[v]) {
          edge &e = G[prevv[v]][preve[v]];
          e.cap -= d;
          G[v][e.rev].cap += d;
        }
      }

      return res;
    };

    cout << min_cost_flow(S, T, 1) << endl;
  }

  return 0;
}