#include <bits/stdc++.h>
#define SPED \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define fi first
#define se second
#define lint long long
#define fami signed
#define lore main
#define freefire freopen
const lint INF = 1e15;
using namespace std;
int n, k, m;
vector<pair<int, lint>> adj[100005];
lint dp[100005][(1 << 5) + 5];
fami lore()
{
memset(dp, 0x1f, sizeof dp);
SPED;
cin >> n >> k >> m;
for (int i = 0; i < k; i++)
{
int u;
cin >> u;
dp[u][1 << i] = 0;
}
for (int i = 1; i <= m; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
for (int mask = 1; mask < (1 << k); mask++)
{
priority_queue<pair<lint, lint>, vector<pair<lint, lint>>, greater<pair<lint, lint>>> pq;
for (int i = 1; i <= n; i++)
{
for (int sub = mask; sub > 0; sub = (sub - 1) & mask)
{
dp[i][mask] = min(dp[i][mask], dp[i][sub] + dp[i][mask ^ sub]);
}
if (dp[i][mask] < INF)
pq.emplace(dp[i][mask], i);
}
while (!pq.empty())
{
auto [cur, u] = pq.top();
pq.pop();
if (cur > dp[u][mask])
continue;
for (auto [v, w] : adj[u])
{
if (dp[v][mask] > cur + w)
{
dp[v][mask] = cur + w;
pq.emplace(dp[v][mask], v);
}
}
}
}
lint ans = INF;
for (int i = 1; i <= n; i++)
ans = min(ans, dp[i][(1 << k) - 1]);
cout << ans;
}
// Let your soul wander where dreams are born.
# | 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... |