#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);
std::vector<bool> special(n + 1);
for (auto &i : A) {
std::cin >> i;
special[i] = true;
}
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 (ll j = 1; j <= n; ++j) {
if (i == j or !special[j]) {
continue;
}
sets[i].insert(dist[i][j]);
}
}
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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
648 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
648 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
6 ms |
2396 KB |
Output is correct |
32 |
Correct |
14 ms |
2652 KB |
Output is correct |
33 |
Correct |
19 ms |
2632 KB |
Output is correct |
34 |
Correct |
3 ms |
2648 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
1256 ms |
9392 KB |
Output is correct |
37 |
Execution timed out |
6079 ms |
13704 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4714 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
648 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
6 ms |
2396 KB |
Output is correct |
32 |
Correct |
14 ms |
2652 KB |
Output is correct |
33 |
Correct |
19 ms |
2632 KB |
Output is correct |
34 |
Correct |
3 ms |
2648 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
1256 ms |
9392 KB |
Output is correct |
37 |
Execution timed out |
6079 ms |
13704 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |