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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define PUSH(n, v)                                                             \\
  for (int i = 0; i < (n); i++) {                                              \\
    int j;                                                                     \\
    cin >> j;                                                                  \\
    v.push_back(j);                                                            \\
  }
#define ALL(v) v.begin(), v.end()
#define print(s) cout << (s) << endl

const int INF = 2e9;

vector<vector<int>> dp;
vector<vector<int>> dist;
int n;

int rec(int S, int v) {
  if (dp[S][v] >= 0)
    return dp[S][v];
  if (S == (1 << n) - 1 && v == 0)
    return dp[S][v] = 0;

  int res = INF;
  REP(i, n) {
    if (!(S >> i & 1) && dist[v][i] >= 0) {
      // ith vertex is not in S
      // move from v to i
      res = min(res, rec(S | 1 << i, i) + dist[v][i]);
    }
  }
  return dp[S][v] = res;
}

int main() {
  int V, E;
  cin >> V >> E;
  n = V;
  dist = vector<vector<int>>(V, vector<int>(V, -1));
  REP(i, E) {
    int s, t, d;
    cin >> s >> t >> d;
    dist[s][t] = d;
  }

  dp = vector<vector<int>>(1 << V, vector<int>(V + 1, -1));
  // dp[S][j] denotes the minimum distance from jth vertex to 0th vertex passing
  // all unpassed vertices

  int ans = rec(0, 0);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define PUSH(n, v)                                                             \\
  for (int i = 0; i < (n); i++) {                                              \\
    int j;                                                                     \\
    cin >> j;                                                                  \\
    v.push_back(j);                                                            \\
  }
#define ALL(v) v.begin(), v.end()
#define print(s) cout << (s) << endl

const int INF = 2e9;

int rec(int S, int v, vector<vector<int>> dp, vector<vector<int>> dist, int n) {
  if (dp[S][v] >= 0)
    return dp[S][v];
  if (S == (1 << n) - 1 && v == 0)
    return dp[S][v] = 0;

  int res = INF;
  REP(i, n) {
    if (!(S >> i & 1) && dist[v][i] >= 0) {
      // ith vertex is not in S
      // move from v to i
      res = min(res, rec(S | 1 << i, i, dp, dist, n) + dist[v][i]);
    }
  }
  return dp[S][v] = res;
}

int main() {
  int V, E;
  cin >> V >> E;
  vector<vector<int>> dist(V, vector<int>(V, -1));
  REP(i, E) {
    int s, t, d;
    cin >> s >> t >> d;
    dist[s][t] = d;
  }

  vector<vector<int>> dp(1 << V, vector<int>(V + 1, -1));
  // dp[S][j] denotes the minimum distance from jth vertex to 0th vertex passing
  // all unpassed vertices

  int ans = rec(0, 0, dp, dist, V);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}