Problem p01679 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

vector<vector<pair<int, int>>> edges;

int memo[17][1 << 16];
struct aa {
  int now;
  bitset<17> eats;
  int time;
};
class Compare {
public:
  // aaが昇順に並ぶ
  bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};

int main() {
  while (1) {
    for (int i = 0; i < 17; ++i) {
      for (int j = 0; j < (1 << 16); ++j) {
        memo[i][j] = 99999999;
      }
    }
    int N, M, L, S, T;
    cin >> N >> M >> L >> S >> T;
    if (!N)
      break;
    S--;
    edges.clear();
    edges.resize(L + 1);
    int diss[300][300];
    for (int i = 0; i < N; ++i) {

      for (int j = 0; j < N; ++j) {

        diss[i][j] = i == j ? 0 : 99999999;
      }
    }
    for (int i = 0; i < M; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      diss[a][b] = c;
      diss[b][a] = c;
    }

    for (int i = 0; i < N; ++i) {
      for (size_t j = 0; j < N; j++) {
        for (size_t k = 0; k < N; k++) {
          diss[j][k] = min(diss[j][k], diss[j][i] + diss[i][k]);
        }
      }
    }
    vector<pair<int, int>> ramens;
    ramens.push_back(make_pair(S, 0));
    for (int i = 0; i < L; ++i) {
      int j, e;
      cin >> j >> e;
      j--;
      ramens.push_back(make_pair(j, e));
    }
    for (int i = 0; i < L + 1; ++i) {
      for (int j = 0; j < L + 1; ++j) {
        if (i != j) {
          edges[i].push_back(
              make_pair(j, diss[ramens[i].first][ramens[j].first]));
        }
      }
    }

    priority_queue<aa, vector<aa>, Compare> que;
    que.push(aa{0, bitset<17>(1), 0});

    memo[0][0] = 0;
    int ans = 0;
    while (!que.empty()) {
      aa atop(que.top());
      que.pop();
      bitset<17> nbs(atop.eats);
      if (atop.now == 0) {
        ans = max(ans, int(nbs.count()));
        if (ans == L + 1)
          break;
      }

      for (auto j : edges[atop.now]) {
        const int nextplace = j.first;
        const int nexttime = atop.time + j.second;
        if (nexttime < memo[nextplace][nbs.to_ulong() / 2] && nexttime <= T) {
          memo[nextplace][nbs.to_ulong() / 2] = nexttime;
          que.push(aa{nextplace, nbs, nexttime});

          if (!nbs[nextplace]) {
            nbs[nextplace] = true;
            int alltime = nexttime + ramens[nextplace].second;
            if (alltime < memo[nextplace][nbs.to_ulong() / 2] && alltime <= T) {
              memo[nextplace][nbs.to_ulong() / 2] = alltime;
              que.push(aa{nextplace, nbs, alltime});
            }
            nbs[nextplace] = false;
          }
        }
      }
    }
    cout << ans - 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

vector<vector<pair<int, int>>> edges;

int memo[20][1 << 17];
struct aa {
  int now;
  bitset<17> eats;
  int time;
};
class Compare {
public:
  // aaが昇順に並ぶ
  bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};

int main() {
  while (1) {
    for (int i = 0; i < 17; ++i) {
      for (int j = 0; j < (1 << 17); ++j) {
        memo[i][j] = 99999999;
      }
    }
    int N, M, L, S, T;
    cin >> N >> M >> L >> S >> T;
    if (!N)
      break;
    S--;
    edges.clear();
    edges.resize(L + 1);
    int diss[300][300];
    for (int i = 0; i < N; ++i) {

      for (int j = 0; j < N; ++j) {

        diss[i][j] = i == j ? 0 : 99999999;
      }
    }
    for (int i = 0; i < M; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      diss[a][b] = c;
      diss[b][a] = c;
    }

    for (int i = 0; i < N; ++i) {
      for (size_t j = 0; j < N; j++) {
        for (size_t k = 0; k < N; k++) {
          diss[j][k] = min(diss[j][k], diss[j][i] + diss[i][k]);
        }
      }
    }
    vector<pair<int, int>> ramens;
    ramens.push_back(make_pair(S, 0));
    for (int i = 0; i < L; ++i) {
      int j, e;
      cin >> j >> e;
      j--;
      ramens.push_back(make_pair(j, e));
    }
    for (int i = 0; i < L + 1; ++i) {
      for (int j = 0; j < L + 1; ++j) {
        if (i != j) {
          edges[i].push_back(
              make_pair(j, diss[ramens[i].first][ramens[j].first]));
        }
      }
    }

    priority_queue<aa, vector<aa>, Compare> que;
    que.push(aa{0, bitset<17>(1), 0});

    memo[0][1] = 0;
    int ans = 0;
    while (!que.empty()) {
      aa atop(que.top());
      que.pop();
      bitset<17> nbs(atop.eats);
      if (atop.now == 0) {
        ans = max(ans, int(nbs.count()));
        if (ans == L + 1)
          break;
      }

      for (auto j : edges[atop.now]) {
        const int nextplace = j.first;
        const int nexttime = atop.time + j.second;
        if (nexttime < memo[nextplace][nbs.to_ulong()] && nexttime <= T) {
          memo[nextplace][nbs.to_ulong()] = nexttime;
          que.push(aa{nextplace, nbs, nexttime});

          if (!nbs[nextplace]) {
            nbs[nextplace] = true;
            int alltime = nexttime + ramens[nextplace].second;
            if (alltime < memo[nextplace][nbs.to_ulong()] && alltime <= T) {
              memo[nextplace][nbs.to_ulong()] = alltime;
              que.push(aa{nextplace, nbs, alltime});
            }
            nbs[nextplace] = false;
          }
        }
      }
    }
    cout << ans - 1 << endl;
  }
  return 0;
}