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

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;
const int my_inf = 2147483647;
const long long int my_linf = 9223372036854775807;

long long int getfee(const map<long long int, long long int> fee,
                     const int dis) {

  auto it = fee.upper_bound(dis);
  pair<long long int, long long int> l = *prev(it);
  pair<long long int, long long int> r = *it;
  long long int afee =
      ((r.first - dis) * l.second + (dis - l.first) * r.second) /
      (r.first - l.first);
  return afee;
}

vector<vector<vector<long long int>>> edges;
int main() {
  while (1) {
    long long int N, M, C, S, G;
    cin >> N >> M >> C >> S >> G;
    if (!N)
      break;
    S--;
    G--;
    edges = vector<vector<vector<long long int>>>(
        C, vector<vector<long long int>>(
               N, vector<long long int>(N, 9999999))); // 1:compa 2:from

    for (long long int i = 0; i < M; ++i) {
      long long int X, Y, D, c;
      cin >> X >> Y >> D >> c;
      X--;
      Y--;
      c--;
      edges[c][X][Y] = min(edges[c][X][Y], D);
      edges[c][Y][X] = min(edges[c][Y][X], D);
    }
    vector<long long int> ps;
    for (long long int i = 0; i < C; ++i) {
      long long int P;
      cin >> P;
      ps.push_back(P);
    }
    vector<map<long long int, long long int>> fees;
    for (long long int i = 0; i < C; ++i) {
      vector<long long int> qs, rs;
      for (long long int j = 0; j < ps[i] - 1; ++j) {
        long long int Q;
        cin >> Q;
        qs.push_back(Q);
      }
      qs.push_back(20000000);
      for (long long int j = 0; j < ps[i]; ++j) {
        long long int R;
        cin >> R;
        rs.push_back(R);
      }
      map<long long int, long long int> fee;
      fee[0] = 0;
      long long int from = 0;
      long long int sum = 0;
      for (long long int j = 0; j < ps[i]; ++j) {
        fee[qs[j]] = sum + (qs[j] - from) * rs[j];
        sum = fee[qs[j]];
        from = qs[j];
      }
      fees.push_back(fee);
    }
    for (long long int compa = 0; compa < C; ++compa) {
      for (long long int k = 0; k < N; ++k) {
        for (long long int i = 0; i < N; ++i) {
          for (long long int j = 0; j < N; ++j) {
            edges[compa][i][j] = min(edges[compa][i][j],
                                     edges[compa][i][k] + edges[compa][k][j]);
          }
        }
      }
    }
    for (long long int compa = 0; compa < C; ++compa) {
      for (long long int i = 0; i < N; ++i) {
        for (long long int j = 0; j < N; ++j) {
          edges[compa][i][j] = getfee(fees[compa], edges[compa][i][j]);
        }
      }
    }
    for (long long int compa = 0; compa < C; ++compa) {
      for (long long int i = 0; i < N; ++i) {
        for (long long int j = 0; j < N; ++j) {
          edges[0][i][j] = min(edges[compa][i][j], edges[0][i][j]);
        }
      }
    }
    for (long long int k = 0; k < N; ++k) {
      for (long long int i = 0; i < N; ++i) {
        for (long long int j = 0; j < N; ++j) {
          edges[0][i][j] = min(edges[0][i][j], edges[0][i][k] + edges[0][k][j]);
        }
      }
    }
    long long int ans = edges[0][S][G];
    if (ans >= 9999999)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;
const int my_inf = 2147483647;
const long long int my_linf = 9223372036854775807;

long long int getfee(const map<long long int, long long int> fee,
                     const int dis) {

  auto it = fee.upper_bound(dis);
  pair<long long int, long long int> l = *prev(it);
  pair<long long int, long long int> r = *it;
  long long int afee =
      ((r.first - dis) * l.second + (dis - l.first) * r.second) /
      (r.first - l.first);
  return afee;
}

vector<vector<vector<long long int>>> edges;
int main() {
  while (1) {
    long long int N, M, C, S, G;
    cin >> N >> M >> C >> S >> G;
    S--;
    G--;
    edges = vector<vector<vector<long long int>>>(
        C, vector<vector<long long int>>(
               N, vector<long long int>(N, 999999))); // 1:compa 2:from

    for (long long int i = 0; i < M; ++i) {
      long long int X, Y, D, c;
      cin >> X >> Y >> D >> c;
      X--;
      Y--;
      c--;
      edges[c][X][Y] = min(edges[c][X][Y], D);
      edges[c][Y][X] = min(edges[c][Y][X], D);
    }
    vector<long long int> ps;
    for (long long int i = 0; i < C; ++i) {
      long long int P;
      cin >> P;
      ps.push_back(P);
    }
    vector<map<long long int, long long int>> fees;
    for (long long int i = 0; i < C; ++i) {
      vector<long long int> qs, rs;
      for (long long int j = 0; j < ps[i] - 1; ++j) {
        long long int Q;
        cin >> Q;
        qs.push_back(Q);
      }
      qs.push_back(20000000);
      for (long long int j = 0; j < ps[i]; ++j) {
        long long int R;
        cin >> R;
        rs.push_back(R);
      }
      map<long long int, long long int> fee;
      fee[0] = 0;
      long long int from = 0;
      long long int sum = 0;
      for (long long int j = 0; j < ps[i]; ++j) {
        fee[qs[j]] = sum + (qs[j] - from) * rs[j];
        sum = fee[qs[j]];
        from = qs[j];
      }
      fees.push_back(fee);
    }
    for (long long int compa = 0; compa < C; ++compa) {
      for (long long int k = 0; k < N; ++k) {
        for (long long int i = 0; i < N; ++i) {
          for (long long int j = 0; j < N; ++j) {
            edges[compa][i][j] = min(edges[compa][i][j],
                                     edges[compa][i][k] + edges[compa][k][j]);
          }
        }
      }
    }
    for (long long int compa = 0; compa < C; ++compa) {
      for (long long int i = 0; i < N; ++i) {
        for (long long int j = 0; j < N; ++j) {
          edges[compa][i][j] = getfee(fees[compa], edges[compa][i][j]);
        }
      }
    }
    for (long long int compa = 0; compa < C; ++compa) {
      for (long long int i = 0; i < N; ++i) {
        for (long long int j = 0; j < N; ++j) {
          edges[0][i][j] = min(edges[compa][i][j], edges[0][i][j]);
        }
      }
    }
    for (long long int k = 0; k < N; ++k) {
      for (long long int i = 0; i < N; ++i) {
        for (long long int j = 0; j < N; ++j) {
          edges[0][i][j] = min(edges[0][i][j], edges[0][i][k] + edges[0][k][j]);
        }
      }
    }
    long long int ans = edges[0][S][G];
    if (ans >= 999999)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}