alexjercan/bugnet huggingface dataset:
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>
#define shosu(x) fixed << setprecision(x)
using namespace std;
typedef pair<int, int> P;
const int inf = 1 << 30;
const double pi = acos(-1);

int V, E;
vector<vector<int>> G, dp;

int func(int S, int v) {
  if (dp[S][v] != inf)
    return dp[S][v];
  if (S == (1 << V) - 1 && v == 0)
    return dp[S][v] = 0;
  int tmp = inf - 1;
  for (int i = 0; i < V; i++) {
    if (G[v][i] == inf)
      continue;
    if (!(S >> i & 1))
      tmp = min(tmp, func(S | 1 << i, i) + G[v][i]);
  }
  return dp[S][v] = tmp;
}

int main() {
  cin >> V >> E;
  G = vector<vector<int>>(V, vector<int>(V, inf));
  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    G[s][t] = d;
  }
  dp = vector<vector<int>>(1 << V, vector<int>(V, inf));
  int ans = func(0, 0);
  if (ans == inf - 1)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <vector>
#define shosu(x) fixed << setprecision(x)
using namespace std;
typedef pair<int, int> P;
const int inf = 1 << 30;
const double pi = acos(-1);

int V, E;
vector<vector<int>> G, dp;

int func(int S, int v) {
  if (dp[S][v] != inf && dp[S][v] >= 0)
    return dp[S][v];
  if (S == (1 << V) - 1 && v == 0)
    return dp[S][v] = 0;
  int tmp = inf;
  for (int i = 0; i < V; i++) {
    if (G[v][i] == inf)
      continue;
    if (!(S >> i & 1))
      tmp = min(tmp, func(S | 1 << i, i) + G[v][i]);
  }
  return dp[S][v] = tmp;
}

int main() {
  cin >> V >> E;
  G = vector<vector<int>>(V, vector<int>(V, inf));
  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    G[s][t] = d;
  }
  dp = vector<vector<int>>(1 << V, vector<int>(V, inf));
  int ans = func(0, 0);
  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
        f(v, v', k):- min f(v, v',k - 1) + e(v, v'),

*/
#include <bits/stdc++.h>

using namespace std;

const int INF = (1 << 30) - 1;
const int mxv = 16;
int V, E;
int adj[mxv][mxv], dp[(1 << mxv)][mxv], a, b, c;

int tsp(int v, int bitmask) {

  if (bitmask == (1 << V) - 1)
    return adj[v][0];

  if (dp[bitmask][v] < INF)
    return dp[bitmask][v];

  for (int i = 0; i < V; ++i)
    if (!(bitmask & (1 << i)))
      dp[bitmask][v] =
          min(dp[bitmask][v], adj[v][i] + tsp(i, bitmask | (1 << i)));
  return dp[bitmask][v];
}

int main(void) {

  // freopen("input.in", "r", stdin);
  scanf("%d%d", &V, &E);

  for (int i = 0; i < V; ++i)
    for (int j = 0; j < V; ++j)
      adj[i][j] = INF;

  for (int i = 0; i < (1 << V); ++i)
    for (int j = 0; j < V; ++j)
      dp[i][j] = INF;

  for (int i = 0; i < E; ++i)
    scanf("%d%d%d", &a, &b, &c), adj[a][b] = c;

  dp[1][0] = 0;
  for (int i = 1; i < (1 << V); ++i)
    for (int j = 0; j < V; ++j) {
      if (!(i & (1 << j)))
        continue;
      for (int k = 0; k < V; ++k) {
        if (i & (1 << k))
          continue;
        dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + adj[j][k]);
      }
    }

  int solve = INF;
  for (int i = 1; i < V; ++i)
    solve = min(solve, dp[(1 << V) - 1][i] + adj[i][0]);

  printf("%d\\n", (solve == INF) ? -1 : solve);

  return 0;
}

Error: TLE

Fixed submission:

/*
        f(v, v', k):- min f(v, v',k - 1) + e(v, v'),

*/
#include <bits/stdc++.h>

using namespace std;

const int INF = (1 << 30) - 1;
const int mxv = 16;
int V, E;
int adj[mxv][mxv], dp[(1 << mxv)][mxv], a, b, c;

