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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define INF (1e9)

struct Edge {
  int to, dist, company;
};

void FW(vvi &d, int n) {
  REP(i, n) d[i][i] = 0;
  REP(i, n) REP(j, n) REP(k, n) {
    if (d[j][i] != INF && d[i][k] != INF) {
      d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
    }
  }
}

int main() {
  int station_n, rail_n, company_n, start, goal;
  while (cin >> station_n >> rail_n >> company_n >> start >> goal, station_n) {
    start--;
    goal--;
    vector<vector<Edge>> E(station_n);
    REP(i, rail_n) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      x--;
      y--;
      c--;
      if (x > y)
        swap(x, y);
      bool found = false;
      REP(i, E[x].size()) {
        if (E[x][i].to == y && E[x][i].company == c) {
          if (E[x][i].dist > d) {
            E[x][i] = {y, d, c};
          }
          found = true;
          break;
        }
      }
      if (!found) {
        E[x].push_back({y, d, c});
      }
    }

    vvi cost(company_n);
    vi p(company_n);
    REP(c, company_n) cin >> p[c];
    vvi q(company_n), r(company_n);
    REP(c, company_n) {
      REP(j, p[c] - 1) {
        int d;
        cin >> d;
        q[c].push_back(d);
      }
      REP(j, p[c]) {
        int d;
        cin >> d;
        r[c].push_back(d);
      }
    }
    REP(c, company_n) {
      int v = 0;
      int i = 0, j = 0;
      cost[c].push_back(0);
      if (p[c] > 1) {
        while (i <= q[c][p[c] - 2]) {
          if (i >= q[c][j]) {
            j++;
          }
          v += r[c][j];
          cost[c].push_back(v);
          i++;
        }
      } else {
        cost[c].push_back(r[c][0]);
      }
    }

    vvi V(station_n, vi(station_n, INF));
    REP(c, company_n) {
      vvi W(station_n, vi(station_n, INF));
      REP(i, station_n) {
        for (auto &e : E[i]) {
          if (e.company == c) {
            W[i][e.to] = W[e.to][i] = e.dist;
          }
        }
      }
      // ???????????¢
      FW(W, station_n);

      // ??????
      REP(i, station_n) REP(j, station_n) {
        if (i != j && W[i][j] != INF) {
          int dist = W[i][j];
          int n = cost[c].size();
          if (dist < n) {
            V[i][j] = min(V[i][j], cost[c][dist]);
          } else {
            int d = cost[c][n - 1] - cost[c][n - 2];
            V[i][j] = min(V[i][j], cost[c][n - 1] + d * (dist - n + 1));
          }
        }
      }
    }

    FW(V, station_n);
    cout << (V[start][goal] != INF ? V[start][goal] : -1) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define INF (1e9)

struct Edge {
  int to, dist, company;
};

void FW(vvi &d, int n) {
  REP(i, n) d[i][i] = 0;
  REP(i, n) REP(j, n) REP(k, n) {
    if (d[j][i] != INF && d[i][k] != INF) {
      d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
    }
  }
}

int main() {
  int station_n, rail_n, company_n, start, goal;
  while (cin >> station_n >> rail_n >> company_n >> start >> goal, station_n) {
    start--;
    goal--;
    vector<vector<Edge>> E(station_n);
    REP(i, rail_n) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      x--;
      y--;
      c--;
      bool found = false;
      REP(i, E[x].size()) {
        if (E[x][i].to == y && E[x][i].company == c) {
          if (E[x][i].dist > d) {
            E[x][i] = {y, d, c};
            E[y][i] = {x, d, c};
          }
          found = true;
          break;
        }
      }
      if (!found) {
        E[x].push_back({y, d, c});
        E[y].push_back({x, d, c});
      }
    }

    vvi cost(company_n);
    vi p(company_n);
    REP(c, company_n) cin >> p[c];
    vvi q(company_n), r(company_n);
    REP(c, company_n) {
      REP(j, p[c] - 1) {
        int d;
        cin >> d;
        q[c].push_back(d);
      }
      REP(j, p[c]) {
        int d;
        cin >> d;
        r[c].push_back(d);
      }
    }
    REP(c, company_n) {
      int v = 0;
      int i = 0, j = 0;
      cost[c].push_back(0);
      if (p[c] > 1) {
        while (i <= q[c][p[c] - 2]) {
          if (i >= q[c][j]) {
            j++;
          }
          v += r[c][j];
          cost[c].push_back(v);
          i++;
        }
      } else {
        cost[c].push_back(r[c][0]);
      }
    }

    vvi V(station_n, vi(station_n, INF));
    REP(c, company_n) {
      vvi W(station_n, vi(station_n, INF));
      REP(i, station_n) {
        for (auto &e : E[i]) {
          if (e.company == c) {
            W[i][e.to] = e.dist;
          }
        }
      }
      // ???????????¢
      FW(W, station_n);

      // ??????
      REP(i, station_n) REP(j, station_n) {
        if (i != j && W[i][j] != INF) {
          int dist = W[i][j];
          int n = cost[c].size();
          if (dist < n) {
            V[i][j] = min(V[i][j], cost[c][dist]);
          } else {
            int d = cost[c][n - 1] - cost[c][n - 2];
            V[i][j] = min(V[i][j], cost[c][n - 1] + d * (dist - n + 1));
          }
        }
      }
    }

    FW(V, station_n);
    cout << (V[start][goal] != INF ? V[start][goal] : -1) << endl;
  }
}