Problem:
Score : 400 points

Problem Statement

There are N towns in the State of Atcoder, connected by M bidirectional roads.

The i-th road connects Town A_i and B_i and has a length of C_i.

Joisino is visiting R towns in the state, r_1,r_2,..,r_R (not necessarily in this order).

She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.

If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?

Constraints

2≤N≤200

1≤M≤N×(N-1)/2

2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)

r_i≠r_j (i≠j)

1≤A_i,B_i≤N, A_i≠B_i

(A_i,B_i)≠(A_j,B_j),(A_i,B_i)≠(B_j,A_j) (i≠j)

1≤C_i≤100000

Every town can be reached from every town by road.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M R
r_1 ... r_R
A_1 B_1 C_1
:
A_M B_M C_M

Output

Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.

Sample Input 1

3 3 3
1 2 3
1 2 1
2 3 1
3 1 4

Sample Output 1

2

For example, if she visits the towns in the order of 1, 2, 3, the distance traveled will be 2, which is the minimum possible.

Sample Input 2

3 3 2
1 3
2 3 2
1 3 6
1 2 2

Sample Output 2

4

The shortest distance between Towns 1 and 3 is 4. Thus, whether she visits Town 1 or 3 first, the distance traveled will be 4.

Sample Input 3

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

Sample Output 3

3

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

namespace Equeue
{
    
    
    class Equeue
    {
      static Node[] Nodes;
        static void Main(string[] args)
        {
            long nil = 10000000;
            //var N = long.Parse(Console.ReadLine());
            var NMR = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
            var N = NMR[0];
            var M = NMR[1];
            var R = NMR[2];
          
            var r = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
            
            //それぞれの町への最短経路を格納するマップ
            var map = new long[N+1,N+1];
          　　
          　for(int i = 1;i <= N;i++){
             for(int j = 1;j <= N;j++){
               if(i != j){
                 map[i,j] = nil;
               }
             }
            }
            
            for(int i = 0;i < M;i++){
              var ABC = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
              var A = ABC[0];
              var B = ABC[1];
              var C = ABC[2];
              map[A,B] = C;
              map[B,A] = C;
            }
            
            //ワーシャルフロイドで最短経路を求める
            for (int k = 1; k <= N; k++)
            {
                for (int i = 1; i <= N; i++)
                {
                    for (int j = 1; j <= N; j++)
                    {
                        map[i, j] = Math.Min(map[i, j], map[i, k] + map[k, j]);
                    }
                }
            }
          
            //rの全ての順列集合を求める
            Array.Sort(r); //Allpermutuationの準備
            var lis =  AllPermutation(r);
          
            var ans = long.MaxValue;
            foreach(var item in lis){
              var sum = 0L;
              for(int end = 1; end < R;end++){
                sum += map[item[end-1],item[end]];
              }
              ans = Math.Min(ans,sum);
            }
            Console.WriteLine(ans);
        }
      
        static List<T[]> AllPermutation<T>(params T[] array) where T : IComparable
        {
            var a = new List<T>(array).ToArray();
            var res = new List<T[]>();
            res.Add(new List<T>(a).ToArray());
            var n = a.Length;
            var next = true;
            while (next)
            {
                next = false;

                // 1
                int i;
                for (i = n - 2; i >= 0; i--)
                {
                    if (a[i].CompareTo(a[i + 1]) < 0) break;
                }
                // 2
                if (i < 0) break;

                // 3
                var j = n;
                do
                {
                    j--;
                } while (a[i].CompareTo(a[j]) > 0);

                if (a[i].CompareTo(a[j]) < 0)
                {
                    // 4
                    var tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;
                    Array.Reverse(a, i + 1, n - i - 1);
                    res.Add(new List<T>(a).ToArray());
                    next = true;
                }
            }
            return res;
        }
      

      
      
        static bool WFS(long st){
          var q = new Queue<Node>();
          var tmp = Nodes[st];
          tmp.loc = 100;
          q.Enqueue(tmp);
          
          while(q.Count > 0){
            tmp = q.Dequeue();
            foreach(KeyValuePair<long,long> item in tmp.adj) {
              var child = Nodes[item.Key];
              //Console.WriteLine(item.Key+" "+child.loc+" "+item.Value);
              if(child.loc != -1 && (child.loc != (item.Value+tmp.loc))){
                return false;
              }else if(child.loc == -1){
                child.loc = item.Value+tmp.loc;
                q.Enqueue(Nodes[item.Key]);
                //Console.WriteLine(item.Key+" "+child.loc+" "+item.Value);
              }
              
            }
          }
          return true;
        }      
      
    }
  


    public class Node{
      public long loc = -1;
      public Dictionary<long,long> adj = new Dictionary<long,long>();
    }
  
  
    public class PriorityQueue<T> : IEnumerable<T>
    {
        private readonly List<T> _data = new List<T>();
        private readonly IComparer<T> _comparer;
        private readonly bool _isDescending;

        public PriorityQueue(IComparer<T> comparer, bool isDescending = true)
        {
            _comparer = comparer;
            _isDescending = isDescending;
        }

        public PriorityQueue(Comparison<T> comparison, bool isDescending = true)
            : this(Comparer<T>.Create(comparison), isDescending)
        {
        }

        public PriorityQueue(bool isDescending = true)
            : this(Comparer<T>.Default, isDescending)
        {
        }

        public void Enqueue(T item)
        {
            _data.Add(item);
            var childIndex = _data.Count - 1;
            while (childIndex > 0)
            {
                var parentIndex = (childIndex - 1) / 2;
                if (Compare(_data[childIndex], _data[parentIndex]) >= 0)
                    break;
                Swap(childIndex, parentIndex);
                childIndex = parentIndex;
            }
        }

        public T Dequeue()
        {
            var lastIndex = _data.Count - 1;
            var firstItem = _data[0];
            _data[0] = _data[lastIndex];
            _data.RemoveAt(lastIndex--);
            var parentIndex = 0;
            while (true)
            {
                var childIndex = parentIndex * 2 + 1;
                if (childIndex > lastIndex)
                    break;
                var rightChild = childIndex + 1;
                if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0)
                    childIndex = rightChild;
                if (Compare(_data[parentIndex], _data[childIndex]) <= 0)
                    break;
                Swap(parentIndex, childIndex);
                parentIndex = childIndex;
            }
            return firstItem;
        }

        public T Peek()
        {
            return _data[0];
        }

        private void Swap(int a, int b)
        {
            var tmp = _data[a];
            _data[a] = _data[b];
            _data[b] = tmp;
        }

        private int Compare(T a, T b)
        {
            return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b);
        }

        public int Count => _data.Count;

        public IEnumerator<T> GetEnumerator()
        {
            return _data.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }
}