#include <bits/stdc++.h>
#define int long long
using namespace std;
using pii = pair<int, int>;
const int N = 1e5 + 5, LOG = 5;
const long long INF = 1e18;
int a[LOG], n, m, k;
long long dp[N][1 << LOG];
vector<pii> adj[N];
signed main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> k >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < (1 << k); j++)
            dp[i][j] = INF;
    for (int i = 0; i < k; i++)
    {
        cin >> a[i];
        dp[a[i]][1 << i] = 0;
    }
    for (int i = 1; i <= m; i++)
    {
        int x, y, z;
        cin >> x >> y >> z;
        adj[x].push_back({y, z});
        adj[y].push_back({x, z});
    }
    for (int mask = 0; mask < (1 << k); mask++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int sub = mask; sub; sub = (sub - 1) & mask)
            {
                dp[i][mask] = min(dp[i][mask], dp[i][sub] + dp[i][mask ^ sub]);
            }
        }
        priority_queue<pii, vector<pii>, greater<pii>> pq;
        for (int i = 1; i <= n; i++)
            if (dp[i][mask] < INF)
                pq.push({dp[i][mask], i});
        while (!pq.empty())
        {
            auto [w, u] = pq.top();
            pq.pop();
            if (dp[u][mask] != w)
                continue;
            for (auto [v, cost] : adj[u])
            {
                if (dp[v][mask] > dp[u][mask] + cost)
                {
                    dp[v][mask] = dp[u][mask] + cost;
                    pq.push({dp[v][mask], v});
                }
            }
        }
    }
    long long ans = INF;
    for (int i = 1; i <= n; i++)
        ans = min(ans, dp[i][(1 << k) - 1]);
    cout << ans << "\n";
}
| # | 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... |