Problem:
Score : 900 points

Problem Statement

Mole decided to live in an abandoned mine. The structure of the mine is represented by a simple connected undirected graph which consists of N vertices numbered 1 through N and M edges.
The i-th edge connects Vertices a_i and b_i, and it costs c_i yen (the currency of Japan) to remove it.

Mole would like to remove some of the edges so that there is exactly one path from Vertex 1 to Vertex N that does not visit the same vertex more than once. Find the minimum budget needed to achieve this.

Constraints

2 \leq N \leq 15

N-1 \leq M \leq N(N-1)/2

1 \leq a_i, b_i \leq N

1 \leq c_i \leq 10^{6}

There are neither multiple edges nor self-loops in the given graph.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
:
a_M b_M c_M

Output

Print the answer.

Sample Input 1

4 6
1 2 100
3 1 100
2 4 100
4 3 100
1 4 100
3 2 100

Sample Output 1

200

By removing the two edges represented by the red dotted lines in the figure below, the objective can be achieved for a cost of 200 yen.

Sample Input 2

2 1
1 2 1

Sample Output 2

0

It is possible that there is already only one path from Vertex 1 to Vertex N in the beginning.

Sample Input 3

15 22
8 13 33418
14 15 55849
7 10 15207
4 6 64328
6 9 86902
15 7 46978
8 14 53526
1 2 8720
14 12 37748
8 3 61543
6 5 32425
4 11 20932
3 12 55123
8 2 45333
9 12 77796
3 9 71922
12 15 70793
2 4 25485
11 6 1436
2 7 81563
7 11 97843
3 1 40491

Sample Output 3

133677

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
struct SubsetGenerator {
    subset: usize,
    set: usize,
}
#[allow(dead_code)]
impl SubsetGenerator {
    fn new(set: usize) -> SubsetGenerator {
        SubsetGenerator {
            subset: set & (-(set as i64) as usize),
            set: set,
        }
    }
}
impl Iterator for SubsetGenerator {
    type Item = usize;

    fn next(&mut self) -> Option<usize> {
        if self.subset == 0 {
            return None;
        }
        let ret = self.subset;
        self.subset = (self.subset as i64 - self.set as i64) as usize & self.set;
        Some(ret)
    }
}

#[test]
fn test_next_subset() {
    for s in SubsetGenerator::new(0b1101) {
        println!("{:04b}", s);
    }
    let mut iterator = SubsetGenerator::new(0b1101);
    assert!(iterator.next().unwrap() == 0b0001);
    assert!(iterator.next().unwrap() == 0b0100);
    assert!(iterator.next().unwrap() == 0b0101);
    assert!(iterator.next().unwrap() == 0b1000);
    assert!(iterator.next().unwrap() == 0b1001);
    assert!(iterator.next().unwrap() == 0b1100);
    assert!(iterator.next().unwrap() == 0b1101);
    assert!(iterator.next() == None);
}

struct Solver {
    matrix: Vec<Vec<i32>>,
    memo: Vec<Vec<i32>>,
    border: Vec<i32>,
}

impl Solver {
    fn new() -> Solver {
        let n = read::<usize>();
        let m = read::<usize>();
        let mut matrix = vec![vec![-1; n]; n];
        for _ in 0..m {
            let a = read::<usize>() - 1;
            let b = read::<usize>() - 1;
            let c = read::<i32>();
            matrix[a][b] = c;
            matrix[b][a] = c;
        }
        let mut border = vec![0; 1 << n];
        for group in 0..1 << n {
            let mut sum = 0;
            for i in 0..n {
                for j in i + 1..n {
                    if ((group >> i) & 1) == ((group >> j) & 1) {
                        continue;
                    }
                    if matrix[i][j] == -1 {
                        continue;
                    }
                    sum += matrix[i][j];
                }
            }
            border[group] = sum;
        }
        Solver {
            matrix: matrix,
            memo: vec![vec![-1; 1 << n]; n],
            border: border,
        }
    }
    fn calc(&mut self, from: usize, rest: usize) -> i32 {
        let n = self.matrix.len();
        if from == n - 1 {
            return self.border[rest | (1 << from)];
        }
        if self.memo[from][rest] != -1 {
            return self.memo[from][rest];
        }
        let mut ret = 1 << 30;
        for to in 0..n {
            if self.matrix[from][to] == -1 || ((rest >> to) & 1) == 0 {
                continue;
            }
            for group in SubsetGenerator::new(rest ^ (1 << to)).chain(0..1) {
                let nrest = rest ^ (1 << to) ^ group;
                let mut nret = self.border[group | (1 << from)];
                nret -= self.matrix[from][to] * 2;
                nret += self.calc(to, nrest);
                ret = std::cmp::min(ret, nret);
            }
        }
        self.memo[from][rest] = ret;
        ret
    }
    fn solve(&mut self) {
        let n = self.matrix.len();
        let rest = ((1 << n) - 1) ^ 1;
        let ans = self.calc(0, rest) / 2;
        println!("{}", ans);
    }
}

fn main() {
    let mut solver = Solver::new();
    solver.solve();
}

#[test]
fn max_case() {
    let mut solver = Solver {
        matrix: vec![vec![1; 15]; 15],
        memo: vec![vec![-1; 1 << 15]; 15],
        border: vec![1; 1 << 15],
    };
    solver.solve();
}