Problem p00763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define inf 1000000000000

long long dp[101][101];
long long dist[101][101][21];

long long solve(int n, int m, int c, int s, int g) {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = inf;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= c; k++) {
        dist[i][j][k] = inf;
        if (i == j) {
          dist[i][j][k] = 0;
        }
      }
    }
  }
  long long x, y, z, w;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z >> w;
    dist[x][y][w] = min(dist[x][y][w], z);
    dist[y][x][w] = min(dist[y][x][w], z);
  }
  for (int q = 1; q <= c; q++) {
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          dist[i][j][q] = min(dist[i][j][q], dist[i][k][q] + dist[k][j][q]);
        }
      }
    }
  }
  vector<int> p(n + 1);
  for (int i = 1; i <= c; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= c; i++) {
    vector<int> q(p[i]);
    for (int j = 1; j < p[i]; j++) {
      cin >> q[j];
    }
    vector<long long> r(p[i] + 1, 0);
    for (int j = 1; j <= p[i]; j++) {
      cin >> r[j];
    }
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        long long dis = dist[j][k][i];
        long long cost = 0;
        for (int l = 1; l <= p[i]; l++) {
          if (l == p[i]) {
            cost += r[l] * (dis - q[l - 1]);
            break;
          }
          if (dis <= q[l]) {
            cost += r[l] * (dis - q[l - 1]);
            break;
          } else {
            cost += r[l] * (q[l] - q[l - 1]);
          }
        }
        dp[j][k] = min(dp[j][k], cost);
      }
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  if (dp[s][g] == inf) {
    dp[s][g] = -1;
  }
  return dp[s][g];
}

int main() {
  int n, m, c, s, g;
  while (1) {
    cin >> n >> m >> c >> s >> g;
    if (n == 0)
      break;
    cout << solve(n, m, c, s, g) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define inf 1000000000000

long long dp[101][101];
long long dist[101][101][21];

long long solve(int n, int m, int c, int s, int g) {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = inf;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= c; k++) {
        dist[i][j][k] = inf;
        if (i == j) {
          dist[i][j][k] = 0;
        }
      }
    }
  }
  long long x, y, z, w;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z >> w;
    dist[x][y][w] = min(dist[x][y][w], z);
    dist[y][x][w] = min(dist[y][x][w], z);
  }
  for (int q = 1; q <= c; q++) {
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          dist[i][j][q] = min(dist[i][j][q], dist[i][k][q] + dist[k][j][q]);
        }
      }
    }
  }
  vector<int> p(c + 1);
  for (int i = 1; i <= c; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= c; i++) {
    vector<int> q(p[i]);
    for (int j = 1; j < p[i]; j++) {
      cin >> q[j];
    }
    vector<long long> r(p[i] + 1, 0);
    for (int j = 1; j <= p[i]; j++) {
      cin >> r[j];
    }
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        long long dis = dist[j][k][i];
        long long cost = 0;
        for (int l = 1; l <= p[i]; l++) {
          if (l == p[i]) {
            cost += r[l] * (dis - q[l - 1]);
            break;
          }
          if (dis <= q[l]) {
            cost += r[l] * (dis - q[l - 1]);
            break;
          } else {
            cost += r[l] * (q[l] - q[l - 1]);
          }
        }
        dp[j][k] = min(dp[j][k], cost);
      }
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  if (dp[s][g] == inf) {
    dp[s][g] = -1;
  }
  return dp[s][g];
}

int main() {
  int n, m, c, s, g;
  while (1) {
    cin >> n >> m >> c >> s >> g;
    if (n == 0)
      break;
    cout << solve(n, m, c, s, g) << endl;
  }
  return 0;
}