Problem p00763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)

int n, m, c, s, g;

int d_dist[30][110][110];
int d_final[110][110];

int p[30], q[30][60], r[30][60];

const int inf = 1e9;

int getcost(int dist, int *q, int *p) {
  if (dist == inf)
    return inf;
  int res = 0;
  rep(i, 1000000) {
    if (dist <= q[i + 1]) {
      res += (dist - q[i]) * p[i];
      break;
    } else {
      res += (q[i + 1] - q[i]) * p[i];
    }
  }
  return res;
}

signed main() {
  /*
    ????´?????????¢??§???????????¨???WF 1
    ??????????????¨???????????¨????????? 2
    ??????????????§WF 3
   */
  while (cin >> n >> m >> c >> s >> g && n) {
    --s, --g;
    fill(&d_dist[0][0][0], &d_dist[30][110][0], inf);
    fill(&d_final[0][0], &d_final[110][0], inf);
    rep(i, c) rep(j, n) d_dist[i][j][j] = 0;
    rep(i, n) d_final[i][i] = 0;

    rep(i, m) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      --x, --y, --c;
      d_dist[c][x][y] = min(d_dist[c][x][y], d);
      d_dist[c][y][x] = min(d_dist[c][y][x], d);
    }

    rep(i, c) cin >> p[i];
    rep(i, c) {
      q[i][0] = 0;
      rep(j, p[i] - 1) cin >> q[i][j + 1];
      q[i][p[i]] = inf * 2;
      rep(j, p[i]) cin >> r[i][j];
    }

    // rep(i,c){
    //     rep(j,p[i]+10) cout << q[i][j] << ' ';
    //     cout << endl;
    //     rep(j,p[i]+10) cout << r[i][j] << ' ';
    //     cout << endl;
    // }
    // while(1);

    rep(ic, c) rep(k, n) rep(i, n) rep(j, n) {
      d_dist[ic][i][j] =
          min(d_dist[ic][i][j], d_dist[ic][i][k] + d_dist[ic][k][j]);
    }

    // rep(ic,c){
    //     rep(i,n){
    //         rep(j,n) {
    //             cout << d_dist[ic][i][j] << ' ';
    //         }
    //         cout << endl;
    //     }
    // }
    // while(1);

    rep(ic, c) rep(i, n) rep(j, n) {
      d_final[i][j] =
          min(d_final[i][j], getcost(d_dist[ic][i][j], q[ic], r[ic]));
      // cout << getcost(d_dist[ic][i][j], r[ic], q[ic]) << endl;
    }

    // rep(i,n){
    //     rep(j,n) {
    //         cout << d_final[i][j] << ' ';
    //     }
    //     cout << endl;
    // }

    rep(k, n) rep(i, n) rep(j, n) {
      d_final[i][j] = min(d_final[i][j], d_final[i][k] + d_final[k][j]);
    }
    int ans = d_final[s][g];
    cout << (ans == inf ? -1 : ans) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)

int n, m, c, s, g;

int d_dist[30][110][110];
int d_final[110][110];

int p[30], q[30][60], r[30][60];

const int inf = 1e9;

int getcost(int dist, int *q, int *p) {
  if (dist == inf)
    return inf;
  int res = 0;
  rep(i, 1000000) {
    if (dist <= q[i + 1]) {
      res += (dist - q[i]) * p[i];
      break;
    } else {
      res += (q[i + 1] - q[i]) * p[i];
    }
  }
  return res;
}

signed main() {
  /*
    ????´?????????¢??§???????????¨???WF 1
    ??????????????¨???????????¨????????? 2
    ??????????????§WF 3
   */
  while (cin >> n >> m >> c >> s >> g && n) {
    --s, --g;
    rep(ic, c) rep(i, n) rep(j, n) d_dist[ic][i][j] = inf;
    rep(i, n) rep(j, n) d_final[i][j] = inf;
    rep(i, c) rep(j, n) d_dist[i][j][j] = 0;
    rep(i, n) d_final[i][i] = 0;

    rep(i, m) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      --x, --y, --c;
      d_dist[c][x][y] = min(d_dist[c][x][y], d);
      d_dist[c][y][x] = min(d_dist[c][y][x], d);
    }

    rep(i, c) cin >> p[i];
    rep(i, c) {
      q[i][0] = 0;
      rep(j, p[i] - 1) cin >> q[i][j + 1];
      q[i][p[i]] = inf * 2;
      rep(j, p[i]) cin >> r[i][j];
    }

    // rep(i,c){
    //     rep(j,p[i]+10) cout << q[i][j] << ' ';
    //     cout << endl;
    //     rep(j,p[i]+10) cout << r[i][j] << ' ';
    //     cout << endl;
    // }
    // while(1);

    rep(ic, c) rep(k, n) rep(i, n) rep(j, n) {
      d_dist[ic][i][j] =
          min(d_dist[ic][i][j], d_dist[ic][i][k] + d_dist[ic][k][j]);
    }

    // rep(ic,c){
    //     rep(i,n){
    //         rep(j,n) {
    //             cout << d_dist[ic][i][j] << ' ';
    //         }
    //         cout << endl;
    //     }
    // }
    // while(1);

    rep(ic, c) rep(i, n) rep(j, n) {
      d_final[i][j] =
          min(d_final[i][j], getcost(d_dist[ic][i][j], q[ic], r[ic]));
      // cout << getcost(d_dist[ic][i][j], r[ic], q[ic]) << endl;
    }

    // rep(i,n){
    //     rep(j,n) {
    //         cout << d_final[i][j] << ' ';
    //     }
    //     cout << endl;
    // }

    rep(k, n) rep(i, n) rep(j, n) {
      d_final[i][j] = min(d_final[i][j], d_final[i][k] + d_final[k][j]);
    }
    int ans = d_final[s][g];
    cout << (ans == inf ? -1 : ans) << endl;
  }
}