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

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int INF = 1 << 29;
int n, c;
int f[20][20001];
int dist[20][100][100];
int G[100][100];
int cost[100];
bool used[100];

typedef pair<int, int> E;

void setcost(int cp, vector<int> x, vector<int> r) {
  f[cp][0] = 0;
  x.push_back(20000);
  int curx = 1;
  for (int i = 0; i < x.size(); ++i) {
    for (int j = curx; j <= x[i]; ++j)
      f[cp][j] = f[cp][j - 1] + r[i];
    curx = x[i] + 1;
  }
}

int dijkstra(int s, int g) {
  priority_queue<E, vector<E>, greater<E>> Q;
  for (int i = 0; i < n; ++i)
    cost[i] = INF, used[i] = false;

  Q.push(E(0, s));
  while (!Q.empty()) {
    E e = Q.top();
    Q.pop();
    int d = e.first;
    int t = e.second;
    if (used[t])
      continue;
    used[t] = true;
    cost[t] = min(cost[t], d);
    for (int i = 0; i < n; ++i) {
      if (cost[i] <= d + G[t][i])
        continue;
      Q.push(E(d + G[t][i], i));
    }
  }
  return cost[g];
}

int main() {
  int m, s, g;
  while (cin >> n >> m >> c >> s >> g, n) {
    s--;
    g--;
    for (int k = 0; k < c; ++k)
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          dist[k][i][j] = INF;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        G[i][j] = INF;

    while (m--) {
      int x, y, d, cp;
      cin >> x >> y >> d >> cp;
      x--;
      y--;
      cp--;
      dist[cp][x][y] = dist[cp][y][x] = min(dist[cp][x][y], d);
    }
    vector<int> crvnum(c);
    for (int i = 0; i < c; ++i)
      cin >> crvnum[i];
    for (int i = 0; i < c; ++i) {
      vector<int> x, r;
      for (int j = 0; j < crvnum[i] - 1; ++j) {
        int a;
        cin >> a;
        x.push_back(a);
      }
      for (int j = 0; j < crvnum[i]; ++j) {
        int a;
        cin >> a;
        r.push_back(a);
      }
      setcost(i, x, r);
    }

    for (int cp = 0; cp < c; ++cp)
      for (int k = 0; k < n; ++k)
        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j)
            dist[cp][i][j] =
                min(dist[cp][i][j], dist[cp][i][k] + dist[cp][k][j]);

    for (int cp = 0; cp < c; ++cp)
      for (int i = 0; i < n; ++i)
        dist[cp][i][i] = 0;

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        for (int cp = 0; cp < c; ++cp)
          if (dist[cp][i][j] <= 20000)
            G[i][j] = min(G[i][j], f[cp][dist[cp][i][j]]);
    int ans = dijkstra(s, g);
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int INF = 1 << 29;
int n, c;
int f[20][20001];
int dist[20][100][100];
int G[100][100];
int cost[100];
bool used[100];

typedef pair<int, int> E;

void setcost(int cp, vector<int> x, vector<int> r) {
  f[cp][0] = 0;
  x.push_back(20000);
  int curx = 1;
  for (int i = 0; i < x.size(); ++i) {
    for (int j = curx; j <= x[i]; ++j)
      f[cp][j] = f[cp][j - 1] + r[i];
    curx = x[i] + 1;
  }
}

int dijkstra(int s, int g) {
  priority_queue<E, vector<E>, greater<E>> Q;
  for (int i = 0; i < n; ++i)
    cost[i] = INF, used[i] = false;

  Q.push(E(0, s));
  while (!Q.empty()) {
    E e = Q.top();
    Q.pop();
    int d = e.first;
    int t = e.second;
    if (used[t])
      continue;
    cost[t] = min(cost[t], d);
    for (int i = 0; i < n; ++i) {
      if (cost[i] <= d + G[t][i])
        continue;
      Q.push(E(d + G[t][i], i));
    }
  }
  return cost[g];
}

int main() {
  int m, s, g;
  while (cin >> n >> m >> c >> s >> g, n) {
    s--;
    g--;
    for (int k = 0; k < c; ++k)
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          dist[k][i][j] = INF;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        G[i][j] = INF;

    while (m--) {
      int x, y, d, cp;
      cin >> x >> y >> d >> cp;
      x--;
      y--;
      cp--;
      dist[cp][x][y] = dist[cp][y][x] = min(dist[cp][x][y], d);
    }
    vector<int> crvnum(c);
    for (int i = 0; i < c; ++i)
      cin >> crvnum[i];
    for (int i = 0; i < c; ++i) {
      vector<int> x, r;
      for (int j = 0; j < crvnum[i] - 1; ++j) {
        int a;
        cin >> a;
        x.push_back(a);
      }
      for (int j = 0; j < crvnum[i]; ++j) {
        int a;
        cin >> a;
        r.push_back(a);
      }
      setcost(i, x, r);
    }

    for (int cp = 0; cp < c; ++cp)
      for (int k = 0; k < n; ++k)
        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j)
            dist[cp][i][j] =
                min(dist[cp][i][j], dist[cp][i][k] + dist[cp][k][j]);

    for (int cp = 0; cp < c; ++cp)
      for (int i = 0; i < n; ++i)
        dist[cp][i][i] = 0;

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        for (int cp = 0; cp < c; ++cp)
          if (dist[cp][i][j] <= 20000)
            G[i][j] = min(G[i][j], f[cp][dist[cp][i][j]]);

    int ans = dijkstra(s, g);
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}