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

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<stdbool.h>
#include<assert.h>
typedef long long ll;
typedef long double ld;
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define rrep(i,l,r)for(ll i=(l);i>=(r);i--)
#define INF (1LL<<60)
#define MOD1 1000000007
#define MOD2 998244353
#define MAX_N (1 << 17)
#define YES printf("Yes\n")
#define NO printf("No\n")
#define PN printf("\n")
#define charsize 100005 //10^5+5

void swap(ll *a, ll *b){ll c;c=*b;*b=*a;*a= c;}
ll max2(ll a,ll b){return a>=b?a:b;}
ll min2(ll a,ll b){return a>=b?b:a;}
ll min3(ll a, ll b, ll c){return (a<=b && a<=c) ? a : b<=c ? b : c;}
ll max3(ll a, ll b, ll c){return (a>=b && a>=c) ? a : b>=c ? b : c;}
ll minn(ll n, ll a[n]){ll b=INF;rep(i,0,n) b=min2(b,a[i]);return b;}
ll maxn(ll n, ll a[n]){ll b=-INF;rep(i,0,n) b=max2(b,a[i]);return b;}
ll ABS(ll a){return a>=0?a:(-a);}
ll POW(ll a, ll b){ll c=1;rep(i,0,b) c*=a;return c;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll mod(ll n){n+= n<0?((-n)/MOD1+1)*MOD1:0; return n%=MOD1;}
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}

typedef struct{ ll a , b;}fr;
int cmp1( const void *p, const void *q ) { return ((fr*)p) ->a - ((fr*)q)->a;}
int cmp2( const void *p, const void *q ) { return ((fr*)q) ->a - ((fr*)p)->a;}
void strsortup(fr*a,int n){qsort(a,n,sizeof(fr),cmp1);}
void strsortdown(fr*a,int n){qsort(a,n,sizeof(fr),cmp2);}

ll dist[300][300];
void WarshallFloyd(ll n){
    rep(k,0,n){
        rep(i,0,n){
            if(i==k) continue;
            rep(j,0,n){
                dist[i][j] = min2(dist[i][j],dist[i][k]+dist[k][j]);
            }
        }
    }
}

/*階乗 a!*/
ll factorial(ll a){return a==0?1:a*factorial(a-1);}
ll permutation(ll n, ll a[n], ll p){
    rrep(i,n-1,1){
        // rep(j,0,n) printf("%lld ",a[j]);
        // printf("%lld ",p);
        // PN;
        if(p/factorial(i)==0){
            continue;
        }
        swap( &a[n-i-1] , &a[ (n-i-1) + p/factorial(i) ]);
        ll b[i];
        p%=factorial(i);
        rep(j,0,i){
            b[j] =  a[n-i+j];
        }
        sortup(b,i);
        rep(j,0,i){
            a[n-i+j] = b[j];
        }
    }
    ll ans=0;
    rep(i,0,n){
        ans+=a[i]*POW(10,n-1-i);
    }
    return ans;
}

/*nをa進数にしたときの、i桁目の数*/
ll a_base2(ll n, ll a, ll i){return i==1?n%a:a_base2(n/a,a,i-1);}

int main(void){
    // char ;
    ll n,r,m,ans=INF;
    scanf("%lld%lld%lld",&n,&m,&r);
    ll town[r],a[m],b[m],c[m];

    rep(i,0,n) dist[i][i]=0;
    rep(i,0,n){
        rep(j,0,n){
            if(i!=j) dist[i][j]=INF;
        }
    }

    rep(i,0,r){
        scanf("%lld",&town[i]);
    }
    rep(i,0,m){
        scanf("%lld%lld%lld",&a[i],&b[i],&c[i]);
        dist[a[i]-1][b[i]-1]=c[i];
        dist[b[i]-1][a[i]-1]=c[i];
    }

    WarshallFloyd(n);
    // PN;
    // rep(i,0,n){
    //     rep(j,0,n){
    //         printf("%lld ",dist[i][j]);
    //     }
    //     PN;
    // }
    // PN;

    ll num[r];

    rep(i,0,factorial(r)){
        rep(j,0,r) num[j]=j+1;
        ll order=permutation(r,num,i),distsum=0;
        // printf("%lld\n",order);
        rep(j,0,r-1){
            // printf("%lld ",a_base2(order,10,r-j)-1);
            distsum+=dist[ town[ a_base2(order,10,r-j)-1 ]-1 ][ town[ a_base2(order,10,r-j-1)-1 ]-1 ];
            // printf("%lld ",dist[ town[ a_base2(order,10,r-j)-1 ] ][ town[ a_base2(order,10,r-j-1)-1 ] ]);
        }
        // printf("%lld\n",distsum);
        ans= min2(ans,distsum);
    }



    
    printf("%lld\n",ans);
    return 0;
}