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

#include <bits/stdc++.h>
using namespace std;

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000

typedef long long LL;
typedef long double LD;

struct edge {
  int t;
  int h;
  int c;
  int r;
};

int main() {
  while (1) {
    int n, m, h, k;
    cin >> n >> m >> h >> k;
    if (n == 0)
      return 0;
    vector<list<edge>> e(n);
    int a, b, c, hh, r;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c >> hh >> r;
      e[a - 1].push_back({b - 1, hh, c, r - 1});
      e[b - 1].push_back({a - 1, hh, c, r - 1});
    }
    int s, t;
    cin >> s >> t;
    s--, t--;
    int p;
    cin >> p;
    vector<int> val(1 << k, INF);
    val[0] = 0;
    int l, x, d, kk;
    for (int i = 0; i < p; i++) {
      cin >> l >> d;
      x = 0;
      for (int j = 0; j < l; j++) {
        cin >> kk;
        x |= (1 << (kk - 1));
      }
      for (int j = (1 << k) - 1; j >= 0; j--) {
        val[j | x] = min(val[j | x], val[j] + d);
      }
    }
    vector<vector<int>> cost(n, vector<int>(h + 1, INF));
    int ans = INF;
    for (int i = 0; i < (1 << k); i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k <= h; k++)
          cost[j][k] = INF;
      }
      priority_queue<pair<int, pair<int, int>>> dijk;
      dijk.push({0, {s, 0}});
      pair<int, pair<int, int>> now;
      while (!dijk.empty()) {
        now = dijk.top();
        dijk.pop();
        if (cost[now.second.first][now.second.second] == INF) {
          cost[now.second.first][now.second.second] = -now.first;
          for (auto ee : e[now.second.first]) {
            if (now.second.second + ee.h <= h) {
              if (cost[ee.t][now.second.second + ee.h] == INF) {
                if (i & (1 << ee.r)) {
                  dijk.push({now.first, {ee.t, now.second.second + ee.h}});
                } else {
                  dijk.push(
                      {now.first - ee.c, {ee.t, now.second.second + ee.h}});
                }
              }
            }
          }
        }
      }
      for (int j = 0; j <= h; j++) {
        ans = min(ans, val[i] + cost[t][j]);
      }
    }
    if (ans != INF)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000

typedef long long LL;
typedef long double LD;

struct edge {
  int t;
  int h;
  int c;
  int r;
};

int main() {
  while (1) {
    int n, m, h, k;
    cin >> n >> m >> h >> k;
    if (n == 0)
      return 0;
    vector<list<edge>> e(n);
    int a, b, c, hh, r;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c >> hh >> r;
      e[a - 1].push_back({b - 1, hh, c, r - 1});
      e[b - 1].push_back({a - 1, hh, c, r - 1});
    }
    int s, t;
    cin >> s >> t;
    s--, t--;
    int p;
    cin >> p;
    vector<int> val(1 << k, INF);
    val[0] = 0;
    int l, x, d, kk;
    for (int i = 0; i < p; i++) {
      cin >> l >> d;
      x = 0;
      for (int j = 0; j < l; j++) {
        cin >> kk;
        x |= (1 << (kk - 1));
      }
      for (int j = (1 << k) - 1; j >= 0; j--) {
        val[j | x] = min(val[j | x], val[j] + d);
      }
    }
    vector<vector<int>> cost(n, vector<int>(h + 1, INF));
    int ans = INF;
    for (int i = 0; i < (1 << k); i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k <= h; k++)
          cost[j][k] = INF;
      }
      priority_queue<pair<int, pair<int, int>>> dijk;
      dijk.push({0, {s, 0}});
      pair<int, pair<int, int>> now;
      while (!dijk.empty()) {
        now = dijk.top();
        dijk.pop();
        cost[now.second.first][now.second.second] = -now.first;
        for (auto ee : e[now.second.first]) {
          if (now.second.second + ee.h <= h) {
            if (cost[ee.t][now.second.second + ee.h] == INF) {
              if (i & (1 << ee.r)) {
                dijk.push({now.first, {ee.t, now.second.second + ee.h}});
              } else {
                dijk.push({now.first - ee.c, {ee.t, now.second.second + ee.h}});
              }
            }
          }
        }
      }
      for (int j = 0; j <= h; j++) {
        ans = min(ans, val[i] + cost[t][j]);
      }
    }
    if (ans != INF)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}