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

#include <bits/stdc++.h>

using namespace std;

namespace {

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}

struct Edge {
  int from, to, cost;
  Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};

int N, M, L, S, T;
vector<vector<Edge>> G;
vector<int> J, E;
bool input() {
  cin >> N >> M >> L >> S >> T;
  if (N == 0)
    return false;
  S--;
  G.clear();
  G.resize(N);
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(Edge(a, b, c));
    G[b].push_back(Edge(b, a, c));
  }
  J.clear();
  J.resize(L);
  E.clear();
  E.resize(L);
  for (int i = 0; i < L; i++) {
    cin >> J[i] >> E[i];
    J[i]--;
  }
  return true;
}

vector<vector<int>> W;
const int INF = 1 << 28;

struct State {
  int v, bit, cost;
  State(int v, int bit, int cost) : v(v), bit(bit), cost(cost) {}
};

void solve() {
  W.clear();
  W.resize(N, vector<int>(N, INF));
  for (int i = 0; i < N; i++)
    W[i][i] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < G[i].size(); j++) {
      const Edge &e = G[i][j];
      W[e.from][e.to] = e.cost;
    }
  }
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        W[i][j] = min(W[i][j], W[i][k] + W[k][j]);
      }
    }
  }
  vector<vector<int>> dp(L, vector<int>(1 << L, INF));
  queue<State> Q;
  for (int i = 0; i < L; i++) {
    Q.push(State(i, 1 << i, W[S][J[i]] + E[i]));
    dp[i][1 << i] = W[S][J[i]] + E[i];
  }
  while (!Q.empty()) {
    State s = Q.front();
    Q.pop();
    for (int n = 0; n < L; n++) {
      if (s.bit & (1 << n))
        continue;
      int nbit = s.bit | (1 << n);
      int ncost = s.cost + W[J[s.v]][J[n]] + E[n];
      if (ncost + W[J[n]][S] > T)
        continue;
      if (dp[n][nbit] > ncost) {
        dp[n][nbit] = ncost;
        Q.push(State(n, nbit, ncost));
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < L; i++) {
    for (int bit = 0; bit < (1 << L); bit++) {
      if (dp[i][bit] + W[J[i]][S] <= T) {
        ans = max(ans, __builtin_popcount(bit));
      }
    }
  }
  cout << ans << endl;
}
} // namespace

int main() {
  while (input())
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

namespace {

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}

struct Edge {
  int from, to, cost;
  Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};

int N, M, L, S, T;
vector<vector<Edge>> G;
vector<int> J, E;
bool input() {
  cin >> N >> M >> L >> S >> T;
  if (N == 0)
    return false;
  S--;
  G.clear();
  G.resize(N);
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(Edge(a, b, c));
    G[b].push_back(Edge(b, a, c));
  }
  J.clear();
  J.resize(L);
  E.clear();
  E.resize(L);
  for (int i = 0; i < L; i++) {
    cin >> J[i] >> E[i];
    J[i]--;
  }
  return true;
}

vector<vector<int>> W;
const int INF = 1 << 28;

struct State {
  int v, bit, cost;
  State(int v, int bit, int cost) : v(v), bit(bit), cost(cost) {}
};

void solve() {
  W.clear();
  W.resize(N, vector<int>(N, INF));
  for (int i = 0; i < N; i++)
    W[i][i] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < G[i].size(); j++) {
      const Edge &e = G[i][j];
      W[e.from][e.to] = e.cost;
    }
  }
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        W[i][j] = min(W[i][j], W[i][k] + W[k][j]);
      }
    }
  }
  vector<vector<int>> dp(L, vector<int>(1 << L, INF));
  queue<State> Q;
  for (int i = 0; i < L; i++) {
    Q.push(State(i, 1 << i, W[S][J[i]] + E[i]));
    dp[i][1 << i] = W[S][J[i]] + E[i];
  }
  while (!Q.empty()) {
    State s = Q.front();
    Q.pop();
    for (int n = 0; n < L; n++) {
      if (s.bit & (1 << n))
        continue;
      int nbit = s.bit | (1 << n);
      int ncost = s.cost + W[J[s.v]][J[n]] + E[n];
      if (dp[n][nbit] > ncost) {
        dp[n][nbit] = ncost;
        Q.push(State(n, nbit, ncost));
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < L; i++) {
    for (int bit = 0; bit < (1 << L); bit++) {
      if (dp[i][bit] + W[J[i]][J[0]] <= T) {
        ans = max(ans, __builtin_popcount(bit));
      }
    }
  }
  cout << ans << endl;
}
} // namespace

int main() {
  while (input())
    solve();
  return 0;
}