#include <bits/stdc++.h>
typedef long long ll;
const ll inf = 1e15;
struct Edge {
ll u, v, w;
};
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);
std::vector<Edge> edges;
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});
edges.push_back({u, v, w});
}
std::vector<ll> A(k);
std::priority_queue<std::pair<ll, std::pair<ll, ll>>> pq;
std::vector<bool> special(n + 1);
for (auto &i : A) {
std::cin >> i;
special[i] = true;
}
auto get_best = [&]() {
std::vector<ll> d(n + 1, inf);
for (auto &i : A) {
special[i] = true;
pq.push({0, {i, i}});
d[i] = 0;
}
std::vector<std::pair<ll, ll>> closest_special(n + 1);
std::vector<bool> vis(n + 1);
while (!pq.empty()) {
ll node = pq.top().second.first, source = pq.top().second.second,
dist = -pq.top().first;
pq.pop();
if (vis[node]) {
continue;
}
vis[node] = true;
closest_special[node] = {source, dist};
for (auto &[i, i_d] : adj[node]) {
if (dist + i_d < d[i]) {
d[i] = dist + i_d;
pq.push({-d[i], {i, source}});
}
}
}
std::pair<ll, std::pair<ll, ll>> ans = {inf, {0, 0}};
for (auto &[u, v, w] : edges) {
if (special[u] and special[v]) {
ans = std::min(ans, {w, {u, v}});
}
}
for (auto &[u, v, w] : edges) {
if (closest_special[u].first != closest_special[v].first) {
ans = std::min(
ans, {closest_special[u].second + w + closest_special[v].second,
{closest_special[u].first, closest_special[v].first}});
}
}
return ans;
};
auto best = get_best();
auto &[u, v] = best.second;
special[u] = special[v] = false;
A.erase(std::remove(A.begin(), A.end(), u), A.end());
A.erase(std::remove(A.begin(), A.end(), v), A.end());
auto second_best = get_best();
ll ans = best.first + second_best.first;
ll f = inf, s = inf;
for (auto &[x, w] : adj[u]) {
if (x != v and special[x]) {
f = std::min(f, w);
}
}
for (auto &[y, w] : adj[v]) {
if (y != u and special[y]) {
s = std::min(s, w);
}
}
if (f != inf and s != inf) {
ans = std::min(ans, best.first + f + s);
}
std::cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
10752 KB |
Output is correct |
2 |
Correct |
5 ms |
5724 KB |
Output is correct |
3 |
Correct |
730 ms |
211264 KB |
Output is correct |
4 |
Correct |
342 ms |
91380 KB |
Output is correct |
5 |
Correct |
81 ms |
20736 KB |
Output is correct |
6 |
Correct |
68 ms |
17920 KB |
Output is correct |
7 |
Correct |
99 ms |
22844 KB |
Output is correct |
8 |
Correct |
34 ms |
12044 KB |
Output is correct |
9 |
Correct |
65 ms |
15616 KB |
Output is correct |
10 |
Correct |
45 ms |
13008 KB |
Output is correct |
11 |
Correct |
815 ms |
209732 KB |
Output is correct |
12 |
Correct |
51 ms |
13164 KB |
Output is correct |
13 |
Correct |
213 ms |
54328 KB |
Output is correct |
14 |
Correct |
103 ms |
22780 KB |
Output is correct |
15 |
Correct |
767 ms |
210692 KB |
Output is correct |
16 |
Correct |
24 ms |
10000 KB |
Output is correct |
17 |
Correct |
554 ms |
138632 KB |
Output is correct |
18 |
Correct |
4 ms |
5780 KB |
Output is correct |
19 |
Correct |
822 ms |
210032 KB |
Output is correct |
20 |
Correct |
93 ms |
21764 KB |
Output is correct |
21 |
Correct |
82 ms |
21244 KB |
Output is correct |
22 |
Correct |
42 ms |
12812 KB |
Output is correct |
23 |
Correct |
13 ms |
8464 KB |
Output is correct |
24 |
Correct |
513 ms |
146132 KB |
Output is correct |
25 |
Correct |
68 ms |
15884 KB |
Output is correct |
26 |
Correct |
33 ms |
11008 KB |
Output is correct |
27 |
Correct |
55 ms |
14084 KB |
Output is correct |
28 |
Correct |
8 ms |
7144 KB |
Output is correct |
29 |
Correct |
97 ms |
22012 KB |
Output is correct |
30 |
Correct |
205 ms |
42708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |