#include <bits/stdc++.h>
using namespace std;
void setup()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, k, m, v[5], a, b;
long long f[1 << 5][100000], c;
vector<pair<int, int>> g[100000];
priority_queue<pair<long long, int>> pq;
int main()
{
setup();
cin >> n >> k >> m;
for (int i = 0; i < k; ++i)
{
cin >> v[i];
v[i]--;
}
while (m--)
{
cin >> a >> b >> c;
g[a - 1].push_back({b - 1, c});
g[b - 1].push_back({a - 1, c});
}
for (int i = 1; i < (1 << k); ++i)
{
fill_n(f[i], n, 1e18);
for (int j = 0; j < k; ++j)
{
f[1 << j][v[j]] = 0;
}
for (int j = 0; j < n; ++j)
{
for (int h = 0; h < i; ++h)
{
if ((i & h) == h)
{
f[i][j] = min(f[i][j], f[h][j] + f[i ^ h][j]);
}
}
if (f[i][j] < 1e18)
{
pq.push({-f[i][j], j});
}
}
while (!pq.empty())
{
c = -pq.top().first;
a = pq.top().second;
pq.pop();
if (c != f[i][a])
{
continue;
}
for (auto & j : g[a])
{
if (f[i][j.first] > f[i][a] + j.second)
{
f[i][j.first] = f[i][a] + j.second;
pq.push({-f[i][j.first], j.first});
}
}
}
}
c = 1e18;
for (int i = 0; i < n; ++i)
{
c = min(c, f[(1 << k) - 1][i]);
}
cout << c;
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... |