This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 2e5+5, inf=1e18;
ll n,k,m,dist[6][6], im[6], d[maxn],dp[(1 << 5)][maxn];
vector<pair<ll,ll>> G[maxn];
priority_queue<pll, vector<pll>, greater<pll>> pq;
bool vis[maxn];
void dijkstra(ll s){
    f1(i,n) d[i] = inf;
    d[s] = 0ll;
    pq.push({d[s], s});
    while(!pq.empty()){
        pair<ll,ll> item = pq.top();
        pq.pop();
        ll dist = item.first, u = item.second;
        if(dist != d[u]) continue;
        vis[u] = true;
        for(pair<ll,ll> e: G[u]){
            ll c = e.first, w = e.second;
            if(d[u] + w < d[c]){
                d[c] = d[u] + w;
                pq.push({d[c], c});
            }
        }
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp","r",stdin);
        freopen(__file_name ".out","w",stdout);
    }
    // code here
    cin >> n >> k >> m;
    for(ll i=0;i<(1 << k);i++){
        f1(u, n) dp[i][u] = inf;
    }
    f1(i,k) {
        cin >> im[i];
        dp[1 << (i-1)][im[i]] = 0;
    }
    f1(i,m){
        ll u, v, w; cin >> u >> v >> w;
        G[u].push_back({v, w});
        G[v].push_back({u, w});
    }
    for(ll mask=1;mask<(1 << k);mask++){
        // if(__builtin_popcount(mask) == 1) continue;
        for(ll submask=mask&(mask-1);submask;submask=mask&(submask-1)){
            f1(u, n){
                dp[mask][u] = min(dp[mask][u], dp[submask][u] + dp[mask ^ submask][u]);
            }
        }
        f1(u,n) pq.push({dp[mask][u], u});
        while(!pq.empty()){
            pair<ll,ll> item = pq.top();
            pq.pop();
            ll dist = item.first, u = item.second;
            // cout << dist << ' ' << u << ' ' << mask << ' ' << dp[mask][u];el;
            if(dist > dp[mask][u]) continue;
            // vis[u] = true;
            for(pair<ll,ll> e: G[u]){
                ll c = e.first, w = e.second;
                if(dp[mask][u] + w < dp[mask][c]){
                    dp[mask][c] = dp[mask][u] + w;
                    pq.push({dp[mask][c], c});
                }
            }
        }
    }
    cout << *min_element(dp[(1 << k) - 1]+1, dp[(1 << k) - 1] + n + 1);
    return 0;
}
Compilation message (stderr)
cities.cpp: In function 'int main()':
cities.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen(__file_name ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen(__file_name ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |