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 Rust (1.15.1) (judge verdict: Accepted)

use std::io;
use std::cmp;
use std::cmp::Ordering;
use std::collections::BinaryHeap;

// read a line(without line feed)
fn read_line() -> String {
    let mut in_str = String::new();
    io::stdin().read_line(&mut in_str).unwrap();
    return in_str.trim().to_string();
}

// read wods on a line
pub fn read_words_in_line() -> Vec<String> {
    let s = read_line();
    return s.split_whitespace().map(|x| x.to_string()).collect::<Vec<String>>()
}

// read type t on a line
pub fn read_t_in_line<T : std::str::FromStr>() -> Vec<T> {
    let mut v : Vec<T> = Vec::new();
    for e in read_words_in_line().iter().map(|x| x.parse::<T>()) {
        if let Ok(r) = e {
            v.push(r);
        }
        else{
            return Vec::new();
        }
    }
    return v;
}

// sort slice
pub fn sort<T : std::cmp::Ord>(slice : &mut [T]) {
    slice.sort();
}
// sort slice by reverse order
pub fn sort_rev<T : std::cmp::Ord>(slice : &mut [T]) {
    slice.sort_by(|x, y| y.cmp(x));
}

pub fn min<T : std::cmp::Ord>(a : T, b : T) -> T{
    return cmp::min(a, b);
}

pub fn max<T : std::cmp::Ord>(a : T, b : T) -> T{
    return cmp::max(a, b);
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
struct State {
    cost     : i32,
    node     : i32,
    vismask  : i32
}

impl Ord for State {
    fn cmp(&self, other: &State) -> Ordering {
        other.cost.cmp(&self.cost)
    }
}

impl PartialOrd for State {
    fn partial_cmp(&self, other : &State) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}


fn main() {

    let n_m_r = read_t_in_line::<i32>();
    let (n, m, r) = (n_m_r[0], n_m_r[1], n_m_r[2]);
    let towns : Vec<_> = read_t_in_line::<i32>().iter().map(|x| x - 1).collect();
    let mut g = vec![vec![] ; n as usize];

    for _ in 0..m {
        let a_b_c = read_t_in_line::<i32>();
        let f = a_b_c[0] - 1;
        let t = a_b_c[1] - 1;
        let c = a_b_c[2];
        g[f as usize].push((t, c));
        g[t as usize].push((f, c));
    }
    
    
    let mut dist : Vec<Vec<_>> = vec![(0..(1 << r)).map(|_| i32::max_value()).collect() ; n as usize];
    let mut pq = BinaryHeap::new();


    for i in 0..n {
        let mut vis = 0;
        for j in 0..r {
            let town = towns[j as usize];
            if town == i {
                vis |= 1 << j;
                break;
            }
        }
        dist[i as usize][vis] = 0;
        pq.push(State{cost : 0, node : i, vismask : vis as i32} );
    }

    
    while let Some(State{cost, node, vismask}) = pq.pop() {
        if cost > dist[node as usize][vismask as usize] {
            continue;
        }

        // println!("*");
        // println!("{} {} {}", cost, node, vismask);

        for &(nnode, rcost) in &g[node as usize] {
            let mut nvismask = vismask;
            for j in 0..r {
                let town = towns[j as usize];
                if town == nnode {
                    nvismask |= 1 << j;
                    break;
                }
            }
            // println!("{} {} {}", rcost+cost, nnode, nvismask);

            let nstate = State{cost : rcost + cost, node : nnode, vismask : nvismask};

            if nstate.cost < dist[nnode as usize][nvismask as usize] {
                dist[nnode as usize][nvismask as usize] = nstate.cost;
                pq.push(nstate);
            }
        }
    }
    
    let mut res = i32::max_value();
    // すべてのtownを通過したものから、最短の距離のものを求める
    for i in 0..n {
        let last_vis = (1 << r) - 1;
        let d = dist[i as usize][last_vis as usize];
        res = cmp::min(res, d);
    }
    println!("{}", res);
}