Problem p00763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int n, m, C, S, T, p[21], q[21][51], r[21][51];
int G[20][101][101];

void init() {
  for (int i = 0; i < 20; i++)
    for (int j = 0; j < 100; j++)
      for (int k = 0; k < 100; k++)
        G[i][j][k] = 1e9;
}

int calc(int c, int D) {
  int res = 0;
  for (int i = 1; i < p[c] && D; i++) {
    int d = min(D, q[c][i] - q[c][i - 1]);
    D -= d;
    res += d * r[c][i - 1];
  }
  if (D)
    res += D * r[c][p[c] - 1];
  return res;
}

int dijkstra() {
  int D[101];
  for (int i = 0; i < 101; i++)
    D[i] = 1e9;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, S));
  D[S] = 0;
  while (!Q.empty()) {
    P t = Q.top();
    Q.pop();
    int pos = t.second, cost = t.first;
    if (pos == T)
      return cost;
    if (D[pos] < cost)
      continue;

    for (int c = 0; c < C; c++)
      for (int i = 0; i < n; i++) {
        if (G[c][pos][i] == 1e9)
          continue;
        int ncost = cost + calc(c, G[c][pos][i]);
        D[i] = min(D[i], ncost);
        Q.push(P(ncost, i));
      }
  }
  return -1;
}

void WF() {

  for (int c = 0; c < C; c++)
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          G[c][i][j] = min(G[c][i][j], G[c][i][k] + G[c][k][j]);
}

int main() {
  while (1) {
    cin >> n >> m >> C >> S >> T;
    S--, T--;
    if (!n)
      break;

    init();
    for (int i = 0, a, b, d, c; i < m; i++) {
      cin >> a >> b >> d >> c;
      a--, b--, c--;
      G[c][a][b] = G[c][b][a] = min(G[c][a][b], d);
    }

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

    WF();
    cout << dijkstra() << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int n, m, C, S, T, p[21], q[21][51], r[21][51];
int G[20][101][101];

void init() {
  for (int i = 0; i < 20; i++)
    for (int j = 0; j < 100; j++)
      for (int k = 0; k < 100; k++)
        G[i][j][k] = 1e9;
}

int calc(int c, int D) {
  int res = 0;
  for (int i = 1; i < p[c] && D; i++) {
    int d = min(D, q[c][i] - q[c][i - 1]);
    D -= d;
    res += d * r[c][i - 1];
  }
  if (D)
    res += D * r[c][p[c] - 1];
  return res;
}

int dijkstra() {
  int D[101];
  for (int i = 0; i < 101; i++)
    D[i] = 1e9;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, S));
  D[S] = 0;
  while (!Q.empty()) {
    P t = Q.top();
    Q.pop();
    int pos = t.second, cost = t.first;
    if (pos == T)
      return cost;
    if (D[pos] < cost)
      continue;

    for (int c = 0; c < C; c++)
      for (int i = 0; i < n; i++) {
        if (G[c][pos][i] == 1e9)
          continue;
        int ncost = cost + calc(c, G[c][pos][i]);
        if (D[i] > ncost)
          D[i] = ncost, Q.push(P(ncost, i));
      }
  }
  return -1;
}

void WF() {

  for (int c = 0; c < C; c++)
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          G[c][i][j] = min(G[c][i][j], G[c][i][k] + G[c][k][j]);
}

int main() {
  while (1) {
    cin >> n >> m >> C >> S >> T;
    S--, T--;
    if (!n)
      break;

    init();
    for (int i = 0, a, b, d, c; i < m; i++) {
      cin >> a >> b >> d >> c;
      a--, b--, c--;
      G[c][a][b] = G[c][b][a] = min(G[c][a][b], d);
    }

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

    WF();
    cout << dijkstra() << endl;
  }
  return 0;
}