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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

const int MAX_V = 16;
const int MAX_E = (1 << MAX_V);
const int INF = INT_MAX / 3;
const int NONE = -1;

int V, E;
int dist[MAX_V][MAX_V];
int dp
    [MAX_E]
    [MAX_V]; //?????£?????????????????????is_visit??§????????¨???????????????v?????§???????????????

// v: ?????¨????????????is_visit: ?????£??????????????????
int dfs(int v, int is_visit) {
  if (dp[is_visit][v] != NONE)
    return dp[is_visit][v];
  if (is_visit == (1 << V) - 1)
    return dist[v][0];
  dp[is_visit][v] = INF;

  rep(i, V) {
    if (is_visit & (1 << i))
      continue;
    dp[is_visit][v] =
        min(dfs(i, (is_visit | (1 << i))) + dist[v][i], dp[is_visit][v]);
  }
  return dp[is_visit][v];
}

int main() {
  cin >> V >> E;
  fill_n((int *)dist, sizeof(dist) / sizeof(int), INF);
  fill_n((int *)dp, sizeof(dp) / sizeof(int), NONE);
  int s, t, d;
  rep(i, E) {
    cin >> s >> t >> d;
    dist[s][t] = d;
  }

  int is_visit = 0;
  int ans = dfs(0, (is_visit | 1));

  cout << ((ans == INF) ? -1 : ans) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

const int MAX_V = 16;
const int MAX_E = (1 << MAX_V);
const int INF = INT_MAX / 3;

int V, E;
int dist[MAX_V][MAX_V];
int dp
    [MAX_E]
    [MAX_V]; //?????£?????????????????????is_visit??§????????¨???????????????v?????§???????????????

// v: ?????¨????????????is_visit: ?????£??????????????????
int dfs(int v, int is_visit) {
  if (dp[is_visit][v] != INF)
    return dp[is_visit][v];
  if (is_visit == (1 << V) - 1)
    return dist[v][0];

  rep(i, V) {
    if (is_visit & (1 << i))
      continue;
    dp[is_visit][v] =
        min(dfs(i, (is_visit | (1 << i))) + dist[v][i], dp[is_visit][v]);
  }
  return dp[is_visit][v];
}

int main() {
  cin >> V >> E;
  fill_n((int *)dist, sizeof(dist) / sizeof(int), INF);
  fill_n((int *)dp, sizeof(dp) / sizeof(int), INF);
  int s, t, d;
  rep(i, E) {
    cin >> s >> t >> d;
    dist[s][t] = d;
  }

  int is_visit = 0;
  int ans = dfs(0, (is_visit | 1));

  cout << ((ans == INF) ? -1 : ans) << endl;
  return 0;
}