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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>

using namespace std;
const int bitmaskMAXN = (1<<17);
const int MAXN = 17;

int mat[MAXN][MAXN];
int dp[bitmaskMAXN][MAXN];
int cost[bitmaskMAXN][MAXN];

int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int x,y,w;
        cin>>x>>y>>w;
        x--;
        y--;
        mat[x][y] = w;
        mat[y][x] = w;
    }
    for(int i=0;i<(1<<n);i++){
        for(int x=0;x<n;x++){
            dp[i][x] = (1<<30);
            for(int y = 0;y<n;y++){
                if(i & (1<<y)){
                    cost[i][x] +=mat[x][y];
                    //cost[i][x] = cost of all edges between nodes in mask and x
                }
            }
        }
    }
    dp[1][0] = 0;
    for(int mask =0;mask<(1<<n);mask++){
        for(int x=0;x<n;x++){
            if((mask & (1<<x)) && dp[mask][x]<(1<<30)){
                for(int y=0;y<n;y++){
                    if(mat[x][y] && (mask&(1<<y))==0){
                        //if there's an edge between x and y, and y does not lies in mask
                        int newcost = cost[mask][y]-mat[x][y];
                        //newcost = cost of all edges between y and edges in mask - cost of (x,y) (cost to remove all edges except (x,y)
                        dp[mask|(1<<y)][y] = min(dp[mask|(1<<y)][y],dp[mask][x]+newcost);
                    }
                }
                int nmask = mask^(1<<x);
                //nmask = mask with vertices in mask except x
                int left = ((1<<n)-1)^mask;
                //left = set of vertices not in mask
                for(int i=left;i>0;i=(i-1)&left){
                    //i = mask which only has vertices in the set with no vertices from mask
                    int newcost = 0;
                    for(int y=0;y<n;y++){
                        if(i&(1<<y)){
                            newcost+=cost[nmask][y];
                            //newcost = cost to remove edges connecting nodes in in i and nodes in mask(except for node x itself)
                        }
                    }
                    dp[mask|i][x] = min(dp[mask|i][x],dp[mask][x]+newcost);
                    //extends the current set of vertices
                }
            }
        }
    }
    cout<<dp[(1<<n)-1][n-1]<<endl;
}