#include <algorithm>
#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(n);
for (int i = 0; i < n; i++)
dist[i] = dijkstra(graph, i);
ll min_dist = LLONG_MAX / 3;
for (int a = 0; a < n - 1; a++) {
for (int b = a + 1; b < n; b++) {
// middle vertex (c) must be unrestricted!
for (int c = 0; c < n; c++) {
ll cur_dist = dist[a][c] + dist[c][b];
for (int const& i : important) {
cur_dist += min({ dist[a][i], dist[b][i], dist[c][i] });
}
min_dist = min(min_dist, cur_dist);
}
}
}
cout << min_dist << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6099 ms |
132164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6083 ms |
8200 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6076 ms |
136944 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6057 ms |
134576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |