#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, K, m;
cin >> n >> K >> m;
vector<int> a(K);
for (int i = 0; i < K; i++)
{
cin >> a[i];
a[i]--;
}
vector<vector<pair<int, int>>> g(n);
for (int i = 0; i < m; i++)
{
int u, v, w;
cin >> u >> v >> w;
u--, v--;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
auto dijkstra = [&](int i)
{
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
pq.emplace(0, i);
vector<long long> dis(n, INF);
vector<bool> vis(n);
while (pq.size())
{
auto [d, u] = pq.top();
pq.pop();
if (vis[u])
{
continue;
}
vis[u] = 1;
dis[u] = d;
for (auto &[v, w] : g[u])
{
if (dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
pq.emplace(dis[v], v);
}
}
}
return dis;
};
auto merge = [&](const vector<long long> &b, const vector<long long> &c) -> vector<long long>
{
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
for (int i = 0; i < n; i++)
{
pq.emplace(b[i] + c[i], i);
}
vector<long long> ret(n, INF);
vector<bool> vis(n);
while (pq.size())
{
auto [d, u] = pq.top();
pq.pop();
if (vis[u])
{
continue;
}
vis[u] = 1;
ret[u] = d;
for (auto &[v, w] : g[u])
{
if (ret[v] > ret[u] + w)
{
ret[v] = ret[u] + w;
pq.emplace(ret[v], v);
}
}
}
return ret;
};
sort(a.begin(), a.end());
vector<vector<long long>> dis(K);
for (int i = 0; i < K; i++)
{
dis[i] = dijkstra(a[i]);
}
long long ans = INF;
vector<int> ord(K);
iota(ord.begin(), ord.end(), 0);
set<vector<int>> vis;
do
{
if (vis.find(ord) != vis.end())
{
continue;
}
swap(ord[0], ord[1]);
vis.insert(ord);
swap(ord[0], ord[1]);
vector<long long> res = dis[ord[0]];
for (int i = 1; i < K - 1; i++)
{
res = merge(res, dis[ord[i]]);
}
ans = min(ans, res[a[ord[K - 1]]]);
}
while (next_permutation(ord.begin(), ord.end() - 1));
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
229 ms |
18516 KB |
Output is correct |
2 |
Correct |
199 ms |
19356 KB |
Output is correct |
3 |
Correct |
68 ms |
11424 KB |
Output is correct |
4 |
Correct |
43 ms |
5124 KB |
Output is correct |
5 |
Correct |
112 ms |
12324 KB |
Output is correct |
6 |
Correct |
40 ms |
5388 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
3 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
460 ms |
21492 KB |
Output is correct |
2 |
Correct |
433 ms |
21004 KB |
Output is correct |
3 |
Correct |
237 ms |
13768 KB |
Output is correct |
4 |
Correct |
275 ms |
13284 KB |
Output is correct |
5 |
Correct |
107 ms |
7444 KB |
Output is correct |
6 |
Correct |
47 ms |
6864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1947 ms |
21100 KB |
Output is correct |
2 |
Correct |
1879 ms |
20972 KB |
Output is correct |
3 |
Correct |
1726 ms |
21036 KB |
Output is correct |
4 |
Correct |
1124 ms |
14768 KB |
Output is correct |
5 |
Correct |
1027 ms |
13364 KB |
Output is correct |
6 |
Correct |
308 ms |
7532 KB |
Output is correct |
7 |
Correct |
76 ms |
6868 KB |
Output is correct |