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: Runtime Error)

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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            string read = Console.ReadLine();
            string[] split = read.Split(' ');
            int N = int.Parse(split[0]);
            int M = int.Parse(split[1]);
            int R = int.Parse(split[2]);
            read = Console.ReadLine();
            split = read.Split(' ');
            int[] need = new int[R + 1];
            for (int i = 1; i <= R; i++)
            {
                need[i] = int.Parse(split[i-1]);
            }
            
            int[][] road = new int[M+1][];
            int[][] useroad = new int[M + 1][];
            for (int i = 0; i <=M; i++)
            {
                road[i] = new int[3];
                useroad[i] = new int[3];
            }
            for (int i = 1; i <= M; i++)
            {
                split = Console.ReadLine().Split(' ');
                road[i][0] = int.Parse(split[0]);
                road[i][1] = int.Parse(split[1]);
                road[i][2] = int.Parse(split[2]);
            }
            //準備完了
            int[] group = new int[N + 1];
            //ソート
            Array.Sort(road, (a, b) => a[2] - b[2]);
            int useroad_index = 1;
            for (int i = 1; i < M;i++)
            {

                //ループ開始
                //次に小さいノードが、同じグループに属していない場合、
                if (group[road[i][0]] * group[road[i][1]] == 0 || group[road[i][0]] != group[road[i][1]])
                {
                    int maxgroup = max(group);
                    useroad[useroad_index][0] = road[i][0];
                    useroad[useroad_index][1] = road[i][1];
                    useroad[useroad_index][2] = road[i][2];
                    useroad_index++;
                    //その間を移動するノードをオンにする。
                    int previous_group_0 = group[road[i][0]];
                    int previous_group_1 = group[road[i][1]];
                    group[road[i][0]] = maxgroup + 1;
                    group[road[i][1]] = maxgroup + 1;

                    //：オンにしたノードの両端を、同じグループに突っ込む

                    if (previous_group_0 != 0 || previous_group_1 != 0)
                    {
                        if (previous_group_0 * previous_group_1 != 0)//両方グループに属す
                        {
                            int min = Math.Min(previous_group_0, previous_group_1);//小さい方のグループへ
                            int max = Math.Max(previous_group_0, previous_group_1);
                            for (int t = 0; t <= N; t++)
                            {
                                if (group[t] == max) group[t] = min;

                            }

                        }
                        else
                        {//片方しかグループに属してない、もう片方は野良
                            int max = Math.Max(previous_group_0, previous_group_1);
                            group[road[i][0]] = max;
                            group[road[i][1]] = max;

                        }

                    }
                    //：全てのグループに対して、ノードのかぶりがあるか検査
                    //：かぶりがあった場合、より前に作られたグループに統合する
                    int GoodGroup = 0;
                    bool GO_SIGN = true;
                    for (int temp = 1; temp <= R; temp++)
                    {
                        if (group[need[temp]] == 0)
                        {
                            GO_SIGN = false;
                            break;
                        }
                        if (temp == 1) GoodGroup = group[need[temp]];
                        else if (GoodGroup != group[need[temp]])
                        {
                            GO_SIGN = false;
                            break;
                        }

                    }
                    if (GO_SIGN == true) break;
                    //：もし、必要な町たち(need)を全て網羅するグループがひとつでもある場合、break;

                }
                ;
            }
            int[] n_of_road = new int[N + 1];
            for (int i = 1; i <= useroad_index; i++)
            {
                n_of_road[useroad[i][0]]--;
                n_of_road[useroad[i][1]]--;
            }
            int m = max(n_of_road);
            m = -m;


            //ここから間違いだということはわかっている
            /*　最小全域木
             を出すところまでは出来た
             でも、そこから最短ルートを構成する方法が全くわからない
             とりあえず、少しでも丸がついている方が自分的には嬉しいので、
             こんなものが100%通るわけはないと知りつつ、
             これ以下を書く
             
             */
            int output = 0;
            for (int i = 1; i <= useroad_index; i++)
            {
                output+=n_of_road[useroad[i][2]];
            }
            Console.WriteLine(output);
            


            //ループ終了

        }
        public static int max(int[] group)
        {
            int output = 0;
            for (int i = 0; i < group.Length; i++)
            {
                if (output < group[i]) output = group[i];
            }
            return output;

        }


    }
}