#include <climits>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
struct edge {
int to;
ll weight;
bool operator >(edge const& rhs) const {
return weight > rhs.weight;
}
};
vector<ll> dijkstra(vector<vector<edge>> const& graph, int const start) {
priority_queue<edge, vector<edge>, greater<edge>> pq;
vector<ll> dist(graph.size(), LLONG_MAX / 3);
dist[start] = 0;
pq.push({ start, 0 });
while (!pq.empty()) {
auto const [cur, cur_dist] = pq.top();
pq.pop();
if (cur_dist != dist[cur])
continue;
for (auto const& [neigh, weight] : graph[cur]) {
ll const new_dist = cur_dist + weight;
if (new_dist < dist[neigh]) {
dist[neigh] = new_dist;
pq.push({ neigh, new_dist });
}
}
}
return dist;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k, m;
cin >> n >> k >> m;
vector<int> important(k);
for (int& x : important) {
cin >> x;
x--;
}
vector<vector<edge>> graph(n);
for (int i = 0; i < m; i++) {
int u, v;
ll w;
cin >> u >> v >> w;
u--; v--;
graph[u].push_back({ v, w });
graph[v].push_back({ u, w });
}
vector<vector<ll>> dist(k);
for (int i = 0; i < k; i++)
dist[i] = dijkstra(graph, important[i]);
if (k == 2) {
cout << dist[0][important[1]] << "\n";
}
else {
ll min_dist = LLONG_MAX / 3;
for (int i = 0; i < n; i++) {
ll const cur_dist = dist[0][i] + dist[1][i] + dist[2][i];
min_dist = min(min_dist, cur_dist);
}
cout << min_dist << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
17248 KB |
Output is correct |
2 |
Correct |
241 ms |
17668 KB |
Output is correct |
3 |
Correct |
86 ms |
10360 KB |
Output is correct |
4 |
Correct |
55 ms |
9400 KB |
Output is correct |
5 |
Correct |
252 ms |
16352 KB |
Output is correct |
6 |
Correct |
56 ms |
9348 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
329 ms |
17916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
330 ms |
18740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |