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

#include <bits/stdc++.h>
using namespace std;

int n, m, l, s, inf = 1e5 + 1, t, a, c, b, ok;
int g[305][305], G[305][305], re;
int ind[25], cost[25], dp[20][(1 << 18)];

int f(int cur, int vis) {
  if (dp[cur][vis] != -1)
    return dp[cur][vis];
  if ((vis & (~(1 << cur))) == 0)
    return dp[cur][vis] = G[l][cur] + cost[cur];
  dp[cur][vis] = 1e9;
  for (int j = 0; j < l; j++) {
    if ((j != cur) && (((1 << j) & vis) != 0)) {
      dp[cur][vis] = min(dp[cur][vis], f(j, vis & (~(1 << cur))) + G[j][cur]);
    }
  }
  dp[cur][vis] += cost[cur];
  return dp[cur][vis];
}

int main() {
  while (cin >> n >> m >> l >> s >> t) {
    if (n + m + l + s + t == 0)
      break;
    memset(dp, -1, sizeof(dp));
    s--;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        g[i][j] = inf;
      g[i][i] = 0;
    }
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      a--;
      b--;
      g[a][b] = c;
      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][k] + g[k][j], g[i][j]);
      }
    }
    for (int i = 0; i < l; i++) {
      cin >> ind[i] >> cost[i];
      ind[i]--;
    }
    for (int i = 0; i < l; i++) {
      G[i][l] = g[ind[i]][s];
      G[l][i] = g[ind[i]][s];
      for (int j = 0; j < l; j++)
        G[i][j] = g[ind[i]][ind[j]];
    }
    re = 0;
    for (int i = 0; i < (1 << l); i++) {
      int cnt = 0;
      for (int j = 0; j < l; j++)
        if (i & (1 << j))
          cnt++;
      for (int j = 0; j < l; j++) {
        if (i & (1 << j)) {
          if (f(j, i) + G[j][l] <= t)
            re = max(re, cnt);
        }
      }
    }
    cout << re << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define inf 1e9
using namespace std;
typedef pair<int, int> ii;
vector<vector<int>> g;
vector<vector<int>> adj;
vector<ii> v1;
int re, n, m, l, s;

int f(int i1, int num, int t, int xz) {
  if (xz == 0)
    return num;
  else {
    num++;
    t = t - v1[xz].second;
    int i2 = i1;
    i2 |= (1 << xz);
    if (t - adj[xz][0] < 0)
      return 0;
    int hh = f(i2, num, t - adj[xz][0], 0);
    for (int j1 = 1; j1 <= l; j1++) {
      if (!(i2 & (1 << j1))) {
        if (t - adj[xz][j1] >= 0)
          hh = max(f(i2, num, t - adj[xz][j1], j1), hh);
      }
    }
    return hh;
  }
}

int main() {
  int t, x, y, z;
  cin >> n >> m >> l >> s >> t;
  while (n || m || l || s || t) {
    v1.clear();
    g.assign(n + 1, vector<int>(n + 1, inf));
    for (int i = 0; i < m; i++) {
      cin >> x >> y >> z;
      g[x][y] = min(z, g[x][y]);
      g[y][x] = min(z, g[y][x]);
    }
    v1.push_back(ii(s, 0));
    for (int i = 0; i < l; i++) {
      cin >> x >> y;
      v1.push_back(ii(x, y));
    }
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
        }
      }
    }
    adj.clear();
    for (int i = 0; i < v1.size(); i++) {
      vector<int> vv;
      for (int j = 0; j < v1.size(); j++)
        vv.push_back(g[v1[i].first][v1[j].first]);
      adj.push_back(vv);
    }
    re = 0;
    for (int i = 1; i <= l; i++) {
      if (adj[0][i] <= t) {
        re = max(f(1, 0, t - adj[0][i], i), re);
      }
    }
    cout << re << endl;
    cin >> n >> m >> l >> s >> t;
  }
  return 0;
}