This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
const ll inf = 1e15;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, m, k;
std::cin >> n >> m >> k;
std::vector<std::vector<std::pair<ll, ll>>> adj(n + 1);
for (ll i = 0, u, v, w; i < m; ++i) {
std::cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
std::vector<ll> A(k);
for (auto &i : A) {
std::cin >> i;
}
auto sssp = [&](ll x) {
std::priority_queue<std::pair<ll, ll>> pq;
pq.push({0, x});
std::vector<ll> ans(n + 1, inf);
std::vector<bool> vis(n + 1);
ans[x] = 0;
while (!pq.empty()) {
ll node = pq.top().second, d = -pq.top().first;
pq.pop();
if (vis[node]) {
continue;
}
vis[node] = true;
for (auto &[i, dist] : adj[node]) {
if (d + dist < ans[i]) {
ans[i] = d + dist;
pq.push({-ans[i], i});
}
}
}
return ans;
};
std::vector<std::vector<ll>> dist(n + 1);
std::vector<std::multiset<ll>> sets(n + 1);
for (ll i = 1; i <= n; ++i) {
dist[i] = sssp(i);
for (auto &d : dist[i]) {
sets[i].insert(d);
}
sets[i].erase(sets[i].find(0));
}
ll ans = inf;
for (ll p_a = 0; p_a < k; ++p_a) {
for (ll p_b = p_a + 1; p_b < k; ++p_b) {
for (ll p_c = p_b + 1; p_c < k; ++p_c) {
ll a = A[p_a], b = A[p_b], c = A[p_c];
sets[c].erase(sets[c].find(dist[c][a]));
sets[c].erase(sets[c].find(dist[c][b]));
if (!sets[c].empty()) {
ans = std::min(ans, dist[a][b] + *sets[c].begin());
}
sets[c].insert(dist[c][a]);
sets[c].insert(dist[c][b]);
}
}
}
std::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... |