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

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll n, m, C, s, g, x[10000], y[10000], d[10000], c[10000], p[20], q[20][50],
    r[20][50];
vector<P> e[100][20];
ll ans[100];
priority_queue<P, vector<P>, greater<P>> pq;
ll temp_data[100];
ll INF = 10000000000000LL;

ll get(ll color, ll len) {
  ll ans = 0;
  for (ll i = 0; i < p[color] - 1; i++) {
    if (len <= q[color][i]) {
      return (len - (i == 0 ? 0 : q[color][i - 1])) * r[color][i] + ans;
    }
    ans += (q[color][i] - (i == 0 ? 0 : q[color][i - 1])) * r[color][i];
  }
  return (len - (p[color] == 1 ? 0 : q[color][p[color] - 2])) *
             r[color][p[color] - 1] +
         ans;
}

int main() {
  while (scanf("%lld%lld%lld%lld%lld", &n, &m, &C, &s, &g), n) {
    s--;
    g--;
    for (ll i = 0; i < m; i++) {
      scanf("%lld%lld%lld%lld", &x[i], &y[i], &d[i], &c[i]);
      x[i]--;
      y[i]--;
      c[i]--;
      e[x[i]][c[i]].push_back(P(y[i], d[i]));
      e[y[i]][c[i]].push_back(P(x[i], d[i]));
    }
    for (ll i = 0; i < C; i++)
      scanf("%lld", &p[i]);
    for (ll i = 0; i < C; i++) {
      for (ll j = 0; j < p[i] - 1; j++)
        scanf("%lld", &q[i][j]);
      for (ll j = 0; j < p[i]; j++)
        scanf("%lld", &r[i][j]);
    }
    for (ll i = 0; i < n; i++)
      ans[i] = INF;
    pq.push(P(0, s));
    ans[s] = 0;
    while (!pq.empty()) {
      P pt = pq.top();
      pq.pop();
      ll before = pt.second, cost = pt.first;
      // printf("pq! before: %lld, cost: %lld\\n", before, cost);
      if (ans[before] < cost)
        continue;
      for (ll color = 0; color < C; color++) {
        for (ll i = 0; i < n; i++)
          temp_data[i] = INF;
        temp_data[before] = 0;
        priority_queue<P, vector<P>, greater<P>> que;
        que.push(P(0, before));
        while (!que.empty()) {
          P qt = que.top();
          que.pop();
          ll from = qt.second, len = qt.first, temp = get(color, len) + cost;
          if (len > temp_data[from])
            continue;
          if (ans[from] > temp) {
            ans[from] = temp;
            pq.push(P(temp, from));
          }
          // printf("  que! from: %lld, len: %lld, cost: %lld\\n", from, len,
          // temp);
          for (ll i = 0; i < e[from][color].size(); i++) {
            ll to = e[from][color][i].first,
               len2 = len + e[from][color][i].second;
            if (temp_data[to] <= len2)
              continue;
            temp_data[to] = len2;
            que.push(P(len2, to));
          }
        }
      }
    }
    if (ans[g] == INF)
      ans[g] = -1;
    printf("%lld\\n", ans[g]);
    for (ll i = 0; i < n; i++)
      for (ll j = 0; j < C; j++)
        e[i][j].clear();
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 300000000000;

ll N, M, C, S, g;
ll x[10000], y[10000], D[10000], c[10000];
ll p[20];
ll q[20][50], r[20][50];

ll sum[20][50];

ll money(ll company, ll length) {
  ll u = upper_bound(q[company], q[company] + p[company], length) - q[company];
  u--;
  return sum[company][u] + (length - q[company][u]) * r[company][u];
}

struct edge {
  ll to, length;
};
typedef pair<ll, ll> P;

ll V;
vector<edge> G[100][20];
ll d[100];
priority_queue<P, vector<P>, greater<P>> que;

void dfs(ll from, ll now, ll number, ll length, ll before) {
  for (ll i = 0; i < G[now][number].size(); i++) {
    edge e = G[now][number][i];
    ll m = money(number, length + e.length);
    if (e.to == before)
      continue;
    if (d[e.to] >= d[from] + m) {
      d[e.to] = d[from] + m;
      que.push(P(d[e.to], e.to));
      dfs(from, e.to, number, length + e.length, now);
    }
  }
}

void init() {
  for (ll i = 0; i < 100; i++)
    for (ll j = 0; j < 20; j++)
      G[i][j].clear();
  V = N;
  for (ll i = 0; i < C; i++) {
    sum[i][0] = 0;
    for (ll j = 0; j < p[i] - 1; j++) {
      sum[i][j + 1] = sum[i][j] + (q[i][j + 1] - q[i][j]) * r[i][j];
    }
  }

  for (ll i = 0; i < M; i++) {
    edge e1, e2;
    e1.to = y[i];
    e2.to = x[i];
    e1.length = e2.length = D[i];
    G[x[i]][c[i]].push_back(e1);
    G[y[i]][c[i]].push_back(e2);
  }
}

void dijkstra(ll s) {
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (d[v] < p.first)
      continue;
    for (ll i = 0; i < C; i++) {
      dfs(v, v, i, 0, -1);
    }
  }
}

int main() {
  while (1) {
    scanf("%lld%lld%lld%lld%lld", &N, &M, &C, &S, &g);
    S--;
    g--;
    if (N == 0)
      break;
    for (ll i = 0; i < M; i++)
      scanf("%lld%lld%lld%lld", &x[i], &y[i], &D[i], &c[i]);
    for (ll i = 0; i < M; i++)
      x[i]--, y[i]--, c[i]--;
    for (ll i = 0; i < C; i++)
      scanf("%lld", &p[i]);
    for (ll i = 0; i < C; i++) {
      q[i][0] = 0;
      for (ll j = 0; j < p[i] - 1; j++)
        scanf("%lld", &q[i][j + 1]);
      for (ll j = 0; j < p[i]; j++)
        scanf("%lld", &r[i][j]);
    }
    init();
    dijkstra(S);
    ll ans = d[g];
    if (ans == INF)
      ans = -1;
    printf("%lld\\n", ans);
  }
}