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: Wrong Answer)

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

public static class Permutation
{
    public static int Factorial(int n)
    {
        if (n == 0)
        {
            return 1;
        }
        return n * Factorial(n - 1);
    }

    public static IEnumerable<int[]> GetPermutations(int n)
    {
        int max = Factorial(n);
        for (int i = 0; i < max; i++)
        {
            yield return GetPermutation(n, i);
        }
    }

    public static int[] GetPermutation(int n, int serialNum)
    {
        var m = new int[n];
        for (int i = 0; i < n; i++)
        {
            m[i] = i;
        }

        var perm = new int[n];
        int cur = 0;
        for (int i = n; i >= 1; i--)
        {
            int nn = serialNum % i;
            perm[cur++] = m[nn];
            for (int j = nn; j < i - 1; j++)
            {
                m[j] = m[j + 1];
            }
            serialNum = serialNum / i;
        }
        return perm;
    }
}

public class PriorityQueue<TKey, TValue>
{
    SortedDictionary<TKey, HashSet<TValue>> dict = new SortedDictionary<TKey, HashSet<TValue>>();
    Dictionary<TValue, TKey> rDict = new Dictionary<TValue, TKey>();
    int count = 0;

    public int Count
    {
        get
        {
            return count;
        }
    }

    public void Enqueue(TKey key, TValue value)
    {
        if (!dict.ContainsKey(key))
        {
            dict[key] = new HashSet<TValue>();
        }

        dict[key].Add(value);
        rDict[value] = key;
        count++;
    }

    public KeyValuePair<TKey, TValue> Dequeue()
    {
        var queue = dict.First();
        if (queue.Value.Count <= 1)
        {
            dict.Remove(queue.Key);
        }

        var val = queue.Value.First();
        queue.Value.Remove(val);
        count--;
        rDict.Remove(val);
        return new KeyValuePair<TKey, TValue>(queue.Key, val);
    }

    public KeyValuePair<TKey, TValue> Peek()
    {
        var queue = dict.First();
        var val = queue.Value.First();
        return new KeyValuePair<TKey, TValue>(queue.Key, val);
    }

    public void ChangePriority(TKey newKey, TValue value)
    {
        ChangePriority(rDict[value], newKey, value);
    }

    public void ChangePriority(TKey prevKey, TKey newKey, TValue value)
    {
        var set = dict[prevKey];
        set.Remove(value);
        if (set.Count == 0)
        {
            dict.Remove(prevKey);
        }
        count--;

        Enqueue(newKey, value);
    }

    public bool ContainsValue(TValue val)
    {
        return rDict.ContainsKey(val);
    }
}


class Solution
{
    static long[] Dijkstra(List<Tuple<int, long>>[] link, int k)
    {
        var n = link.Length - 1;
        var d = Enumerable.Repeat(long.MaxValue, n + 1).ToArray();
        d[k] = 0;
        var q = new PriorityQueue<long, int>();
        q.Enqueue(0, k);

        while (q.Count > 0)
        {
            var u = q.Dequeue();
            foreach (var nei in link[u.Value])
            {
                var v = nei.Item1;
                var len = nei.Item2;
                if (d[v] > d[u.Value] + len)
                {
                    d[v] = d[u.Value] + len;
                    if (q.ContainsValue(v))
                    {
                        q.ChangePriority(d[v], v);
                    }
                    else
                    {
                        q.Enqueue(d[v], v);
                    }
                }
            }
        }

        return d;
    }

    public static long SolveByBruteForce(List<Tuple<int, long>>[] link, IList<int> target)
    {
        long min = long.MaxValue;
        foreach (var pattern in Permutation.GetPermutations(target.Count))
        {
            long d = 0;
            var cur = target[pattern[0]];
            for (int i = 1; i < pattern.Length; i++)
            {
                var next = target[pattern[i]];
                Console.WriteLine($"{cur} {next}");
                d += link[cur].First(l => l.Item1 == next).Item2;
                cur = next;

                if (d >= min)
                {
                    break;
                }
            }
            if (d < min)
            {
                min = d;
            }
        }

        return min;
    }

    static void Main()
    {
        var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var n = vals[0];
        var m = vals[1];
        var rCount = vals[2];
        var r = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

        var link = Enumerable.Range(0, n + 1).Select(_ => new List<Tuple<int, long>>()).ToArray();
        for (int i = 0; i < m; i++)
        {
            var v = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            link[v[0]].Add(Tuple.Create(v[1], (long)v[2]));
            link[v[1]].Add(Tuple.Create(v[0], (long)v[2]));
        }

        foreach (var start in r)
        {
            var distance = Dijkstra(link, start);

            link[start] = new List<Tuple<int, long>>();
            foreach (var end in r)
            {
                if (end != start)
                {
                    link[start].Add(new Tuple<int, long>(end, distance[end]));
                }
            }
            Console.Error.WriteLine(string.Join(" ", link[start]));
        }

        var d = SolveByBruteForce(link, r);
        Console.WriteLine(d);
    }
}