Problem p00763 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }

int main() {
  for (int n, m, c, s, t; cin >> n >> m >> c >> s >> t, n | m | c | s | t;) {
    s--;
    t--;
    vector<vvi> dists(c, vvi(n, vi(n, INFTY)));
    rep(i, c) rep(j, n) dists[i][j][j] = 0;
    rep(i, m) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      x--;
      y--;
      c--;
      chmin(dists[c][x][y], d);
      chmin(dists[c][y][x], d);
    }
    vi ps(c);
    rep(i, c) cin >> ps[i];
    vvi sums(c, vi(20000));
    rep(i, c) {
      vi qs(ps[i]), rs(ps[i]);
      rep(j, ps[i] - 1) cin >> qs[j + 1];
      rep(j, ps[i]) cin >> rs[j];
      rep(j, ps[i] - 1) repi(k, qs[j], qs[j + 1]) sums[i][k + 1] =
          sums[i][k] + rs[j];
      repi(k, qs[ps[i] - 1], 20000) sums[i][k + 1] = sums[i][k] + rs[ps[i] - 1];
    }

    for (vvi &dist : dists)
      rep(k, n) rep(i, n) rep(j, n) chmin(dist[i][j], dist[i][k] + dist[k][j]);

    vvi dp(n, vi(n, INFTY));
    rep(i, n) rep(j, n) rep(k, c) if (dists[k][i][j] != INFTY)
        chmin(dp[i][j], sums[k][dists[k][i][j]]);

    rep(k, n) rep(i, n) rep(j, n) chmin(dp[i][j], dp[i][k] + dp[k][j]);

    cout << (dp[s][t] == INFTY ? -1 : dp[s][t]) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

struct Edge {
  int u, v, w, c;
  Edge() {}
  Edge(int u, int v, int w, int c) : u(u), v(v), w(w), c(c) {}
};
typedef vector<vector<Edge>> Graph;

struct State {
  int v, w, c, cw;
  State() {}
  State(int v, int w, int c, int cw) : v(v), w(w), c(c), cw(cw) {}
  bool operator<(const State &s) const { return w < s.w; }
  bool operator>(const State &s) const { return w > s.w; }
};

int main() {
  for (int n, m, c, s, t; cin >> n >> m >> c >> s >> t, n | m | c | s | t;) {
    s--;
    t--;
    vi xs(m), ys(m), ds(m), cs(m);
    rep(i, m) {
      cin >> xs[i] >> ys[i] >> ds[i] >> cs[i];
      xs[i]--;
      ys[i]--;
      cs[i]--;
    }

    vi ps(c);
    rep(i, c) cin >> ps[i];
    vvi qs(c), rs(c);
    rep(i, c) {
      qs[i].resize(ps[i]);
      repi(j, 1, ps[i]) cin >> qs[i][j];
      rs[i].resize(ps[i]);
      rep(j, ps[i]) cin >> rs[i][j];
    }

    vvi sums(c, vi(20000));
    rep(i, c) {
      rep(j, ps[i] - 1) repi(k, qs[i][j], qs[i][j + 1]) sums[i][k + 1] =
          sums[i][k] + rs[i][j];
      repi(k, qs[i][ps[i] - 1], sums[i].size() - 1) sums[i][k + 1] =
          sums[i][k] + rs[i][ps[i] - 1];
    }

    Graph g(n);
    rep(i, m) {
      g[xs[i]].emplace_back(xs[i], ys[i], ds[i], cs[i]);
      g[ys[i]].emplace_back(ys[i], xs[i], ds[i], cs[i]);
    }

    vvvi dist(n, vvi(c, vi(20000, INFTY))); // [駅][鉄道会社][距離]
    priority_queue<State, vector<State>, greater<State>> pq;
    pq.emplace(s, 0, 0, 0);
    while (!pq.empty()) {
      State cur = pq.top();
      pq.pop();
      if (cur.cw >= 20000 || dist[cur.v][cur.c][cur.cw] <= cur.w)
        continue;
      dist[cur.v][cur.c][cur.cw] = cur.w;

      for (Edge e : g[cur.v]) {
        if (e.c == cur.c)
          pq.emplace(e.v, cur.w + sums[e.c][cur.cw + e.w] - sums[e.c][cur.cw],
                     e.c, cur.cw + e.w);
        else
          pq.emplace(e.v, cur.w + sums[e.c][e.w], e.c, e.w);
      }
    }

    int res = INFTY;
    rep(i, c) rep(j, dist[t][i].size()) res = min(res, dist[t][i][j]);
    cout << (res == INFTY ? -1 : res) << endl;
  }

  return 0;
}