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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int n, m, c, s, g, e[21][101][101], un[21][20001];

int solve() {
  int used[101] = {};
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(0, s));
  while (!Q.empty()) {
    int ct = Q.top().first, pos = Q.top().second;
    Q.pop();
    if (used[pos]++)
      continue;
    if (pos == g)
      return ct;
    for (int i = 1; i <= c; i++)
      for (int j = 1; j <= n; j++)
        if (e[i][pos][j] != 1e9)
          Q.push(P(ct + un[i][e[i][pos][j]], j));
  }
  return -1;
}

int main() {
  while (cin >> n >> m >> c >> s >> g, n) {
    for (int i = 1; i <= c; i++)
      for (int j = 1; j <= n; j++)
        for (int k = 1; k <= n; k++)
          e[i][j][k] = 1e9;
    for (int i = 0, x, y, d, k; i < m; i++) {
      cin >> x >> y >> d >> k;
      e[k][x][y] = e[k][y][x] = min(e[k][x][y], d);
    }
    for (int i = 1; i <= c; i++)
      for (int j = 1; j <= n; j++)
        for (int k = 1; k <= n; k++)
          for (int l = 1; l <= n; l++)
            e[i][k][l] = min(e[i][k][j] + e[i][j][l], e[i][k][l]);
    int p[101];
    for (int i = 1; i <= c; i++)
      cin >> p[i];
    for (int i = 1; i <= c; i++) {
      int q[50], r[50];
      q[p[i] - 1] = 1e9;
      for (int j = 0; j < p[i] - 1; j++)
        cin >> q[j];
      for (int j = 0; j < p[i]; j++)
        cin >> r[j];
      int nw = 0;
      for (int j = 0; j < 20000; j++) {
        if (q[nw] <= j)
          nw++;
        un[i][j + 1] = un[i][j] + r[nw];
      }
    }
    cout << solve() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> pp;
int n, m, c, s, g, e[21][101][101], un[21][20001];

int solve() {
  int used[21][101][101] = {};
  priority_queue<pp, vector<pp>, greater<pp>> Q;
  Q.push(pp(P(0, 0), P(s, s)));
  while (!Q.empty()) {
    pp T = Q.top();
    Q.pop();
    int ct = T.first.first, cp = T.first.second, pr = T.second.first,
        pos = T.second.second;
    if (used[cp][pr][pos]++)
      continue;
    if (pos == g)
      return ct;
    for (int i = 1; i <= c; i++)
      for (int j = 1; j <= n; j++)
        if (e[i][pos][j] != 1e9)
          Q.push(pp(P(ct + un[i][e[i][pos][j]], i), P(pos, j)));
  }
  return -1;
}

int main() {
  while (cin >> n >> m >> c >> s >> g, n) {
    for (int i = 1; i <= c; i++)
      for (int j = 1; j <= n; j++)
        for (int k = 1; k <= n; k++)
          e[i][j][k] = 1e9;
    for (int i = 0, x, y, d, k; i < m; i++) {
      cin >> x >> y >> d >> k;
      e[k][x][y] = e[k][y][x] = min(e[k][x][y], d);
    }
    for (int i = 1; i <= c; i++)
      for (int j = 1; j <= n; j++)
        for (int k = 1; k <= n; k++)
          for (int l = 1; l <= n; l++)
            e[i][k][l] = min(e[i][k][j] + e[i][j][l], e[i][k][l]);
    int p[101];
    for (int i = 1; i <= c; i++)
      cin >> p[i];
    for (int i = 1; i <= c; i++) {
      int q[50], r[50];
      q[p[i] - 1] = 1e9;
      for (int j = 0; j < p[i] - 1; j++)
        cin >> q[j];
      for (int j = 0; j < p[i]; j++)
        cin >> r[j];
      int nw = 0;
      for (int j = 0; j < 20000; j++) {
        if (q[nw] <= j)
          nw++;
        un[i][j + 1] = un[i][j] + r[nw];
      }
    }
    cout << solve() << endl;
  }
  return 0;
}