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

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef int W;
const int MAXV = 105;
const int MAXE = 10005 * 25 * 2;
const int INF = 1 << 29;

struct St {
  int c, v;
};
bool operator<(const St &s, const St &t) {
  return (s.c != t.c) ? s.c > t.c : s.v < t.v;
}

struct ShortestPath {
  int V, E, edge[MAXE], prev[MAXE], to[MAXE];
  W dist[MAXV], cost[MAXE];
  ShortestPath(int n) : V(n), E(0) { memset(edge, -1, sizeof(edge)); }
  void add_edge(int s, int t, W d) {
    to[E] = t;
    cost[E] = d;
    prev[E] = edge[s];
    edge[s] = E;
    E++;
  }
  W shortest_path(int s) {
    for (int i = 0; i < V; ++i)
      dist[i] = -1;
    dist[s] = 0;
    priority_queue<St> q;
    q.push((St){0, s});
    while (!q.empty()) {
      St s = q.top();
      q.pop();
      for (int e = edge[s.v]; e != -1; e = prev[e]) {
        int w = to[e];
        if (dist[w] == -1 || dist[w] > dist[s.v] + cost[e]) {
          dist[w] = dist[s.v] + cost[e];
          q.push((St){dist[w], w});
        }
      }
    }
  }
};

int dist[25][151][151];
int p[21];
int q[21][51], r[21][51];

int calc(int c, int d) {
  int ret = 0, prev = 0;
  for (int i = 0; i < p[c] - 1 && d > 0; ++i) {
    if (d >= q[c][i]) {
      ret += q[c][i] * r[c][i];
      d -= q[c][i] - prev;
    } else {
      ret += d * r[c][i];
      d = 0;
      break;
    }
  }
  ret += d * r[c][p[c] - 1];
  return ret;
}

int main() {
  int n, m, c, s, g;
  while (cin >> n >> m >> c >> s >> g, n | m | c | s | g) {
    s--, g--;
    for (int d = 0; d < c; ++d)
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
          dist[d][i][j] = INF;
        dist[d][i][i] = 0;
      }
    for (int i = 0; i < m; ++i) {
      int x, y, d, cc;
      cin >> x >> y >> d >> cc;
      cc--, x--, y--;
      dist[cc][x][y] = dist[cc][y][x] = min(dist[cc][x][y], d);
    }
    for (int cc = 0; cc < c; ++cc) {
      for (int k = 0; k < n; ++k)
        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j)
            dist[cc][i][j] =
                min(dist[cc][i][j], dist[cc][i][k] + dist[cc][k][j]);
    }

    for (int i = 0; i < c; ++i)
      cin >> p[i];
    for (int i = 0; i < c; ++i) {
      for (int j = 0; j < p[i] - 1; ++j)
        cin >> q[i][j];
      for (int j = 0; j < p[i]; ++j)
        cin >> r[i][j];
    }
    for (int i = 0; i < c; ++i) {
      for (int j = p[i] - 2; j >= 1; --j)
        q[i][j] -= q[i][j - 1];
    }

    ShortestPath sp(n);
    for (int cc = 0; cc < c; ++cc) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          if (i == j)
            continue;
          if (dist[cc][i][j] >= INF)
            continue;
          sp.add_edge(i, j, calc(cc, dist[cc][i][j]));
          sp.add_edge(j, i, calc(cc, dist[cc][j][i]));
        }
      }
    }
    sp.shortest_path(s);
    cout << sp.dist[g] << endl;
  }
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef int W;
const int MAXV = 10005;
const int MAXE = 200005;
const int INF = 1 << 29;

struct St {
  int c, v;
};
bool operator<(const St &s, const St &t) {
  return (s.c != t.c) ? s.c > t.c : s.v < t.v;
}

struct ShortestPath {
  int V, E, edge[MAXE], prev[MAXE], to[MAXE];
  W dist[MAXV], cost[MAXE];
  ShortestPath(int n) : V(n), E(0) { memset(edge, -1, sizeof(edge)); }
  void add_edge(int s, int t, W d) {
    to[E] = t;
    cost[E] = d;
    prev[E] = edge[s];
    edge[s] = E;
    E++;
  }
  W shortest_path(int s) {
    for (int i = 0; i < V; ++i)
      dist[i] = -1;
    dist[s] = 0;
    priority_queue<St> q;
    q.push((St){0, s});
    while (!q.empty()) {
      St s = q.top();
      q.pop();
      for (int e = edge[s.v]; e != -1; e = prev[e]) {
        int w = to[e];
        if (dist[w] == -1 || dist[w] > dist[s.v] + cost[e]) {
          dist[w] = dist[s.v] + cost[e];
          q.push((St){dist[w], w});
        }
      }
    }
  }
};

int dist[25][151][151];
int p[21];
int q[21][51], r[21][51];

int calc(int c, int d) {
  int ret = 0, prev = 0;
  for (int i = 0; i < p[c] - 1 && d > 0; ++i) {
    if (d >= q[c][i]) {
      ret += q[c][i] * r[c][i];
      d -= q[c][i] - prev;
    } else {
      ret += d * r[c][i];
      d = 0;
      break;
    }
  }
  ret += d * r[c][p[c] - 1];
  return ret;
}

int main() {
  int n, m, c, s, g;
  while (cin >> n >> m >> c >> s >> g, n | m | c | s | g) {
    s--, g--;
    for (int d = 0; d < c; ++d)
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
          dist[d][i][j] = INF;
        dist[d][i][i] = 0;
      }
    for (int i = 0; i < m; ++i) {
      int x, y, d, cc;
      cin >> x >> y >> d >> cc;
      cc--, x--, y--;
      dist[cc][x][y] = dist[cc][y][x] = min(dist[cc][x][y], d);
    }
    for (int cc = 0; cc < c; ++cc) {
      for (int k = 0; k < n; ++k)
        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j)
            dist[cc][i][j] =
                min(dist[cc][i][j], dist[cc][i][k] + dist[cc][k][j]);
    }

    for (int i = 0; i < c; ++i)
      cin >> p[i];
    for (int i = 0; i < c; ++i) {
      for (int j = 0; j < p[i] - 1; ++j)
        cin >> q[i][j];
      for (int j = 0; j < p[i]; ++j)
        cin >> r[i][j];
    }
    for (int i = 0; i < c; ++i) {
      for (int j = p[i] - 2; j >= 1; --j)
        q[i][j] -= q[i][j - 1];
    }

    ShortestPath sp(n);
    for (int cc = 0; cc < c; ++cc) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          if (i == j)
            continue;
          if (dist[cc][i][j] >= INF)
            continue;
          sp.add_edge(i, j, calc(cc, dist[cc][i][j]));
          sp.add_edge(j, i, calc(cc, dist[cc][i][j]));
        }
      }
    }

    sp.shortest_path(s);
    cout << sp.dist[g] << endl;
  }
}