Problem p00763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF (1e9)
#define M 10005
#define N 105
#define L 55
#define C 20
using namespace std;
typedef pair<int, int> P;
int n, m, c, s, g, x, y, id, ic, p[C], q[L], r[L];
int cost1[C][N][N];
int cost2[C][M];
int d[N];

int tocost(int a, int b) {
  if (b < M)
    return cost2[a][b];
  while (1)
    ;
  return cost2[a][M - 1] + (b - M - 1) * (cost2[a][M - 1] - cost2[a][M - 2]);
}

int dijkstra() {
  priority_queue<P, vector<P>, greater<P>> Q;
  for (int i = 0; i < n; i++)
    d[i] = INF;
  d[s] = 0;
  Q.push(P(0, s));
  while (!Q.empty()) {
    P t = Q.top();
    Q.pop();
    int cost = t.first, u = t.second;
    if (u == g)
      return cost;
    if (d[u] < cost)
      continue;
    for (int k = 0; k < c; k++) {
      for (int i = 0; i < n; i++) {
        if (cost1[k][u][i] == INF)
          continue;
        int ncost = cost + tocost(k, cost1[k][u][i]);
        if (d[i] > ncost) {
          d[i] = ncost;
          Q.push(P(ncost, i));
        }
      }
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> n >> m >> c >> s >> g;
    if (!n && !m && !c && !s && !g)
      break;
    s--, g--;
    for (int i = 0; i < C; i++)
      for (int j = 0; j < N; j++)
        for (int k = 0; k < N; k++)
          cost1[i][j][k] = INF;
    for (int i = 0; i < m; i++) {
      cin >> x >> y >> id >> ic;
      x--, y--, ic--;
      cost1[ic][x][y] = cost1[ic][y][x] = min(cost1[ic][x][y], id);
    }
    for (int l = 0; l < c; l++)
      for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
          for (int j = 0; j < n; j++)
            cost1[l][i][j] =
                min(cost1[l][i][j], cost1[l][i][k] + cost1[l][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[j];
      for (int j = 0; j < p[i]; j++)
        cin >> r[j];
      int idx = 0;
      for (int j = 1; j < M; j++) {
        cost2[i][j] = cost2[i][j - 1] + r[idx];
        if (idx < p[i] - 1 && j == q[idx])
          idx++;
      }
    }
    cout << dijkstra() << endl;
    memset(cost2, 0, sizeof(cost2));
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF (1e9)
#define M 10005
#define N 105
#define L 55
#define C 20
using namespace std;
typedef pair<int, int> P;
int n, m, c, s, g, x, y, id, ic, p[C], q[L], r[L];
int cost1[C][N][N];
int cost2[C][M];
int d[N];

int tocost(int a, int b) {
  if (b < M)
    return cost2[a][b];
  return cost2[a][M - 1] + (b - M + 1) * (cost2[a][M - 1] - cost2[a][M - 2]);
}

int dijkstra() {
  priority_queue<P, vector<P>, greater<P>> Q;
  for (int i = 0; i < n; i++)
    d[i] = INF;
  d[s] = 0;
  Q.push(P(0, s));
  while (!Q.empty()) {
    P t = Q.top();
    Q.pop();
    int cost = t.first, u = t.second;
    if (u == g)
      return cost;
    if (d[u] < cost)
      continue;
    for (int k = 0; k < c; k++) {
      for (int i = 0; i < n; i++) {
        if (cost1[k][u][i] == INF)
          continue;
        int ncost = cost + tocost(k, cost1[k][u][i]);
        if (d[i] > ncost) {
          d[i] = ncost;
          Q.push(P(ncost, i));
        }
      }
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> n >> m >> c >> s >> g;
    if (!n && !m && !c && !s && !g)
      break;
    s--, g--;
    for (int i = 0; i < C; i++)
      for (int j = 0; j < N; j++)
        for (int k = 0; k < N; k++)
          cost1[i][j][k] = INF;
    for (int i = 0; i < m; i++) {
      cin >> x >> y >> id >> ic;
      x--, y--, ic--;
      cost1[ic][x][y] = cost1[ic][y][x] = min(cost1[ic][x][y], id);
    }
    for (int l = 0; l < c; l++)
      for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
          for (int j = 0; j < n; j++)
            cost1[l][i][j] =
                min(cost1[l][i][j], cost1[l][i][k] + cost1[l][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[j];
      for (int j = 0; j < p[i]; j++)
        cin >> r[j];
      int idx = 0;
      for (int j = 1; j < M; j++) {
        cost2[i][j] = cost2[i][j - 1] + r[idx];
        if (idx < p[i] - 1 && j == q[idx])
          idx++;
      }
    }
    cout << dijkstra() << endl;
    memset(cost2, 0, sizeof(cost2));
  }
  return 0;
}