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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#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(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 = 1 << 28;

int V, E;
int G[MAX_V][MAX_V];
int dp[MAX_E][MAX_V];

int dfs(int v, int visit) {
  if (dp[visit][v] != -1)
    return dp[visit][v];
  if (visit == (1 << V) - 1)
    return G[v][0];
  dp[visit][v] = INF;
  rep(i, V) {
    if (visit & (1 << i))
      continue; //??¢????¨????????????????
    dp[visit][v] = min(dfs(i, (visit | (1 << i))) + G[v][i], dp[visit][v]);
  }

  return dp[visit][v];
}

int main() {
  cin >> V >> E;
  fill_n((int *)dp, sizeof(dp) / sizeof(int), -1);
  fill_n((int *)G, sizeof(G) / sizeof(int), INF);
  rep(i, E) {
    int s, t, d;
    cin >> s >> t >> d;
    G[s][t] = d;
  }
  int visit = 0;
  int ans = dfs(0, (visit | 1));

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#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(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 = 1 << 28;

int V, E;
int G[MAX_V][MAX_V];
int dp[MAX_E][MAX_V];

int dfs(int v, int visit) {
  if (dp[visit][v] != INF)
    return dp[visit][v];
  if (visit == (1 << V) - 1)
    return G[v][0];

  rep(i, V) {
    if (visit & (1 << i))
      continue; //??¢????¨????????????????
    dp[visit][v] = min(dfs(i, (visit | (1 << i))) + G[v][i], dp[visit][v]);
  }

  return dp[visit][v];
}

int main() {
  cin >> V >> E;
  fill_n((int *)dp, sizeof(dp) / sizeof(int), INF);
  fill_n((int *)G, sizeof(G) / sizeof(int), INF);
  rep(i, E) {
    int s, t, d;
    cin >> s >> t >> d;
    G[s][t] = d;
  }
  int visit = 0;
  int ans = dfs(0, (visit | 1));

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}