#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, k, m;
cin >> n >> k >> m;
vector<int> t(k);
for(int i = 0; i < k; i++) cin >> t[i];
vector<vector<pair<int,int>>> adj(n+1);
for(int i = 0; i < m; i++){
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
int full = (1 << k);
vector<vector<ll>> dp(full, vector<ll>(n+1, INF));
for(int i = 0; i < k; i++) dp[1<<i][t[i]] = 0;
for(int mask = 1; mask < full; mask++){
if((mask & (mask - 1))){
for(int sub = (mask - 1) & mask; sub; sub = (sub - 1) & mask){
int other = mask ^ sub;
for(int v = 1; v <= n; v++){
ll c = dp[sub][v] + dp[other][v];
if(c < dp[mask][v]) dp[mask][v] = c;
}
}
}
// Dijkstra
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
for(int v = 1; v <= n; v++){
if(dp[mask][v] < INF) pq.emplace(dp[mask][v], v);
}
while(!pq.empty()){
auto [d, u] = pq.top(); pq.pop();
if(d != dp[mask][u]) continue;
for(auto &e: adj[u]){
int v = e.first;
ll nd = d + e.second;
if(nd < dp[mask][v]){
dp[mask][v] = nd;
pq.emplace(nd, v);
}
}
}
}
ll ans = INF;
for(int v = 1; v <= n; v++) ans = min(ans, dp[full-1][v]);
cout << ans;
return 0;
}
# | 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... |