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|
s0 t0 d0
s1 t1 d1
:
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.

si and ti represent source and target vertices of i-th edge (directed) and di represents the distance between si and ti (the i-th edge).

Output

Print the shortest distance in a line. If there is no solution, print -1.

Constraints

2 ≤ |V| ≤ 15

0 ≤ di ≤ 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

Solution in C++14 (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define rrep(i, n) for(int i=n-1; i>=0; i--)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int V, E;
int g[20][20];
int dp[1<<20][20];

int main() {
    cin >> V >> E;
    rep(i, V) rep(j, V) g[i][j] = -1;
    rep(i, E) {
        int s, t, d;
        cin >> s >> t >> d;
        g[s][t] = d;
    }

    rep(i, 1<<V) rep(j, 20) dp[i][j] = 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 & 1<<u) && g[v][u] != -1) {
                    dp[S][v] = min(dp[S][v], dp[S|1<<u][u] + g[v][u]);
                }
            }
        }
    }

    cout << (dp[0][0] != INF ? dp[0][0] : -1) << endl;
}