int tsp(int v, int bitmask) {

  if (bitmask == (1 << V) - 1)
    return adj[v][0];

  if (dp[bitmask][v] < INF)
    return dp[bitmask][v];

  for (int i = 0; i < V; ++i)
    if (!(bitmask & (1 << i)))
      dp[bitmask][v] =
          min(dp[bitmask][v], adj[v][i] + tsp(i, bitmask | (1 << i)));
  return dp[bitmask][v];
}

int main(void) {

  // freopen("input.in", "r", stdin);
  scanf("%d%d", &V, &E);

  for (int i = 0; i < V; ++i)
    for (int j = 0; j < V; ++j)
      adj[i][j] = INF;

  for (int i = 0; i < (1 << V); ++i)
    for (int j = 0; j < V; ++j)
      dp[i][j] = INF;

  for (int i = 0; i < E; ++i)
    scanf("%d%d%d", &a, &b, &c), adj[a][b] = c;

  int sol = tsp(0, 1);
  printf("%d\\n", (sol == INF) ? -1 : sol);

  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int V, E;
int map[15][15];
int dp[1 << 15][15];

int saiki(int vis, int pos) {

  int r = 1 << 28;
  if (dp[vis][pos] != -1)
    return dp[vis][pos];
  if (pos == 0 && vis == (1 << V) - 1) {
    return 0;
  }
  for (int i = 0; i < V; i++) {
    if (map[pos][i] != -1 && !(vis & (1 << i))) {
      r = min(r, saiki(vis | (1 << i), i) + map[pos][i]);
    }
  }

  return dp[vis][pos] = r;
}
int main() {

  int s, t;
  cin >> V >> E;
  fill(&map[0][0], &map[15][0], -1);
  for (int i = 0; i < E; i++) {
    cin >> s >> t;
    cin >> map[s][t];
  }
  fill(&dp[0][0], &dp[1 << 15][0], -1);

  int res = saiki(0, 0);

  if (res >= 1 << 28)
    res = -1;
  cout << res << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct ed {
  short t;
  short pos;
  vector<bool> exp;
};

int main() {

  short V, E;
  bool flag = false, tflag = false;
  queue<struct ed> st;
  vector<short> ex;
  cin >> V >> E;
  vector<vector<short>> v(V, vector<short>(V));
  vector<short> temp(V);
  fill(temp.begin(), temp.end(), -1);
  fill(v.begin(), v.end(), temp);
  short s, t;
  for (short i = 0; i < E; i++) {
    cin >> s >> t;
    cin >> v[s][t];
  }
  struct ed e;
  for (short i = 0; i < V; i++) {
    e.exp.push_back(0);
  }
  short pre, res = 1 << 13;
  e.t = 0;
  e.pos = 0;
  st.push(e);
  while (!st.empty()) {
    tflag = false;
    e = st.front();
    st.pop();
    for (short i = 0; i < V; i++) {
      if (v[e.pos][i] != -1 && !e.exp[i]) {
        e.t += v[e.pos][i];
        e.exp[i] = true;
        pre = e.pos;
        e.pos = i;
        if (i == 0) {
          tflag = true;
          for (short j = 0; j < V; j++) {
            // cout <<"exp = "<<e.exp[j]<<endl;
            if (e.exp[j] == 0) {
              tflag = false;
            }
          }
        }
        if (tflag) {
          res = min(res, e.t);
          flag = true;
        } else {
          st.push(e);
        }
        e.pos = pre;
        e.exp[i] = false;
        e.t -= v[e.pos][i];
      }
    }
  }

  if (!flag)
    res = -1;
  cout << res << endl;

  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define inf (pow(10, 9))
typedef pair<int, int> pii;

int V = 0;
int E = 0;
vector<pii> graph[20]; // from,{to,cost}
int dp[1 << 15][15] = {0};

int DP(int bit, int v) {
  if (dp[bit][v] != inf)
    return dp[bit][v];

  if (bit == (1 << V) - 1 && v == 0)
    return dp[bit][v] = 0;
  if (bit == (1 << V))
    return dp[bit][v] = inf;

  int res = inf - 1;
  for (int i = 0; i < graph[v].size(); i++) {
    pii cmp = graph[v][i];
    if (!((bit >> cmp.first) & 1)) {
      res = min(res, DP(bit | (1 << cmp.first), cmp.first) + cmp.second);
    }
  }

  return dp[bit][v] = res;
}

int solve() {
  for (int i = 0; i < (1 << V); i++) {
    for (int j = 0; j < V; j++) {
      dp[i][j] = inf;
    }
  }
  return DP(0, 0);
}

int main() {
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    graph[s].push_back(pii(t, d));
  }
  if (solve() < (inf - 1))
    printf("%d\\n", solve());
  else
    puts("-1");
  return 0;
}

Error: TLE

Fixed submission:

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

#define inf (pow(10, 9))
typedef pair<int, int> pii;

int V = 0;
int E = 0;
vector<pii> graph[20]; // from,{to,cost}
int dp[1 << 15][15] = {0};

int DP(int bit, int v) {
  if (dp[bit][v] != inf)
    return dp[bit][v];

  if (bit == (1 << V) - 1 && v == 0)
    return 0;

  int res = inf;
  for (int i = 0; i < graph[v].size(); i++) {
    pii cmp = graph[v][i];
    if (!((bit >> cmp.first) & 1)) {
      res = min(res, DP(bit | (1 << cmp.first), cmp.first) + cmp.second);
    }
  }

  return dp[bit][v] = res;
}

int solve() {
  for (int i = 0; i < (1 << V); i++) {
    for (int j = 0; j < V; j++) {
      dp[i][j] = inf;
    }
  }
  return DP(0, 0);
}

int main() {
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    graph[s].push_back(pii(t, d));
  }
  if (solve() != inf)
    printf("%d\\n", solve());
  else
    puts("-1");
  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 200005
#define MAX_N 16
#define INF 10000005

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int V, E;
ll K;
int d[MAX_N][MAX_N];
int dp[1 << MAX_N][MAX_N];
int color[MAX_N];

void solve() {
  for (int S = 0; S < 1 << V; S++) {
    fill(dp[S], dp[S] + V, INF);
  }
  dp[(1 << V) - 1][0] = 0;
  for (int S = (1 << V) - 2; S >= 0; S--) {
    for (int v = 0; v < V; v++) {
      for (int u = 0; u < V; u++) {
        if (!(S >> u & 1)) {
          dp[S][v] = min(dp[S][v], dp[S | 1 << u][u] + d[v][u]);
        }
      }
    }
  }
  if (dp[0][0] >= INF)
    printf("-1\\n");
  else
    printf("%d\\n", dp[0][0]);
}

// dfsで塗る
int dfs(int v, int a) {
  color[v] = 1;
  a++;
  for (int i = 0; i < V; i++) {
    if (d[v][i] != INF && color[i] == 0) {
      a = dfs(i, a);
    }
  }
  return a;
}

int main() {
  int s, t, dd;
  scanf("%d %d", &V, &E);
  if (E == 0) {
    printf("-1\\n");
    return 0;
  }
  for (int i = 0; i <= V; i++) {
    fill(d[i], d[i] + V + 1, INF);
  }
  for (int i = 0; i < E; i++) {
    scanf("%d%d%d", &s, &t, &dd);
    d[s][t] = dd;
  }
  // printf("%d\\n",dfs(0,0));
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 200005
#define MAX_N 15
#define INF 10000005

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int V, E;
ll K;
int d[MAX_N][MAX_N];
int dp[1 << MAX_N][MAX_N];
int color[MAX_N];

void solve() {
  for (int S = 0; S < 1 << V; S++) {
    fill(dp[S], dp[S] + V, INF);
  }
  dp[(1 << V) - 1][0] = 0;
  for (int S = (1 << V) - 2; S >= 0; S--) {
    for (int v = 0; v < V; v++) {
      for (int u = 0; u < V; u++) {
        if (!(S >> u & 1)) {
          dp[S][v] = min(dp[S][v], dp[S | 1 << u][u] + d[v][u]);
        }
      }
    }
  }
  if (dp[0][0] >= INF)
    printf("-1\\n");
  else
    printf("%d\\n", dp[0][0]);
}

// dfsで塗る
int dfs(int v, int a) {
  color[v] = 1;
  a++;
  for (int i = 0; i < V; i++) {
    if (d[v][i] != INF && color[i] == 0) {
      a = dfs(i, a);
    }
  }
  return a;
}

int main() {
  int s, t, dd;
  scanf("%d %d", &V, &E);
  if (E == 0) {
    printf("-1\\n");
    return 0;
  }
  for (int i = 0; i <= V; i++) {
    fill(d[i], d[i] + V + 1, INF);
  }
  for (int i = 0; i < E; i++) {
    scanf("%d%d%d", &s, &t, &dd);
    d[s][t] = dd;
  }
  // printf("%d\\n",dfs(0,0));
  solve();
  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

using namespace std;

#define MAX_CITY_NUM 15
#define MAX 100000000

int D[MAX_CITY_NUM][MAX_CITY_NUM];

int dp[MAX_CITY_NUM][1 << MAX_CITY_NUM];

int tsp(int v, int b, int city_num) {
  int rtn = MAX;
  //??¨????????????????¨??????????
  if (b == (1 << city_num) - 1) {
    if (D[v][0] == -1) {
      return rtn;
    } else {
      return D[v][0];
    }
  }

  for (int i = 0; i < city_num; i++) {
    //????¨?????????????????¨??????????
    if (!(b & (1 << i)) && D[v][i] != -1) {
      int cost = dp[i][b | (1 << i)] != 0 ? dp[i][b | (1 << i)]
                                          : tsp(i, b | (1 << i), city_num);
      dp[i][b | (1 << i)] = cost;
      rtn = min(rtn, D[v][i] + cost);
    }
  }

  return rtn;
}

int main() {

  int V, E, frm, to, cost, ans;

  cin >> V >> E;

  //-1??§?????????
  memset(D, -1, sizeof(D));
  for (int i = 0; i < E; i++) {
    cin >> frm >> to >> cost;
    D[frm][to] = cost;
  }

  ans = tsp(0, 1, V);

  if (ans >= MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

#define MAX_CITY_NUM 15
#define MAX 100000000

int d[MAX_CITY_NUM][MAX_CITY_NUM];

int tsp(int v, int b, int city_num) {
  int rtn = MAX;
  //??¨????????????????¨???????????????´???
  if (b == (1 << city_num) - 1) {
    if (d[v][0] == -1) {
      return rtn;
    } else {
      return d[v][0];
    }
  }

  for (int i = 0; i < city_num; i++) {
    //??????????¨???????????????§????????´???
    if (!(b & (1 << i)) && d[v][i] != -1) {
      rtn = min(rtn, d[v][i] + tsp(i, b | (1 << i), city_num));
    }
  }

  return rtn;
}

int main() {

  int V, E, frm, to, cost, ans;

  cin >> V >> E;

  //-1??§?????????
  memset(d, -1, sizeof(d));
  for (int i = 0; i < E; i++) {
    cin >> frm >> to >> cost;
    d[frm][to] = cost;
  }

  ans = tsp(0, 1, V);

  if (ans >= MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
// #include "MyMath.h"
// #include "DataStructure.h"
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
using namespace std;
const int num = 15, num1 = 10000000;
typedef long long int ll;
typedef pair<int, int> p;
const int INF = 20000000;
int dp[1 << num][num], m[num][num];
int n, e;
int solve(int s, int v) {
  if (s == (1 << n) - 1 && v == 0) {
    return dp[s][v] = 0;
  }
  if (dp[s][v] >= 0)
    return dp[s][v];
  int res = INF;
  for (int t = 0; t < n; t++)
    if (!(s >> t & 1))
      res = min(res, m[v][t] + solve(s | 1 << t, t));
  return dp[s][v] = res;
}
int main() {
  cin >> n >> e;
  fill(m[0], m[num], INF);
  fill(dp[0], dp[1 << num], -1);
  for (int i = 0; i < e; i++) {
    int s, t, u;
    cin >> s >> t >> u;
    m[s][t] = u;
  }
  if (solve(0, 0) >= INF)
    cout << -1 << endl;
  else
    cout << solve(0, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
// #include "MyMath.h"
// #include "DataStructure.h"
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
using namespace std;
const int num = 15, num1 = 10000000;
typedef long long int ll;
typedef pair<int, int> p;
const int INF = 20000000;
int dp[1 << num][num], m[num][num];
int n, e;
int solve(int s, int v) {
  if (s == (1 << n) - 1 && v == 0) {
    return dp[s][v] = 0;
  }
  if (dp[s][v] != INF)
    return dp[s][v];
  int res = INF;
  for (int t = 0; t < n; t++)
    if (!(s >> t & 1))
      res = min(res, m[v][t] + solve(s | 1 << t, t));
  return dp[s][v] = res;
}
int main() {
  cin >> n >> e;
  fill(m[0], m[num], INF);
  fill(dp[0], dp[1 << num], INF);
  for (int i = 0; i < e; i++) {
    int s, t, u;
    cin >> s >> t >> u;
    m[s][t] = u;
  }
  if (solve(0, 0) >= INF)
    cout << -1 << endl;
  else
    cout << solve(0, 0) << endl;
  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

const int V_MAX = 15;
const int INF = 10000000;

int n, e;
int g[V_MAX][V_MAX];
int dp[1 << V_MAX][V_MAX];

int tsp(int visited, int v) {
  if (dp[visited][v] != -1)
    return dp[visited][v];

  if (visited == (1 << n) - 1 && v == 0)
    return 0;

  int res = INF;
  for (int u = 0; u < n; ++u) {
    if (!(visited & (1 << u)) && g[v][u] != INF) {
      res = min(res, tsp(visited | (1 << u), u) + g[v][u]);
    }
  }
  return dp[visited][v] = res;
}

int main() {
  scanf("%d %d", &n, &e);
  fill(&g[0][0], &g[n][0], INF);
  for (int i = 0; i < e; ++i) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    g[s][t] = d;
  }

  fill(&dp[0][0], &dp[1 << V_MAX][0], -1);
  int d = tsp(0, 0);
  if (d == INF)
    printf("-1\\n");
  else
    printf("%d\\n", d);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

const int V_MAX = 15;
const int INF = 10000000;

int n, e;
int g[V_MAX][V_MAX];
int dp[1 << V_MAX][V_MAX];

int tsp(int visited, int v) {
  if (dp[visited][v] != -1)
    return dp[visited][v];

  if (visited == (1 << n) - 1 && v == 0)
    return 0;

  int res = INF;
  for (int u = 0; u < n; ++u) {
    if (!(visited & (1 << u)) && g[v][u] != INF) {
      res = min(res, tsp(visited | (1 << u), u) + g[v][u]);
    }
  }
  return res;
}

int main() {
  scanf("%d %d", &n, &e);
  fill(&g[0][0], &g[n][0], INF);
  for (int i = 0; i < e; ++i) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    g[s][t] = d;
  }

  fill(&dp[0][0], &dp[1 << V_MAX][0], -1);
  int d = tsp(0, 0);
  if (d == INF)
    printf("-1\\n");
  else
    printf("%d\\n", d);

  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

int v, n, dis[15][15];
int dp[(1 << 15)][15];
const int INF = 100000000;

int main() {
  cin >> v >> n;
  for (int i = 0; i < v; i++)
    for (int j = 0; j < v; j++)
      dis[i][j] = INF;
  for (int i = 0; i < (1 << v); i++)
    for (int j = 0; j < v; j++)
      dp[i][j] = INF;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    dis[a][b] = c;
  }
  dp[0][0] = 0;
  for (int i = 0; i < (1 << v) - 1; i++) {
    for (int j = 0; j < v; j++) {
      if (i && !(i & (1 << j)))
        continue;
      for (int k = 0; k < v; k++) {
        if (j == k)
          continue;
        if (!(i & (1 << k))) {
          dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + dis[j][k]);
        }
      }
    }
  }
  if (dp[(1 << v) - 1][0] == INF)
    cout << -1 << endl;
  else
    cout << dp[(1 << v) - 1][0] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int v, n, dis[15][15];
int dp[(1 << 15)][15];
const int INF = 100000000;

int main() {
  cin >> v >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      dis[i][j] = INF;
  for (int i = 0; i < (1 << v); i++)
    for (int j = 0; j < v; j++)
      dp[i][j] = INF;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    dis[a][b] = c;
  }
  dp[0][0] = 0;
  for (int i = 0; i < (1 << v) - 1; i++) {
    for (int j = 0; j < v; j++) {
      if (i && !(i & (1 << j)))
        continue;
      for (int k = 0; k < v; k++) {
        if (j == k)
          continue;
        if (!(i & (1 << k))) {
          dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + dis[j][k]);
        }
      }
    }
  }
  if (dp[(1 << v) - 1][0] == INF)
    cout << -1 << endl;
  else
    cout << dp[(1 << v) - 1][0] << endl;
  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define repeat(i, n) for (int i = 0; i < (n); ++i)

#define MAX_V 15
#define MAX_D 1000
#define SUP (MAX_V * MAX_D + 1)
int V, E;
int d[MAX_V][MAX_V];
int memo[1 << MAX_V][MAX_V]; // nodes already walked * current node

int recur(int s, int v) {
  if (memo[s][v] != SUP)
    return memo[s][v];
  if (s == (1 << V) - 1 and v == 0)
    return memo[s][v] = 0;
  int result = SUP + 1;
  repeat(i, V) if (not(s & 1 << i)) if (d[v][i] < SUP) {
    result = min(result, d[v][i] + recur(s | 1 << i, i));
  }
  return memo[s][v] = result;
}

int main() {
  cin >> V >> E;
  repeat(i, V) repeat(j, V) d[i][j] = SUP;
  repeat(i, E) {
    int a, b;
    cin >> a >> b;
    cin >> d[a][b];
  } // there are no multiedge
  repeat(i, 1 << V) repeat(j, V) memo[i][j] = SUP;
  cout << (recur(0, 0) < SUP ? recur(0, 0) : -1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define repeat(i, n) for (int i = 0; i < (n); ++i)

#define MAX_V 15
#define MAX_D 1000
#define SUP (MAX_V * MAX_D + 1)
int V, E;
int d[MAX_V][MAX_V];
int memo[1 << MAX_V][MAX_V]; // nodes already walked * current node

int recur(int s, int v) {
  if (memo[s][v] < SUP)
    return memo[s][v];
  if (s == (1 << V) - 1 and v == 0)
    return memo[s][v] = 0;
  int result = SUP;
  repeat(i, V) if (not(s & 1 << i)) {
    result = min(result, d[v][i] + recur(s | 1 << i, i));
  }
  return memo[s][v] = result;
}

int main() {
  cin >> V >> E;
  repeat(i, V) repeat(j, V) d[i][j] = SUP;
  repeat(i, E) {
    int a, b;
    cin >> a >> b;
    cin >> d[a][b];
  } // there are no multiedge
  repeat(i, 1 << V) repeat(j, V) memo[i][j] = SUP;
  cout << (recur(0, 0) < SUP ? recur(0, 0) : -1) << endl;
  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int inf = 987654321;
int n, m;
vector<vector<int>> G;
vector<vector<int>> dp;

int rec(int visited, int now) {
  if (dp[visited][now] != -1)
    return dp[visited][now];
  if (visited == (1 << n) - 1 && now == 0)
    return dp[visited][now] = 0;
  int res = inf;
  for (int i = 0; i < n; i++) {
    if (visited >> i & 1)
      continue; /* ??¢????¨??????? */
    res = min(res, rec(visited | (1 << i), i) + G[now][i]);
  }
  return dp[visited][now] = res;
}

int main(void) {
  cin >> n >> m;
  G.assign(n, vector<int>(n, inf));
  for (int i = 0; i < m; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    G[p][q] = c;
  }
  dp.assign(1 << n, vector<int>(n, -1));
  int res = rec(0, 0);
  if (res != inf)
    cout << res << endl;
  else
    cout << -1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int inf = 987654321;
int n, m;
vector<vector<int>> G;
vector<vector<int>> dp;

int rec(int visited, int now) {
  if (dp[visited][now] != -1)
    return dp[visited][now];
  if (visited == (1 << n) - 1 && now == 0)
    return dp[visited][now] = 0;
  int res = inf;
  for (int i = 0; i < n; i++) {
    if (visited >> i & 1)
      continue; /* ??¢????¨??????? */
    res = min(res, rec(visited | (1 << i), i) + G[now][i]);
  }
  return dp[visited][now] = res;
}

int main(void) {
  cin >> n >> m;
  G.assign(n, vector<int>(m, inf));
  for (int i = 0; i < m; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    G[p][q] = c;
  }
  dp.assign(1 << n, vector<int>(n, -1));
  int res = rec(0, 0);
  if (res != inf)
    cout << res << endl;
  else
    cout << -1 << endl;
  return 0;
}