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

using static System.Math;using static System.Console;using System.Collections.Generic;using System.Linq;using System;using System.Collections;

class Program
{
    #region Reader
    static string ReadStr => Console.ReadLine();
    static string[] ReadStrs => Console.ReadLine().Split(' ');
    static int ReadInt => Convert.ToInt32(Console.ReadLine());
    static int[] ReadInts => Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
    static long ReadLong => Convert.ToInt64(Console.ReadLine());
    static long[] ReadLongs => Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
    #endregion
    #region Method
    const int mod = 1000000007;
    public static int Mod(int a,int mod) { return a % mod >= 0 ? a % mod : a % mod + mod; }
    public static long Mod(long a, long mod = mod) { return a % mod >= 0 ? a % mod : a % mod + mod; }
    #endregion


    static void Main()
    {
        int[] VE = ReadInts;
        int V = VE[0];
        int E = VE[1];


        List<int[]>[] roads = new List<int[]>[V];
        for (int i = 0; i < V; i++)
        {
            roads[i] = new List<int[]>();
        }
        for (int i = 0; i < E; i++)
        {
            int[] vs = ReadInts;
            roads[vs[0]].Add(new int[2] { vs[1], vs[2] });
        }

        int infty = 1000000000;
        //コストのbitDP[通った集合,現在の位置]
        int[,] dp = new int[(int)Pow(2, V),V];
        for (int i = 0; i < (int)Pow(2,V); i++)
        {
            for (int j = 0; j < V; j++)
            {
                dp[i, j] = infty;
            }
        }

        dp[0,0] = 0;


        for (int s = 0; s < (int)Pow(2, V); s++)
        {
            int[] bits = ToNSinsu(s, 2, V);
            for (int i = 0; i < V; i++)
            {

                foreach (int[] road in roads[i])
                {
                    if (bits[road[0]] == 1) {
                        continue;
                    }

                    dp[s + (int)Pow(2, road[0]), road[0]] = Min(dp[s + (int)Pow(2, road[0]), road[0]],dp[s, i] + road[1]);
                }
            }
        }

        WriteLine(dp[(int)Pow(2, V) - 1, 0] != infty ? dp[(int)Pow(2, V) - 1, 0] : -1);


    }

    public static int[] ToNSinsu(int i, int baseNum, int digit)
    {
        int[] ans = new int[digit];
        int index = 0;
        while (i >= baseNum)
        {
            int d = i / baseNum;
            int r = i % baseNum;

            ans[index] = r;
            index++;
            i = d;
        }
        ans[index] = i;
        return ans;
    }


}