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

#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define each(i, n) for (auto i : n)
#define clr(n) memset(n, 0, sizeof(n))
#define mclr(n) memset(n, -1, sizeof(n))

// 2^8 * 100 * 500 * 24
int INF = 1e9;
int n, m, H, K;
int c[105][105];
int h[105][105];
int r[105][105];

int l[300];
int d[300];
int k[300];
int s, t, p;

int me[(1 << 8) + 1][(1 << 8) + 1];
int ct[105][30];

int solve(int i, int bit, int tg) {
  if (me[i][bit] != -1)
    return me[i][bit];
  if (i == p) {
    if ((bit & tg) == tg) {
      return 0;
    } else {
      return INF;
    }
  }
  return me[i][bit] =
             min(solve(i + 1, bit, tg), solve(i + 1, bit | k[i], tg) + d[i]);
}

vector<vector<int>> ce;
int main() {

  while (cin >> n >> m >> H >> K && n) {
    mclr(c);
    mclr(h);
    mclr(r);
    mclr(l);
    mclr(d);
    clr(k);
    ce.clear();
    ce.resize(n);
    rep(i, m) {
      int a, b, e, f, g;
      cin >> a >> b >> e >> f >> g;
      a--;
      b--;
      g--;
      ce[a].push_back(b);
      ce[b].push_back(a);
      c[a][b] = c[b][a] = e;
      h[a][b] = h[b][a] = f;
      r[a][b] = r[b][a] = g;
    }
    cin >> s >> t >> p;
    s--;
    t--;
    rep(i, p) {
      cin >> l[i] >> d[i];
      rep(j, l[i]) {
        int tmp;
        cin >> tmp;
        tmp--;
        k[i] |= (1 << tmp);
      }
    }
    int ans = INF;
    rep(bit, 1 << K) {
      mclr(me);
      int cost = solve(0, 0, bit);
      if (cost == INF)
        continue;
      rep(i, 105) rep(j, 30) { ct[i][j] = INF; }
      ct[s][0] = cost;
      typedef tuple<int, int, int> T;
      priority_queue<T, vector<T>, greater<T>> qu;
      qu.push(T(cost, s, 0));
      while (qu.size()) {
        int mc = get<0>(qu.top());
        int nw = get<1>(qu.top());
        int tm = get<2>(qu.top());
        qu.pop();
        if (ct[nw][tm] < mc)
          continue;
        each(i, ce[nw]) {
          if (c[nw][i] != -1) {
            int nt = tm + h[nw][i];
            if (nt <= H) {
              int nc = ct[nw][tm];
              if (!(bit & (1 << r[nw][i]))) {
                nc += c[nw][i];
              }
              if (nc < ct[i][nt]) {
                ct[i][nt] = nc;
                qu.push(T(nc, i, nt));
              }
            }
          }
        }
      }
      rep(i, H + 1) { ans = min(ans, ct[t][i]); }
    }
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define each(i, n) for (auto i : n)
#define clr(n) memset(n, 0, sizeof(n))
#define mclr(n) memset(n, -1, sizeof(n))

// 2^8 * 100 * 500 * 24
int INF = 1e9;
int n, m, H, K;
int c[105][105];
int h[105][105];
int r[105][105];

int l[300];
int d[300];
int k[300];
int s, t, p;

int me[(1 << 8) + 1][(1 << 8) + 1];
int ct[105][30];

int solve(int i, int bit, int tg) {
  if (me[i][bit] != -1)
    return me[i][bit];
  if (i == p) {
    if ((bit & tg) == tg) {
      return 0;
    } else {
      return INF;
    }
  }
  return me[i][bit] =
             min(solve(i + 1, bit, tg), solve(i + 1, bit | k[i], tg) + d[i]);
}

vector<vector<int>> ce;
int main() {

  while (cin >> n >> m >> H >> K && n) {
    mclr(c);
    mclr(h);
    mclr(r);
    mclr(l);
    mclr(d);
    clr(k);
    ce.clear();
    ce.resize(n);
    rep(i, m) {
      int a, b, e, f, g;
      cin >> a >> b >> e >> f >> g;
      a--;
      b--;
      g--;
      ce[a].push_back(b);
      ce[b].push_back(a);
      c[a][b] = c[b][a] = e;
      h[a][b] = h[b][a] = f;
      r[a][b] = r[b][a] = g;
    }
    cin >> s >> t >> p;
    s--;
    t--;
    rep(i, p) {
      cin >> l[i] >> d[i];
      rep(j, l[i]) {
        int tmp;
        cin >> tmp;
        tmp--;
        k[i] |= (1 << tmp);
      }
    }
    int ans = INF;
    rep(bit, 1 << K) {
      mclr(me);
      int cost = solve(0, 0, bit);
      if (cost == INF)
        continue;
      rep(i, 105) rep(j, 30) { ct[i][j] = INF; }
      ct[s][0] = cost;
      typedef tuple<int, int, int> T;
      priority_queue<T, vector<T>, greater<T>> qu;
      qu.push(T(cost, s, 0));
      while (qu.size()) {
        int mc = get<0>(qu.top());
        int nw = get<1>(qu.top());
        int tm = get<2>(qu.top());
        qu.pop();
        if (ct[nw][tm] < mc)
          continue;
        each(i, ce[nw]) {
          if (c[nw][i] != -1) {
            int nt = tm + h[nw][i];
            if (nt <= H) {
              int nc = ct[nw][tm];
              if (!(bit & (1 << r[nw][i]))) {
                nc += c[nw][i];
              }
              if (nc <= ct[i][nt]) {
                ct[i][nt] = nc;
                qu.push(T(nc, i, nt));
              }
            }
          }
        }
      }
      rep(i, H + 1) { ans = min(ans, ct[t][i]); }
    }
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}