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

#include <algorithm>
#include <cassert>
#include <deque>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef vector<P> VP;
typedef vector<VP> VVP;

int n, m, c;
int mincost[100][1000]; // mincost[a][b] := aにb回チェンジしてきたときの最小値
struct Pox {
  int p;
  int pop_cost;
  int cost;
  priority_queue<int> Q;
  Pox(int p = -1, int cost = -1, int pop_cost = -1,
      priority_queue<int> Q = priority_queue<int>())
      : p(p), cost(cost), pop_cost(pop_cost), Q(Q) {}
};

void Input(VVP &G) {
  for (int i = 0; i < m; i++) {
    int f, t, c;
    cin >> f >> t >> c;
    f--, t--;
    G[f].push_back(P(t, c));
  }
}

int main() {

  while (cin >> n >> m >> c, n | m | c) {
    VVP G(n);

    Input(G);
    rep(i, n) rep(k, m) mincost[i][k] = (1 << 29);
    rep(i, m) mincost[0][i] = 0;
    deque<Pox> deq;
    deq.push_back(Pox(0, 0, 0, priority_queue<int>()));
    int ans = (1 << 29);

    while (!deq.empty()) {
      Pox pox = deq.front();
      deq.pop_front();
      if (pox.pop_cost >= ans)
        continue;
      if (pox.p == n - 1) {
        assert(pox.cost <= c);
        ans = min(ans, pox.pop_cost);
        continue;
      }

      rep(i, G[pox.p].size()) {
        int next = G[pox.p][i].first;
        int ncost = G[pox.p][i].second + pox.cost;
        int pcost = pox.pop_cost;
        priority_queue<int> q = pox.Q;
        q.push(G[pox.p][i].second);
        if (ncost > c) {
          while (ncost > c) {
            assert(!q.empty());
            ncost -= q.top();
            q.pop();
            pcost++;
          }
        }
        if (mincost[next][pcost] > ncost) {
          mincost[next][pcost] = ncost;
          deq.push_back(Pox(next, ncost, pcost, q));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef vector<P> VP;
typedef vector<VP> VVP;

int n, m, c;

struct Pox {
  int p;
  int cost;
  priority_queue<int> Q;
  bitset<100> visited;
  Pox(int p = -1, int cost = -1, priority_queue<int> Q = priority_queue<int>(),
      bitset<100> visited = bitset<100>())
      : p(p), cost(cost), Q(Q), visited(visited) {}
};

void Input(VVP &G) {
  for (int i = 0; i < m; i++) {
    int f, t, c;
    cin >> f >> t >> c;
    f--, t--;
    G[f].push_back(P(t, c));
  }
}

int main() {

  while (cin >> n >> m >> c, n | m | c) {
    VVP G(n);
    map<string, int> mincost;
    Input(G);
    deque<Pox> deq;
    deq.push_back(Pox(0, 0, priority_queue<int>(), bitset<100>()));
    int ans = (1 << 29);

    while (!deq.empty()) {
      Pox pox = deq.front();
      deq.pop_front();
      if (pox.p == n - 1) {
        // assert(pox.cost <= c);
        int pop_count = 0;
        while (pox.cost > c) {
          pop_count++;
          pox.cost -= pox.Q.top();
          pox.Q.pop();
        }
        ans = min(ans, pop_count);
        continue;
      }

      rep(i, G[pox.p].size()) {
        if (pox.visited[G[pox.p][i].first])
          continue;
        int next = G[pox.p][i].first;
        int cost = G[pox.p][i].second;
        priority_queue<int> Q = pox.Q;
        bitset<100> visited = pox.visited;
        visited[next] = true;
        Q.push(cost);
        if (mincost.find(visited.to_string()) == mincost.end()) {
          mincost[visited.to_string()] = pox.cost + cost;
          deq.push_back(Pox(next, pox.cost + cost, Q, visited));
          continue;
        }
        if (mincost[visited.to_string()] > pox.cost + cost) {
          mincost[visited.to_string()] = pox.cost + cost;
          deq.push_back(Pox(next, pox.cost + cost, Q, visited));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}