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

#include <bits/stdc++.h>
using namespace std;
int N, M, L, S, T;
#define INF (1 << 28)
struct edge {
  int to, c;
};
struct state {
  int id, s, c;
  bool operator>(const state &st) const { return c > st.c; }
};
vector<edge> G[22];
int W[333][333];
int J[20];
int E[20];
int h[21][(1 << 16)];
void solve() {
  fill(h[0], h[L + 1], T + 1);
  priority_queue<state, vector<state>, greater<state>> q;
  q.push((state){L, 0, 0});
  h[L][0] = 0;
  while (!q.empty()) {
    state st = q.top();
    q.pop();
    int id = st.id;
    int bi = st.s;
    int c = st.c;
    //   cout << id <<  " " << bitset<6>(bi) << " " << c << endl;
    // cout << "JE " << J[id] << " " << E[id] << endl;
    if (id < L && (bi & (1 << id)) == 0) {
      int sbi = (bi | (1 << id));
      if (h[id][sbi] > c + E[id]) {
        h[id][sbi] = c + E[id];
        q.push((state){id, sbi, c + E[id]});
      }
    }
    for (int i = 0; i < (int)G[id].size(); i++) {
      edge &e = G[id][i];
      if (h[e.to][bi] > e.c + c) {
        h[e.to][bi] = e.c + c;
        q.push((state){e.to, bi, e.c + c});
      }
    }
  }
  int res = 0;
  for (int i = 0; i < (1 << L); i++) {
    //    cout << S << " " << bitset<10>(i) << " "<< h[L][i] << endl;
    if (h[L][i] <= T) {
      res = max(res, __builtin_popcount(i));
    }
  }
  cout << res << endl;
}

int main() {
  while (cin >> N >> M >> L >> S >> T && (N || M || L || S || T)) {
    --S;
    fill(W[0], W[N], INF);
    for (int i = 0; i < M; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      --a;
      --b;
      W[a][b] = c;
      W[b][a] = c;
    }
    for (int i = 0; i < N; i++)
      W[i][i] = 0;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        for (int k = 0; k < N; k++)
          W[j][k] = min(W[j][k], W[j][i] + W[i][k]);
    for (int i = 0; i < L; i++) {
      int j, e;
      cin >> j >> e;
      --j;
      J[i] = j;
      E[i] = e;
    }
    J[L] = S;
    for (int i = 0; i <= L; i++) {
      for (int j = i + 1; j <= L; j++) {
        int c = W[J[i]][J[j]];
        if (c < INF) {
          G[i].push_back((edge){j, c});
          G[j].push_back((edge){i, c});
        }
      }
    }

    solve();

    for (int i = 0; i <= L; i++)
      G[i].clear();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, M, L, S, T;
#define INF (1 << 29)
struct edge {
  int to, c;
};
struct state {
  int id, s, c;
  bool operator>(const state &st) const { return c > st.c; }
};
vector<edge> G[333];
edge R[333];

int h[301][(1 << 16)];
void solve() {
  fill(h[0], h[N], T + 1);
  priority_queue<state, vector<state>, greater<state>> q;
  q.push((state){S, 0, 0});
  h[S][0] = 0;
  while (!q.empty()) {
    state st = q.top();
    q.pop();
    int id = st.id;
    int bi = st.s;
    int c = st.c;
    //    cout << id <<  " " << bitset<6>(bi) << " " << c << endl;
    // cout << "R " << R[id].to << " " << R[id].c << endl;
    if (R[id].to != -1 && (bi & (1 << R[id].to)) == 0) {
      int sbi = (bi | (1 << R[id].to));
      if (h[id][sbi] > c + R[id].c) {
        h[id][sbi] = c + R[id].c;
        q.push((state){id, sbi, c + R[id].c});
      }
    }
    for (int i = 0; i < (int)G[id].size(); i++) {
      edge &e = G[id][i];
      if (h[e.to][bi] > e.c + c) {
        h[e.to][bi] = e.c + c;
        q.push((state){e.to, bi, e.c + c});
      }
    }
  }
  int res = 0;
  for (int i = 0; i < (1 << L); i++) {
    // cout << S << " " << bitset<10>(i) << " "<< h[S][i] << endl;
    if (h[S][i] <= T) {
      res = max(res, __builtin_popcount(i));
    }
  }
  cout << res << endl;
}

int main() {
  while (cin >> N >> M >> L >> S >> T && (N || M || L || S || T)) {
    --S;
    for (int i = 0; i < M; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      --a;
      --b;
      G[a].push_back((edge){b, c});
      G[b].push_back((edge){a, c});
    }
    for (int i = 0; i < N; i++)
      R[i] = (edge){-1, -1};
    for (int i = 0; i < L; i++) {
      int j, e;
      cin >> j >> e;
      --j;
      R[j] = (edge){i, e};
    }
    solve();

    for (int i = 0; i < N; i++)
      G[i].clear();
  }
}