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

#include <bits/stdc++.h>
using namespace std;

class Edge {
public:
  int to, d, c;
};

int N, M, C, s, g;
vector<vector<Edge>> es;
vector<vector<int>> Q, R;
vector<int> p;
vector<vector<int>> fare;
long long distances[101][101];

int getFare(int d, int c) {
  const vector<int> &q = Q[c];
  const vector<int> &r = R[c];

  if (r.size() == 1) {
    return d * r[0];
  }
  // vector<int> q = {3,6};
  // vector<int> r = {10,5,3};
  if (d <= q[0])
    return d * r[0];
  int sum = q[0] * r[0];
  for (int i = 1; i < q.size(); i++) {
    if (d <= q[i]) {
      return sum + r[i] * (d - q[i - 1]);
    } else {
      sum += r[i] * (q[i] - q[i - 1]);
    }
  }
  return sum + r[r.size() - 1] * (d - q[q.size() - 1]);
}
void calcFare() {
  fare = vector<vector<int>>(C, vector<int>(20010));
  for (int i = 0; i < C; i++) {
    for (int d = 1; d < 20010; d++) {
      fare[i][d] = getFare(d, i);
    }
  }
  // cerr << "calc fare end" << endl;
}

typedef pair<int, int> pii;
const long long INF = 1e10;
void bfs(int s, int c) {
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.emplace(0, s);

  long long dist[101];
  for (int i = 0; i < 100; i++)
    dist[i] = INF;
  dist[s] = 0LL;
  while (!pq.empty()) {
    int cost, v;
    tie(cost, v) = pq.top();
    pq.pop();
    for (Edge e : es[v]) {
      if (e.c != c)
        continue;
      int nextCost = e.d + cost;
      if (dist[e.to] <= nextCost)
        continue;
      dist[e.to] = nextCost;
      pq.emplace(nextCost, e.to);
    }
  }
  // cerr << "search end" << endl;

  for (int i = 0; i < N; i++) {
    if (dist[i] == INF)
      continue;
    long long f = fare[c][dist[i]];
    distances[s][i] = min(distances[s][i], f);
    distances[i][s] = min(distances[i][s], f);
  }
}

int main(void) {

  while (true) {
    cin >> N >> M >> C >> s >> g;
    if (N + M + C + s + g == 0)
      return 0;
    s--;
    g--;
    es = vector<vector<Edge>>(N);
    for (int i = 0; i < M; i++) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      x--;
      y--;
      c--;
      es[x].push_back(Edge{y, d, c});
      es[y].push_back(Edge{x, d, c});
    }

    p = vector<int>(C);
    for (int i = 0; i < C; i++)
      cin >> p[i];

    Q = vector<vector<int>>(C);
    R = vector<vector<int>>(C);
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < p[i] - 1; j++) {
        int q;
        cin >> q;
        Q[i].push_back(q);
      }
      for (int j = 0; j < p[i]; j++) {
        int c;
        cin >> c;
        R[i].push_back(c);
      }
    }
    calcFare();
    for (int i = 0; i < C; i++) {
      for (int j = 1; j <= 10; j++) {
        // fprintf(stderr,"%4d ",fare[i][j]);
      }
      // cerr << endl;
    }

    for (int i = 0; i < 101; i++)
      for (int j = 0; j < 101; j++) {
        if (i == j)
          distances[i][j] = 0;
        else
          distances[i][j] = INF;
      }

    for (int i = 0; i < N; i++) {
      for (int c = 0; c < C; c++) {
        bfs(i, c);
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        // cerr << distances[i][j] << " ";
      }
      // cerr << endl;
    }

    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          distances[i][j] =
              min(distances[i][j], distances[i][k] + distances[k][j]);
        }
      }
    }
    if (distances[s][g] == INF)
      cout << -1 << endl;
    else
      cout << distances[s][g] << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

class Edge {
public:
  int to, d, c;
};

int N, M, C, s, g;
vector<vector<Edge>> es;
vector<vector<int>> Q, R;
vector<int> p;
vector<vector<int>> fare;
long long distances[101][101];

int getFare(int d, int c) {
  const vector<int> &q = Q[c];
  const vector<int> &r = R[c];

  if (r.size() == 1) {
    return d * r[0];
  }
  // vector<int> q = {3,6};
  // vector<int> r = {10,5,3};
  if (d <= q[0])
    return d * r[0];
  int sum = q[0] * r[0];
  for (int i = 1; i < q.size(); i++) {
    if (d <= q[i]) {
      return sum + r[i] * (d - q[i - 1]);
    } else {
      sum += r[i] * (q[i] - q[i - 1]);
    }
  }
  return sum + r[r.size() - 1] * (d - q[q.size() - 1]);
}
void calcFare() {
  fare = vector<vector<int>>(C, vector<int>(20010));
  for (int i = 0; i < C; i++) {
    for (int d = 1; d < 20010; d++) {
      fare[i][d] = getFare(d, i);
    }
  }
  // cerr << "calc fare end" << endl;
}

typedef pair<int, int> pii;
const long long INF = 1e10;
void bfs(int s, int c) {
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.emplace(0, s);

  long long dist[101];
  for (int i = 0; i < 100; i++)
    dist[i] = INF;
  dist[s] = 0LL;
  while (!pq.empty()) {
    int cost, v;
    tie(cost, v) = pq.top();
    pq.pop();
    for (Edge e : es[v]) {
      if (e.c != c)
        continue;
      int nextCost = e.d + cost;
      if (dist[e.to] <= nextCost)
        continue;
      dist[e.to] = nextCost;
      pq.emplace(nextCost, e.to);
    }
  }
  // cerr << "search end" << endl;

  for (int i = 0; i < N; i++) {
    if (dist[i] == INF)
      continue;
    long long f = fare[c][dist[i]];
    distances[s][i] = min(distances[s][i], f);
    distances[i][s] = min(distances[i][s], f);
  }
}

int main(void) {

  while (true) {
    cin >> N >> M >> C >> s >> g;
    if (N + M + C + s + g == 0)
      return 0;
    s--;
    g--;
    es = vector<vector<Edge>>(N);
    for (int i = 0; i < M; i++) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      x--;
      y--;
      c--;
      es[x].push_back(Edge{y, d, c});
      es[y].push_back(Edge{x, d, c});
    }

    p = vector<int>(C);
    for (int i = 0; i < C; i++)
      cin >> p[i];

    Q = vector<vector<int>>(C);
    R = vector<vector<int>>(C);
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < p[i] - 1; j++) {
        int q;
        cin >> q;
        Q[i].push_back(q);
      }
      for (int j = 0; j < p[i]; j++) {
        int c;
        cin >> c;
        R[i].push_back(c);
      }
    }
    calcFare();
    for (int i = 0; i < C; i++) {
      for (int j = 1; j <= 10; j++) {
        fprintf(stderr, "%4d ", fare[i][j]);
      }
      cout << endl;
    }

    for (int i = 0; i < 101; i++)
      for (int j = 0; j < 101; j++) {
        if (i == j)
          distances[i][j] = 0;
        else
          distances[i][j] = INF;
      }

    for (int i = 0; i < N; i++) {
      for (int c = 0; c < C; c++) {
        bfs(i, c);
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        cerr << distances[i][j] << " ";
      }
      cerr << endl;
    }

    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          distances[i][j] =
              min(distances[i][j], distances[i][k] + distances[k][j]);
        }
      }
    }
    if (distances[s][g] == INF)
      cout << -1 << endl;
    else
      cout << distances[s][g] << endl;
  }
  return 0;
}