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

#include <bits/stdc++.h>
#define N 300
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int G[N][N];
int SIRO[N], idx[N];
int n, m, l, s, T;
vector<int> g;
int dijkstra() {
  vector<vector<int>> D(1 << l, vector<int>(n, T + 1));
  priority_queue<PP, vector<PP>, greater<PP>> Q;
  Q.push(PP(0, P(0, s))), D[0][s] = 0;

  int res = 0;
  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    int cost = t.first;
    int bit = t.second.first;
    int pos = t.second.second;
    if (D[bit][pos] < cost)
      continue;
    if (pos == s)
      res = max(res, __builtin_popcount(bit));

    for (int j = 0; j < l + 1; j++) {
      int i = g[j];
      if (i != s && SIRO[i] == 0)
        continue;
      int ncost = cost + G[pos][i];
      int nbit = bit;
      if (D[nbit][i] > ncost) {
        Q.push(PP(ncost, P(nbit, i)));
        D[nbit][i] = ncost;
      }
      if (i == s || bit >> idx[i] & 1)
        continue;
      nbit |= 1 << idx[i], ncost += +SIRO[i];
      if (D[nbit][i] > ncost) {
        Q.push(PP(ncost, P(nbit, i)));
        D[nbit][i] = ncost;
      }
    }
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m >> l >> s >> T;
    if (!n && !m && !l && !s && !T)
      return 0;
    s--;

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
        G[i][j] = T + 1;
      G[i][i] = 0;
    }

    for (int i = 0, a, b, c; i < m; i++) {
      cin >> a >> b >> c;
      a--, b--;
      G[a][b] = G[b][a] = c;
    }

    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          G[i][j] = min(G[i][j], G[i][k] + G[k][j]);

    memset(SIRO, 0, sizeof(SIRO));
    g.resize(l);
    g.push_back(s);
    for (int i = 0, j, e; i < l; i++) {
      cin >> j >> e, SIRO[j - 1] = e, idx[j - 1] = i, g[i] = j - 1;
    }

    cout << dijkstra() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 300
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int G[N][N];
int SIRO[N], idx[N];
int n, m, l, s, T;

int dijkstra() {
  vector<vector<int>> D(1 << l, vector<int>(n, T + 1));
  priority_queue<PP, vector<PP>, greater<PP>> Q;
  Q.push(PP(0, P(0, s))), D[0][s] = 0;

  int res = 0;
  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    int cost = t.first;
    int bit = t.second.first;
    int pos = t.second.second;
    if (D[bit][pos] < cost)
      continue;
    if (pos == s)
      res = max(res, __builtin_popcount(bit));

    for (int i = 0; i < n; i++) {
      if (i != s && SIRO[i] == 0)
        continue;
      int ncost = cost + G[pos][i];
      int nbit = bit;
      if (D[nbit][i] > ncost) {
        Q.push(PP(ncost, P(nbit, i)));
        D[nbit][i] = ncost;
      }
      if (i == s || bit >> idx[i] & 1)
        continue;
      nbit |= 1 << idx[i], ncost += +SIRO[i];
      if (D[nbit][i] > ncost) {
        Q.push(PP(ncost, P(nbit, i)));
        D[nbit][i] = ncost;
      }
    }
  }
  return res;
}

int main() {
  while (1) {
    cin >> n >> m >> l >> s >> T;
    if (!n && !m && !l && !s && !T)
      return 0;
    s--;

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
        G[i][j] = T + 1;
      G[i][i] = 0;
    }

    for (int i = 0, a, b, c; i < m; i++) {
      cin >> a >> b >> c;
      a--, b--;
      G[a][b] = G[b][a] = c;
    }

    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          G[i][j] = min(G[i][j], G[i][k] + G[k][j]);

    memset(SIRO, 0, sizeof(SIRO));
    for (int i = 0, j, e; i < l; i++)
      cin >> j >> e, SIRO[j - 1] = e, idx[j - 1] = i;
    cout << dijkstra() << endl;
  }
  return 0;
}