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 System;
using System.Collections.Generic;
using System.Linq;

namespace AOJ_DPL_2_A_2
{
    class Program
    {
        static void Main(string[] args)
        {
            var VE = ReadInt();
            V = VE[0];
            int E = VE[1];

            d = new long[V, V];
            for (int i = 0; i < V; i++)
            {
                for (int j = 0; j < V; j++)
                {
                    d[i, j] = INF;
                }
            }

            for (int i = 0; i < E; i++)
            {
                var input = ReadInt();
                int s = input[0];
                int t = input[1];
                int d2 = input[2];
                d[s, t] = d2;
            }

            //bitDP
            dp = new long[1 << V, V];
            long ans = Tsp(1, 0);

            if (ans != INF) Console.WriteLine(ans);
            else Console.WriteLine(-1);
            Console.ReadKey();
        }

        static int V;   　 //頂点数
        static long[,] d;　//頂点行列 頂点ない部分はlong.MaxValue / 4で初期化
        static long[,] dp; //DP
        static long INF = long.MaxValue / 2;
        /// <summary>
        /// 巡回セールスマン問題
        /// </summary>
        /// <param name="S">bit訪れた場所のbit</param>
        /// <param name="v">現在の頂点</param>
        /// <returns></returns>
        static long Tsp(int S = 1, int v = 0)
        {
            if (dp[S, v] != 0) return dp[S, v];
            if (S == (1 << V) - 1) return d[v, 0];

            long ret = INF;
            for (int i = 0; i < V; i++)
            {
                if ((S & (1 << i)) > 0) continue;
                if (d[v, i] == INF) continue;
                ret = Math.Min(ret, d[v, i] + Tsp(S | (1 << i), i));
            }

            return dp[S, v] = ret;
        }

        static int[] ReadInt()
        {
            int[] ret = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return ret;
        }
        static long[] ReadLong()
        {
            long[] ret = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return ret;
        }
    }
}