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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#if __GNUC__
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#else
#include <unordered_map>
#include <unordered_set>
#endif

#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) { return __builtin_popcount(n); }
template <> int popcount(int n) { return __builtin_popcount(n); }
template <> int popcount(unsigned long long n) {
  return __builtin_popcountll(n);
}
template <> int popcount(long long n) { return __builtin_popcountll(n); }
#else
#define __typeof__ decltype
template <class T> int popcount(T n) {
  return n ? 1 + popcount(n & (n - 1)) : 0;
}
#endif

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(src))

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "( " << p.first << ", " << p.second << " )";
  return os;
}

template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}

const int mod = 1000000007;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

template <class T>
void print(T a, int n, int br = 1, const string &deli = ", ") {
  cout << "{ ";
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  cout << " }";
  while (br--)
    cout << endl;
}
template <class T>
void print(const vector<T> &v, int br = 1, const string &deli = ", ") {
  print(v, v.size(), br, deli);
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (width != -1)
        cout.width(width);
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  while (br--)
    cout << endl;
}

template <class T> void input(T &a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T, class U> void input(T &a, U &b, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i];
}

int main() {
  ios::sync_with_stdio(false);

  int n, m, c, s, g;
  while (cin >> n >> m >> c >> s >> g, n || m || c || s || g) {
    --s, --g;

    const int INF = 1 << 28;
    static int dis[22][128][128];
    for (int i = 0; i < c; ++i)
      for (int j = 0; j < n; ++j)
        for (int k = 0; k < n; ++k)
          dis[i][j][k] = j == k ? 0 : INF;

    for (int i = 0; i < m; ++i) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      --x, --y, --c;
      min_swap(dis[c][x][y], d);
      min_swap(dis[c][y][x], d);
    }

    int cost[128][128];
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        cost[i][j] = INF;

    {
      int p[22];
      for (int i = 0; i < c; ++i)
        cin >> p[i];
      int q[22][55], r[22][55];
      for (int i = 0; i < c; ++i) {
        for (int j = 0; j < p[i] - 1; ++j)
          cin >> q[i][j];
        q[i][p[i] - 1] = INF;
        for (int j = 0; j < p[i]; ++j)
          cin >> r[i][j];
      }

      for (int C = 0; C < c; ++C) {
        for (int k = 0; k < n; ++k)
          for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
              min_swap(dis[C][i][j], dis[C][i][k] + dis[C][k][j]);

        const int MAX_DIS = 10100 * 222;
        static int val[MAX_DIS];
        val[0] = 0;
        for (int i = 1, seg = 0; i < MAX_DIS; ++i) {
          val[i] = val[i - 1] + r[C][seg];
          if (i == q[C][seg])
            ++seg;
        }

        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j)
            if (dis[C][i][j] != INF)
              min_swap(cost[i][j], val[dis[C][i][j]]);
      }
    }

    static int dp[222];
    for (int i = 0; i < n; ++i)
      dp[i] = INF;

    int res = -1;
    priority_queue<pint, vector<pint>, greater<pint>> q;
    dp[s] = 0;
    q.push(pint(0, s));
    while (!q.empty()) {
      pint tt = q.top();
      q.pop();

      int p = tt.second, w = tt.first;

      if (w > dp[p])
        continue;
      else if (p == g) {
        res = w;
        break;
      }

      for (int i = 0; i < n; ++i) {
        int nw = w + cost[p][i];
        if (nw < dp[i]) {
          dp[i] = nw;
          q.push(pint(nw, i));
        }
      }
    }

    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#if __GNUC__
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#else
#include <unordered_map>
#include <unordered_set>
#endif

#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) { return __builtin_popcount(n); }
template <> int popcount(int n) { return __builtin_popcount(n); }
template <> int popcount(unsigned long long n) {
  return __builtin_popcountll(n);
}
template <> int popcount(long long n) { return __builtin_popcountll(n); }
#else
#define __typeof__ decltype
template <class T> int popcount(T n) {
  return n ? 1 + popcount(n & (n - 1)) : 0;
}
#endif

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(src))

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "( " << p.first << ", " << p.second << " )";
  return os;
}

template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}

const int mod = 1000000007;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

template <class T>
void print(T a, int n, int br = 1, const string &deli = ", ") {
  cout << "{ ";
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  cout << " }";
  while (br--)
    cout << endl;
}
template <class T>
void print(const vector<T> &v, int br = 1, const string &deli = ", ") {
  print(v, v.size(), br, deli);
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (width != -1)
        cout.width(width);
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  while (br--)
    cout << endl;
}

template <class T> void input(T &a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T, class U> void input(T &a, U &b, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i];
}

int main() {
  ios::sync_with_stdio(false);

  int n, m, c, s, g;
  while (cin >> n >> m >> c >> s >> g, n || m || c || s || g) {
    --s, --g;

    const int INF = 1 << 28;
    static int dis[22][128][128];
    for (int i = 0; i < c; ++i)
      for (int j = 0; j < n; ++j)
        for (int k = 0; k < n; ++k)
          dis[i][j][k] = j == k ? 0 : INF;

    for (int i = 0; i < m; ++i) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      --x, --y, --c;
      min_swap(dis[c][x][y], d);
      min_swap(dis[c][y][x], d);
    }

    int p[22];
    for (int i = 0; i < c; ++i)
      cin >> p[i];
    const int MAX_DIS = 10100 * 222;
    static int val[22][MAX_DIS];
    for (int C = 0; C < c; ++C) {
      int q[55], r[55];
      for (int i = 0; i < p[C] - 1; ++i)
        cin >> q[i];
      q[p[C] - 1] = 1e8;

      for (int i = 0; i < p[C]; ++i)
        cin >> r[i];

      val[C][0] = 0;
      for (int i = 1, seg = 0; i < MAX_DIS; ++i) {
        val[C][i] = val[C][i - 1] + r[seg];
        if (i == q[seg])
          ++seg;
      }
    }

    int cost[128][128];
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        cost[i][j] = INF;

    for (int C = 0; C < c; ++C) {
      for (int k = 0; k < n; ++k)
        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j)
            min_swap(dis[C][i][j], dis[C][i][k] + dis[C][k][j]);

      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          if (dis[C][i][j] != INF)
            min_swap(cost[i][j], val[C][dis[C][i][j]]);
    }

    static int dp[222];
    for (int i = 0; i < n; ++i)
      dp[i] = INF;

    int res = -1;
    priority_queue<pint, vector<pint>, greater<pint>> q;
    dp[s] = 0;
    q.push(pint(0, s));
    while (!q.empty()) {
      pint tt = q.top();
      q.pop();

      int p = tt.second, w = tt.first;

      if (w > dp[p])
        continue;
      else if (p == g) {
        res = w;
        break;
      }

      for (int i = 0; i < n; ++i) {
        int nw = w + cost[p][i];
        if (nw < dp[i]) {
          dp[i] = nw;
          q.push(pint(nw, i));
        }
      }
    }

    cout << res << endl;
  }
}