Problem:
Traveling Salesman Problem
For a given weighted directed graph
G(V, E)
, find the distance of the shortest route that meets the following criteria:
It is a closed cycle where it ends at the same point it starts.
It visits each vertex exactly once.
Input
|V|
|E|
s
0
t
0
d
0
s
1
t
1
d
1
:
s
|E|-1
t
|E|-1
d
|E|-1
|V|
is the number of vertices and
|E|
is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,...,
|V|
-1 respectively.
s
i
and
t
i
represent source and target vertices of
i
-th edge (directed) and
d
i
represents the distance between
s
i
and
t
i
(the
i
-th edge).
Output
Print the shortest distance in a line. If there is no solution, print -1.
Constraints
2 ≤
|V|
≤ 15
0 ≤
d
i
≤ 1,000
There are no multiedge
Sample Input 1
4 6
0 1 2
1 2 3
1 3 9
2 0 1
2 3 6
3 2 4
Sample Output 1
16
Sample Input 2
3 3
0 1 1
1 2 1
0 2 1
Sample Output 2
-1

Submitted solutions:

code_snippet: // https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_2_A&lang=ja
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

int main(){
    int node, edge;
    cin >> node >> edge;
    vector<vector<int>> G(node, vector<int>(node, -1));

    int s,t,d;
    for (int i=0; i<edge; i++){
        cin >> s >> t >> d;
        G[s][t] = d;
    }

    int FULL = (1<<node);
    vector<vector<int>> dp(FULL+1, vector<int>(node, 20000));
    // for (int to=0; to<node; to++) dp[1<<to][to] = 0;
    // サイクルは回転不変：どこを視点にしても最小コストは変わらない。
    dp[1<<0][0] = 0;

    for (int search_range_bit=1; search_range_bit<FULL; search_range_bit++){
        for (int to=0; to<node; to++){
            if (!(search_range_bit & (1<<to))) continue;

            int explored_node_bit = search_range_bit ^ (1<<to);
            if (explored_node_bit == 0) continue;

            for (int from=0; from<node; from++){
                if (G[from][to] < 0) continue;
                if (!(explored_node_bit & (1<<from))) continue;
                dp[search_range_bit][to] = min(dp[search_range_bit][to], dp[explored_node_bit][from]+G[from][to]);
            }
        }
    }

    int ans = 20000;
    for (int to=0; to<node; to++){
        if (G[to][0] < 0) continue;
        ans = min(ans, dp[FULL-1][to] + G[to][0]);
    }
    if (ans == 20000) ans = -1;

    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6516
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
const int INF = 1000100100;

template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

int V, E;
int G[20][20];  // グラフ
int dp[50000][20];
// メモ化再帰
int rec(int S, int v) {  
    if (S == 0) {
        if (v == 0) {
            return 0;
        } else {
            return INF;
        }
    }
    if ((S & (1 << v)) == 0) {  // Sに{v}が含まれていない
        return INF;
    }

    int &ret = dp[S][v];
    if (ret != 0) return ret;

    ret = INF;
    REP(u, V) { chmin(ret, rec(S ^ (1 << v), u) + G[u][v]); }
    return ret;
}

int main() {
    cin >> V >> E;

    // グラフの初期化
    REP(i, 20) {
        REP(j, 20) { G[i][j] = INF; }
    }
    REP(i, E) {
        int s, t, d;
        cin >> s >> t >> d;
        G[s][t] = d;
    }

    int ans = rec((1 << V) - 1, 0);

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

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5904
final_rank: 3

code_snippet: #include "bits/stdc++.h"
using namespace std;

#define ll long long
#define lb long double

const int MOD = 1e9+7;
const int INF = 1e9;

const int N = 15;
int dp[1 << N][N];

void solve() {
  int v, e;
  cin >> v >> e;
  vector<array<int, 2>> rg[v];
  for (int i = 0; i < e; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    rg[t].push_back({s, d});
  }
  for (int mk = 0; mk < (1 << v); mk++) {
    for (int i = 0; i < v; i++) {
      dp[mk][i] = INF;
    }
  }
  dp[1][0] = 0;
  for (int mk = 1; mk < (1 << v); mk++) {
    for (int i = 0; i < v; i++) {
      if (((mk >> i) & 1) == 0) continue;
      int prev = mk ^ (1 << i);
      for (auto [u, t] : rg[i]) {
        if (((prev >> u) & 1) == 0) continue;
        dp[mk][i] = min(dp[mk][i], dp[prev][u] + t);
      }
    }
  }
  int ans = INF;
  for (auto [u, t] : rg[0]) {
    ans = min(ans, dp[(1 << v) - 1][u] + t);
  }
  cout << (ans == INF ? -1 : ans) << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);

  // int T = 1;
  // cin >> T;
  // while (T--) {
    solve();
  // }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5260
final_rank: 1

code_snippet: #include "bits/stdc++.h"
using namespace std;

#define ll long long
#define lb long double

const int MOD = 1e9+7;
const int INF = 1e9;

const int N = 15;
int dp[1 << N][N];

void solve() {
  int v, e;
  cin >> v >> e;
  vector<array<int, 2>> rg[v];
  for (int i = 0; i < e; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    rg[t].push_back({s, d});
  }
  for (int mk = 0; mk < (1 << v); mk++) {
    for (int i = 0; i < v; i++) {
      dp[mk][i] = INF;
    }
  }
  dp[1][0] = 0;
  for (int mk = 1; mk < (1 << v); mk++) {
    for (int i = 0; i < v; i++) {
      if (((mk >> i) & 1) == 0) continue;
      int prev = mk ^ (1 << i);
      for (int j = 0; j < v; j++) {
        if (((prev >> j) & 1) == 0) continue;
        for (auto [u, d] : rg[i]) {
          if (u != j || dp[prev][u] == INF) continue;
          dp[mk][i] = dp[prev][u] + d;
        }
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < v; i++) {
    for (auto [u, t] : rg[0]) {
      ans = min(ans, dp[(1 << v) - 1][u] + t);
    }
  }
  cout << (ans == INF ? -1 : ans) << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);

  // int T = 1;
  // cin >> T;
  // while (T--) {
    solve();
  // }

  return 0;
}
accuracy: 0.375
time_ms: 10
memory_kb: 5300
final_rank: 20

code_snippet: #include "bits/stdc++.h"
using namespace std;

#define ll long long
#define lb long double

const int MOD = 1e9+7;
const int INF = 1e9;

const int N = 15;
int dp[1 << N][N];

void solve() {
  int v, e;
  cin >> v >> e;
  vector<array<int, 2>> rg[v];
  for (int i = 0; i < e; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    rg[t].push_back({s, d});
  }
  for (int mk = 0; mk < (1 << v); mk++) {
    for (int i = 0; i < v; i++) {
      dp[mk][i] = INF;
    }
  }
  dp[1][0] = 0;
  for (int mk = 1; mk < (1 << v); mk++) {
    for (int i = 0; i < v; i++) {
      if (((mk >> i) & 1) == 0) continue;
      int prev = mk ^ (1 << i);
      for (int j = 0; j < v; j++) {
        if (((prev >> j) & 1) == 0) continue;
        for (auto [u, d] : rg[i]) {
          if (u != j || dp[prev][u] == INF) continue;
          dp[mk][i] = dp[prev][u] + d;
        }
      }
    }
  }
  int ans = INF;
  for (auto [u, t] : rg[0]) {
    ans = min(ans, dp[(1 << v) - 1][u] + t);
  }
  cout << (ans == INF ? -1 : ans) << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);

  // int T = 1;
  // cin >> T;
  // while (T--) {
    solve();
  // }

  return 0;
}
accuracy: 0.375
time_ms: 10
memory_kb: 5276
final_rank: 19

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

const int INF = 1e9;
const int N = 15;
int dp[1 << N][N];

void solve() {
    int v, e;
    cin >> v >> e;
    vector<vector<pair<int,int>>> g(v); // adjacency list: g[u] = {v, cost}

    for (int i = 0; i < e; i++) {
        int s, t, d;
        cin >> s >> t >> d;
        g[s].push_back({t, d}); // outgoing edge
    }

    // initialize DP table
    for (int mk = 0; mk < (1 << v); mk++)
        for (int i = 0; i < v; i++)
            dp[mk][i] = INF;

    dp[1][0] = 0; // start at vertex 0

    for (int mk = 1; mk < (1 << v); mk++) {
        for (int u = 0; u < v; u++) {
            if (!(mk & (1 << u))) continue; // u not in mask
            for (auto [v2, cost] : g[u]) {
                if (mk & (1 << v2)) continue; // already visited
                int nxt = mk | (1 << v2);
                dp[nxt][v2] = min(dp[nxt][v2], dp[mk][u] + cost);
            }
        }
    }

    int ans = INF;
    for (int u = 1; u < v; u++) {
        for (auto [v2, cost] : g[u]) {
            if (v2 == 0)
                ans = min(ans, dp[(1 << v) - 1][u] + cost);
        }
    }

    cout << (ans == INF ? -1 : ans) << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
accuracy: 1
time_ms: 10
memory_kb: 5288
final_rank: 2

code_snippet: #ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif

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

using ll = long long;            // 2^63-1まで 負の値は可
using ull = unsigned long long;  // 0<= ull <=2^64-1の範囲 負の値は不可
using vl = vector<ll>;
using vvl = vector<vl>;
using P = pair<ll, ll>;
template <typename T>
using MNPQ = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using MXPQ = priority_queue<T, vector<T>, less<T>>;

#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = s; i > n; i--)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define nall(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define vunique(v) v.erase(unique(nall(v)), v.end())
#define IN(a, x) find(nall(a), x) != a.end()
#define YN(flg) cout << (flg ? "Yes" : "No") << "\n"
#define out_grid(x, y, h, w) !(0 <= x && x < h && 0 <= y && y < w)
#define lmd(...) [&](__VA_ARGS__)
#define debug(x) cerr << #x << " = " << x << endl;
const ll INF = 2e18;

template <typename T>
istream& operator>>(istream& is, vector<T>& a) {
    for (auto& x : a) is >> x;
    return is;
}

template <typename T>
ostream& operator<<(ostream& os, vector<T>& a) {
    for (int i = 0; i < (int)a.size(); i++) os << a[i] << " ";
    return os;
}

template <typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2>& p) {
    os << "{" << p.first << "," << p.second << "}";
    return os;
}

template <typename K, typename V>
ostream& operator<<(ostream& os, map<K, V>& a) {
    for (auto& [k, v] : a) os << "{key:" << k << ", item:" << v << "} ";
    return os;
}

struct Edge {
    ll to, w;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll v, e;
    cin >> v >> e;

    vector<vector<Edge>> graph(v);
    rep(i, e) {
        ll s, t, d;
        cin >> s >> t >> d;
        graph[s].push_back({t, d});
    }

    vector dp(1 << v, vector<ll>(v, INF));
    dp[1][0] = 0;

    rep(i, 1 << v) {
        rep(j, v) {
            if (i >> j & 1) {
                for (auto edge : graph[j]) {
                    chmin(dp[i][j], dp[i - (1 << j)][edge.to] + edge.w);
                }
            }
        }
    }

    ll ans = INF;
    for (auto edge : graph[0]) chmin(ans, dp.back()[edge.to] + edge.w);
    cout << (ans == INF ? -1 : ans) << endl;

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 9216
final_rank: 14

code_snippet: #ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif

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

using ll = long long;            // 2^63-1まで 負の値は可
using ull = unsigned long long;  // 0<= ull <=2^64-1の範囲 負の値は不可
using vl = vector<ll>;
using vvl = vector<vl>;
using P = pair<ll, ll>;
template <typename T>
using MNPQ = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using MXPQ = priority_queue<T, vector<T>, less<T>>;

#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = s; i > n; i--)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define nall(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define vunique(v) v.erase(unique(nall(v)), v.end())
#define IN(a, x) find(nall(a), x) != a.end()
#define YN(flg) cout << (flg ? "Yes" : "No") << "\n"
#define out_grid(x, y, h, w) !(0 <= x && x < h && 0 <= y && y < w)
#define lmd(...) [&](__VA_ARGS__)
#define debug(x) cerr << #x << " = " << x << endl;
const ll INF = 2e18;

template <typename T>
istream& operator>>(istream& is, vector<T>& a) {
    for (auto& x : a) is >> x;
    return is;
}

template <typename T>
ostream& operator<<(ostream& os, vector<T>& a) {
    for (int i = 0; i < (int)a.size(); i++) os << a[i] << " ";
    return os;
}

template <typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2>& p) {
    os << "{" << p.first << "," << p.second << "}";
    return os;
}

template <typename K, typename V>
ostream& operator<<(ostream& os, map<K, V>& a) {
    for (auto& [k, v] : a) os << "{key:" << k << ", item:" << v << "} ";
    return os;
}

struct Edge {
    ll to, w;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll v, e;
    cin >> v >> e;

    vector<vector<Edge>> graph(v);
    rep(i, e) {
        ll s, t, d;
        cin >> s >> t >> d;
        graph[s].push_back({t, d});
    }

    vector dp(1 << v, vector<ll>(v, INF));
    dp[1][0] = 0;

    rep(i, 1 << v) {
        rep(j, v) {
            if (i >> j & 1) {
                for (auto edge : graph[j]) {
                    if (i & (1 << j))
                        chmin(dp[i][j], dp[i - (1 << j)][edge.to] + edge.w);
                }
            }
        }
    }

    ll ans = INF;
    for (auto edge : graph[0]) chmin(ans, dp.back()[edge.to] + edge.w);
    cout << (ans == INF ? -1 : ans) << endl;

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 9216
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define int long long
using namespace std;

void solve() {
    int n, m; cin >> n >> m;
    vector<vector<int>> dist(n, vector<int>(n, 1e9));
    for (int i = 0; i < m; i ++) {
        int a, b, d; cin >> a >> b >> d;
        dist[a][b] = min(dist[a][b], d);
    }
    vector<vector<int>> dp(1 << n, vector<int>(n, 1e9));
    dp[1][0] = 0;
    for (int mask = 1; mask < (1 << n); mask ++) {
        for (int u = 0; u < n; u ++) {
            if (!(mask & (1 << u))) {
                continue;
            }
            for (int v = 0; v < n; v ++) {
                if (mask & (1 << v)) {
                    continue;
                }
                dp[mask | (1 << v)][v] = min(dp[mask | (1 << v)][v], dp[mask][u] + dist[u][v]);
            }
        }
    }
    int ans = 1e9;
    for (int u = 0; u < n; u ++) {
        ans = min(ans, dp[(1 << n) - 1][u] + dist[u][0]);
    }
    if (ans != 1e9) {
        cout << ans << endl;
    } else {
        cout << -1 << endl;
    }
}

signed main() {
// #ifndef ONLINE_JUDGE
//     freopen("in.txt", "rt", stdin);
//     freopen("out.txt", "wt", stdout);
// #endif
    int T = 1; // cin >> T;
    while (T --) solve();

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 8192
final_rank: 10

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

int main() {
    int N, E;
    cin >> N >> E;
    int INF=1e9;
    vector<vector<int>> dist(N,vector<int>(N,INF));
    for(int i=0; i<E; i++){
        int s, t, d;
        cin >> s >> t >> d;
        dist[s][t]=d;
    }

    vector<vector<int>> dp((1<<N), vector<int>(N,INF));
    dp[0][0]=0;
    for(int i=0; i<(1<<N); i++){
        for(int j=0; j<N; j++){
            if(i==0 || (i&(1<<j))){
                for(int k=0; k<N; k++){
                    if(!(i&(1<<k)) && dist[j][k]!=INF){
                        int ni=(i|(1<<k));
                        dp[ni][k]=min(dp[ni][k],dp[i][j]+dist[j][k]);
                    }
                }
            }
        }
    }

    if(dp[(1<<N)-1][0]==INF){
        cout << -1 << endl;
    }
    else{
        cout << dp[(1<<N)-1][0] << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 6364
final_rank: 4

code_snippet: #include <iostream>
#include <utility>
#include <vector>

int main()
{
    unsigned int V, E, s, t, d;
    std::cin >> V >> E;
    std::vector<std::vector<unsigned int>> G(V, std::vector<unsigned int>(V, -1));
    for (auto i = 0u; i < E; i++) {
        std::cin >> s >> t >> d;
        G[s][t] = d;
    }

    constexpr unsigned int INF = 1'000'000'000;
    std::vector<std::vector<unsigned int>> DP(1 << V, std::vector<unsigned int>(V, INF));
    DP[0][0] = 0;

    for (auto set = 0u; set < (1u << V); set++) {
        for (auto prev = 0u; prev < V; prev++) {
            for (auto next = 0u; next < V; next++) {
                if ((set & (1 << next)) ||
                    (prev == next) ||
                    (G[prev][next] == static_cast<unsigned int>(-1))
                ) {
                    continue;
                }

                DP[set | (1 << next)][next] = std::min(
                    DP[set | (1 << next)][next],
                    DP[set][prev] + G[prev][next]
                );
            }
        }
    }

    unsigned int ans = DP[(1 << V) - 1][0];
    std::cout << (ans == INF ? -1 : static_cast<int>(ans)) << std::endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6656
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main() {
  int V, E; cin >> V >> E;
  vector<vector<int>> G(V, vector<int>(V, -1));
  for (int _ = 0; _ < E; ++_) {
    int s, t, d; cin >> s >> t >> d;
    G[s][t] = d;
  }

  vector<vector<int>> dp((1<<V), vector<int>(V, -1)); //dp[S][v] = 訪問した集合がSで現在vにいるときの最小移動距離
  dp[1][0] = 0;
  for (int S = 1; S < (1 << V); ++S) {
    for (int v = 0; v < V; ++v) {
      //vは今回訪問するところ
      if (!(S & (1 << v))) continue;
      int prevS = S ^ (1 << v);
      for (int u = 0; u < V; ++u) {
        //uはひとつ前に訪問したところ
        if (!(prevS & (1 << u))) continue; //uがprevSに含まれない
        if (G[u][v] == -1) continue; //uからvには行けない
        if (dp[prevS][u] == -1) continue; //uが前回の訪問先になることはない
        int cost = dp[prevS][u] + G[u][v];
        if (dp[S][v] == -1 || dp[S][v] > cost) {
          dp[S][v] = cost;
        }
      }
    }
  }

  int ans = INT_MAX;
  for (int v = 1; v < V; ++v) {
    if (G[v][0] == -1) continue;
    if (dp[(1 << V) - 1][v] == -1) continue;
    ans = min(ans, dp[(1 << V) -1][v] + G[v][0]);
  }
  if (ans == INT_MAX) cout << -1 << endl;
  else cout << ans << endl;
  return 0;
  
}
accuracy: 1
time_ms: 10
memory_kb: 6380
final_rank: 5

code_snippet: #include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <set>
#include <climits>
#include <queue>
#include <map>
#include <cmath>
#include <sstream>
#include <bitset>
using namespace std;
typedef long long int ll;
#define MAX_V 16
int adjMat[MAX_V][MAX_V];
bool visited[MAX_V][1<<MAX_V];//visited存的並非節點是否有訪問過,而是這個狀態是否有出現過
int dp[MAX_V][1<<MAX_V];//DP表格,第一個中括號代表目前所在的點
//第二個中括號代表目前節點的狀態(ex:0101011,表示1,3,5節點還沒走過,可以直接用bitwise操作來判斷狀態)
//因為DP表格是N*2^N次方,所以時間複雜度就是O(N*2^N)
int v,e;

const char *byte_to_binary(int x)
{
    static char b[9];
    b[0] = '\0';

    int z;
    for (z = 128; z > 0; z >>= 1)
    {
        strcat(b, ((x & z) == z) ? "1" : "0");
    }

    return b;
}
int TSP(int at,int state)
{
    // printf("at=%d,state=%s\n",at, byte_to_binary(state));
    if(state == (1<<v)-1 && at==0){ //全部的狀態都走完而且回到起點惹
        visited[at][state]=true;
        return dp[at][state];
    }
    if(dp[at][state]) //出現過的狀態,直接回傳結果
        return dp[at][state];
    int ans = INT_MAX>>1;
    int cost;
    for(int i=0;i<v;i++){
        //state&(1<<i)==0表示判斷右邊數過來第i個bit是否為0,用來判斷該節點是否有走過
        //state|(1<<i)表示將該節點設成走過
        if(adjMat[at][i]!=-1 && (state & (1<<i))==0){
            // printf("at=%d->i=%d\n",at,i);
            cost = TSP(i,state|(1<<i)) + adjMat[at][i];//往每種可以走的路線都走下去
            // printf("at=%d->i=%d,cost=%d\n",at,i,cost);
            ans = min(ans,cost);//求出最小值
        }
    }
    return dp[at][state] = ans;
}
//
int main()
{   
    // freopen("in","r",stdin);
    while(~scanf("%d %d",&v,&e)){
        memset(adjMat,-1,sizeof(adjMat));
        for(int i=0;i<e;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            adjMat[u][v]=w;
        }
        memset(dp,0,sizeof(dp));
        memset(visited,0,sizeof(visited));
        int ans = TSP(0,0);
        if(ans>=INT_MAX>>1)
            puts("-1");
        else
            printf("%d\n",ans);
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 8688
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

const int INF = 10000000;
const int VISITED = 100000;

int dp(int from, int to, int bit, vector<vector<int>> &memo, const vector<vector<pair<int, int>>> &adj) {
    if (memo[from][bit] != INF) return memo[from][bit];
    if ((1<<from) == bit) {
        bit -= (1<<from);
        for (auto con : adj[from]) {
            auto [u, w] = con;
            if (u == to) memo[from][bit] = w;
        }
        if (memo[from][bit] == INF) memo[from][bit] = VISITED;
        return memo[from][bit];
    }
    for (auto con : adj[from]) {
        auto [nxt, w] = con;
        if (((1<<nxt) & bit) && nxt != to) {
            int nxt_bit = bit - (1<<from);
            if (memo[nxt][nxt_bit] == INF) memo[nxt][nxt_bit] = dp(nxt, to, nxt_bit, memo, adj);
            memo[from][bit] = min(memo[from][bit], memo[nxt][nxt_bit] + w);
        }
    }
    return memo[from][bit];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int V, E;
    cin >> V >> E;
    
    vector<vector<pair<int,int>>> adj(V);
    for (int i = 0; i < E; ++i) {
        int s, t, w;
        cin >> s >> t >> w;
        adj[s].emplace_back(make_pair(t, w));
    }

    int bit = (1<<V);
    vector<vector<int>> memo(V, vector<int>(bit, INF));

    int result = INF;
    for (int i = 0; i < V; ++i) {
        vector<vector<int>> now_memo = memo;
        result = min(result, dp(i, i, bit-1, now_memo, adj));
    }
    
    // for (auto m : memo) {
    //     for (auto i : m) {
    //         cout << i << " ";
    //     }
    //     cout << endl;
    // }

    if (VISITED <= result) cout << -1 << endl;
    else cout << result << endl;

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 7040
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

const int INF = 10000000;
const int VISITED = 100000;

int dp(int from, int to, int bit, vector<vector<vector<int>>> &memo, const vector<vector<pair<int, int>>> &adj) {
    if (memo[from][to][bit] != INF) return memo[from][to][bit];
    if ((1<<from) + (1<<to) == bit) {
        for (auto con : adj[from]) {
            auto [u, w] = con;
            if (u == to) memo[from][to][bit] = w;
        }
        if (memo[from][to][bit] == INF) memo[from][to][bit] = VISITED;
        return memo[from][to][bit];
    }
    for (auto con : adj[from]) {
        auto [nxt, w] = con;
        if (((1<<nxt) & bit) && nxt != to) {
            int nxt_bit = bit - (1<<from);
            if (from == to) nxt_bit += (1<<to);
            if (memo[nxt][to][nxt_bit] == INF) memo[nxt][to][nxt_bit] = dp(nxt, to, nxt_bit, memo, adj);
            memo[from][to][bit] = min(memo[from][to][bit], memo[nxt][to][nxt_bit] + w);
        }
    }
    return memo[from][to][bit];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int V, E;
    cin >> V >> E;
    
    vector<vector<pair<int,int>>> adj(V);
    for (int i = 0; i < E; ++i) {
        int s, t, w;
        cin >> s >> t >> w;
        adj[s].emplace_back(make_pair(t, w));
    }

    int bit = (1<<V);
    vector<vector<vector<int>>> memo(V, vector<vector<int>>(V, vector<int>(bit,INF)));

    int result = INF;
    for (int i = 0; i < V; ++i) {
        result = min(result, dp(i, i, bit-1, memo, adj));
    }
    
    // for (auto m : memo) {
    //     for (auto n : m) {
    //         for (auto i : n) {
    //             cout << i << " ";
    //         }
    //         cout << endl;
    //     }
    //     cout << endl;
    // }

    if (VISITED <= result) cout << -1 << endl;
    else cout << result << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 35020
final_rank: 18

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<deque>
#include<list>
#include<bitset>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;

const ld EPS = 1e-10;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
const ld PI = acos( -1.0 );

#define SWAP( A, B ) { auto tmp = A; A = B; B = tmp; }

template <typename T>
void print( vector<T> v ){
    if( v.size() == 0 ) return;
    for( int i = 0 ; i < v.size() - 1 ; i++ )
        cout << v[i] << ' ';
    cout << v[v.size()-1] << endl;
}

struct Edge{

    int to;
    ll cost;

    Edge( int _to, ll _cost ){

        to = _to;
        cost = _cost;

    }

};

struct Graph{

    int size;
    vector< vector<Edge> > adj;

    Graph( int _size ){

        size = _size;
        for( int i = 0 ; i < size ; i++ ){
            vector<Edge> tmp;
            adj.push_back( tmp );
        }

    }

    ll tsp(){

        vector<vector<ll>> dp;
        for( int i = 0 ; i < ( 1 << size ) ; i++ ){
            vector<ll> v;
            for( int j = 0 ; j < size ; j++ )
                v.push_back( INF );
            dp.push_back( v );
        }
        dp[(1<<size)-1][0] = 0;
        for( int i = ( 1 << size ) - 1 ; i >= 0 ; i-- ){
            for( int j = 0 ; j < size ; j++ ){
                for( int k = 0 ; k < adj[j].size() ; k++ ){
                    Edge e = adj[j][k];
                    if( ( ( i >> e.to ) & 1 ) == 0 )
                        dp[i][j] = min( dp[i][j], dp[i|(1 << e.to)][e.to] + e.cost );
                }
            }
        }
        return dp[0][0] == INF ? -1 : dp[0][0];

    }
 
};

int main(){

    int V, E, s, t;
    ll d;

    cin >> V >> E;
    Graph g( V );
    for( int i = 0 ; i < E ; i++ ){
        cin >> s >> t >> d;
        Edge e( t, d );
        g.adj[s].push_back( e );
    }

    cout << g.tsp() << endl;

}
accuracy: 1
time_ms: 10
memory_kb: 8036
final_rank: 9

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

int main(){
  int64_t v, e, ans = 1000000, goal = 1;
  ans++;
  cin >> v >> e;
  vector<vector<int64_t>> dp(v, vector<int64_t> (32800, 100000));
  vector<int64_t> vp(v, 1);//場所の数字
  
  queue<int64_t> qp;//今いる場所place
  queue<int64_t> qhis;//すでに通った場所history
  
  for(int64_t i = 1; i < v; i++){
    vp.at(i) = vp.at(i - 1) * 2;
    goal *= 2;
  }
  goal *= 2;
  goal--;///goalは2^v-1
  
  vector<vector<int64_t>> path(v, vector<int64_t> (v, 10000000));//道の長さ1,2,3...v
  for(int64_t i = 0; i < e; i++){
    int64_t s, t, d;
    cin >> s >> t >> d;
    t--;
    s--;
    if(t == -1){
       t += v;
    }
    if(s == -1){
      dp.at(t).at(vp.at(t)) = d;
      qp.push(t);
      qhis.push(vp.at(t));
    } else{
      path.at(s).at(t) = d;
    }
  }
  
  /*debug*///////////////////////////////////////////
  /*cout <<"goal" << goal << endl;
  cout << "path" << endl;
  for(int64_t i = 0; i < v; i++){
    for(int64_t j = 0; j < v; j++){
      if(path.at(i).at(j) != 10000000){
        cout << path.at(i).at(j) << "_";
      } else{
        cout << "x_";
      }
    }
    cout << endl;
  }
  cout << "vp  dp" << endl;
  for(int64_t i = 0; i < v; i++){
    cout << vp.at(i) << "to";
    for(int64_t j = 0; j < goal + 1; j++){
      if(dp.at(i).at(j) != 100000){
        cout << dp.at(i).at(j) << " ";
      } else{
        cout << "x" << " ";
      }
    }
    cout << endl;
  }*/
  /////////////////////////////////////////////////////
  
  while(!qp.empty()){
    bitset<15> bitto(qhis.front());
    ///////////////////////////////////cout <<"qp" << qp.front() << "qhis" << bitto << endl;
    int64_t ima = qp.front(), tootta = qhis.front(), mtnr = dp.at(ima).at(tootta);
    for(int64_t i = 0; i < v; i++){
      int64_t d = path.at(ima).at(i), Mtnr = mtnr + d, ikisaki = tootta + vp.at(i);
      if(ikisaki / vp.at(i) % 2 == 1){
        if(dp.at(i).at(ikisaki) > Mtnr){
          dp.at(i).at(ikisaki) = Mtnr;
          qp.push(i);
          qhis.push(ikisaki);
        }
      } else{
        ///////////////////////////cout << ikisaki << endl;
      }
    }
    qp.pop();
    qhis.pop();
  }
  
  /*for(int64_t i = 0; i < v; i++){
    dp.at(vp.at(i)).at(i) = 0;
    int64_t ic;
    ic = path.at(i).size();
    for(int64_t j = 0; j < ic; j++){
      q.push((path.at(i).at(j) % 100) + vp.at(i) * 10000 + 100 * i);//下2桁が行先、下3,4桁が今いる場所、それ以外がすでに経由した場所
    }
    while(!q.empty()){
      int64_t stl, frm, to1, res;
      stl = q.front() / 10000;//すでに経由した場所
      frm = (q.front() % 10000) / 100;
      to1 = q.front() % 100;//行先
      
      bitset<15> zo(stl);
      res = stl + vp.at(to1);
      if(dp.at(to1).at(res) > dp.at(frm).at(stl)){
        
      }
      
      q.pop();
    }
  }*/  
  /*for(int64_t i = 0; i < v; i++){
    cout << vp.at(i) << "to";
    for(int64_t j = 0; j < goal + 1; j++){
      if(dp.at(i).at(j) != 100000){
        cout << dp.at(i).at(j) << " ";
      } else{
        cout << "x" << " ";
      }
    }
    cout << endl;
  }*/
  if(dp.at(v - 1).at(goal) == 100000){
    cout << -1 << endl;
    return 0;
  }
  cout << dp.at(v-1).at(goal) << endl;
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 8000
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

int inf = 10010010;

int main()
{
    int v, e;
    cin >> v >> e;
    vector<vector<pair<int, int>>> box(v + 5);
    vector<vector<int>> box2(v + 5, vector<int>(v + 5, -1));
    for (int i = 0; i < e; ++i)
    {
        int s, t, d;
        cin >> s >> t >> d;
        box[s].emplace_back(t, d);
        box2[s][t] = d;
    }
    int ans = inf;
    for (int i = 0; i < v; ++i)
    {
        vector<vector<int>> dp((1 << v) + 5, vector<int>(v + 5, inf));
        dp[1 << i][i] = 0;
        for (int j = 1 << i; j < (1 << v); ++j)
        {
            for (int k = 0; k < v; ++k)
            {
                if (dp[j][k] == inf)
                    continue;
                for (const auto &edge : box[k])

                {
                    int t = edge.first;
                    int d = edge.second;
                    if ((j & (1 << t)) != 0)
                        continue;
                    int visited = j | (1 << t);
                    dp[visited][t] = min(dp[visited][t], dp[j][k] + d);
                }
            }
        }
        for (int j = 0; j < v; ++j)
        {
            if (box2[j][i] == -1)
                continue;
            ans = min(ans, dp[(1 << v) - 1][j] + box2[j][i]);
        }
    }
    if (ans == inf)
    {
        ans = -1;
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 6940
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int main() {
    int v, e;
    cin >> v >> e;

    const int INF = 1e9;
    vector<vector<pair<int, int>>> box(v + 5);           // 隣接リスト
    vector<vector<int>> box2(v + 5, vector<int>(v + 5, -1)); // 辺の重み

    for (int i = 0; i < e; ++i) {
        int s, t, d;
        cin >> s >> t >> d;
        box[s].emplace_back(t, d);
        box2[s][t] = d;
    }

    int ans = INF;
    for (int i = 0; i < v; ++i) {
        vector<vector<int>> dp((1 << v) + 5, vector<int>(v + 5, INF));
        dp[1 << i][i] = 0;

        for (int j = 0; j < (1 << v); ++j) {
            for (int k = 0; k < v; ++k) {
                if (dp[j][k] == INF) continue;

                for (auto& [t, d] : box[k]) {
                    if (j & (1 << t)) continue;

                    int visited = j | (1 << t);
                    dp[visited][t] = min(dp[visited][t], dp[j][k] + d);
                }
            }
        }

        for (int j = 0; j < v; ++j) {
            if (box2[j][i] == -1) continue;

            int cost = dp[(1 << v) - 1][j] + box2[j][i];
            ans = min(ans, cost);
        }
    }

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

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 7040
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <tuple>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cassert>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()

ll llpow(ll a, ll t)
{
  ll res = 1;
  rep(i, t) res *= a;
  return res;
}

ll inf = std::numeric_limits<ll>::max();

int main()
{
  ll v, e;
  cin >> v >> e;
  vector<vector<ll>> g(v, vector<ll>(v, 10e8));
  rep(i, e)
  {
    ll s, t, d;
    cin >> s >> t >> d;
    g[s][t] = d;
  }
  vector<vector<ll>> dp(llpow(2, v), vector<ll>(v, 10e8));
  dp[1 << 0][0] = 0;
  rep(i, 1 << v) // llpow(2,v) と同意
  {
    rep(j, v)
    {
      rep(k, v)
      {
        if ((i >> j & 1) && !(i >> k & 1) && g[j][k] != 10e8)
        {
          dp[i | 1 << k][k] = min(dp[i | 1 << k][k], dp[i][j] + g[j][k]);
        }
      }
    }
  }
  ll ans = 10e8;
  rep(i, v)
  {
    ans = min(ans, dp[(1 << v) - 1][i] + g[i][0]);
  }
  if (ans == 10e8)
  {
    cout << -1 << endl;
  }
  else
  {
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 7796
final_rank: 8