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 Rust (judge verdict: Accepted)

use std::cmp::min;
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

const INF: i32 = std::i32::MAX;

fn main() {
    input! {
    v: usize,
    e: usize,
    std: [(usize, usize, i32); e]
    }
    let mut g = vec![vec![-1; v]; v];
    for i in 0..e {
        let (s, t, d) = (std[i].0, std[i].1, std[i].2);
        g[s][t] = d;
    }
    let mut dp = vec![vec![-1; v]; 1 << v];
    let ans = dfs((1 << v) - 1, 0, &mut dp, &g);
    println!("{}", if ans < INF {ans} else {-1});
}

fn dfs(b: usize, t: usize, dp: &mut Vec<Vec<i32>>, g: &Vec<Vec<i32>>) -> i32 {
    if dp[b][t] >= 0 { return dp[b][t]; }
    if b.count_ones() == 1 && g[0][t] >= 0 {return g[0][t]}
    let mut res = INF;
    let v = dp[0].len();
    for s in 0..v {
        if b & (1 << s) == 0 { continue; }
        if g[s][t] < 0 { continue; }
        let p = dfs(b ^ (1 << t), s, dp, g);
        if p == INF { continue; }
        res = min(res, p + g[s][t]);
    }
    dp[b][t] = res;
    res
}