#include <bits/stdc++.h>
using namespace std;
template<typename T>
bool minimize(T& a, const T& b){
if(a > b){
return a = b, true;
} return false;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, K, M;
cin >> N >> K >> M;
vector<int> cities(K);
for(int i = 0; i < K; ++i){
cin >> cities[i];
--cities[i];
}
vector<vector<pair<int, int>>> adj(N);
while(M--){
int u, v, w;
cin >> u >> v >> w;
--u, --v;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
const long long inf = 1e18;
using node = tuple<long long, int, int>;
priority_queue<node, vector<node>, greater<node>> pq;
vector<vector<long long>> d(1 << K, vector<long long>(N, inf));
vector<vector<bool>> visited(1 << K, vector<bool>(N, false));
for(int i = 0; i < K; ++i){
d[1 << i][cities[i]] = 0;
pq.push({d[1 << i][cities[i]], 1 << i, cities[i]});
}
int full = (1 << K) - 1;
while(!pq.empty()){
long long cur; int mask, u;
tie(cur, mask, u) = pq.top(); pq.pop();
if(visited[mask][u]) continue;
visited[mask][u] = true;
for(auto [v, w] : adj[u]){
if(minimize(d[mask][v], d[mask][u] + w)){
pq.push({d[mask][v], mask, v});
}
int not_mask = mask ^ full;
for(int s = not_mask; s > 0; s = (s - 1) & not_mask){
if(minimize(d[mask | s][v], d[mask][u] + d[s][v] + w)){
pq.push({d[mask | s][v], mask | s, v});
}
}
}
}
long long ans = inf;
for(int i = 0; i < N; ++i){
minimize(ans, d[(1 << K) - 1][i]);
}
cout << ans << '\n';
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:52:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for(auto [v, w] : adj[u]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
453 ms |
34664 KB |
Output is correct |
2 |
Correct |
405 ms |
34440 KB |
Output is correct |
3 |
Correct |
249 ms |
19260 KB |
Output is correct |
4 |
Correct |
55 ms |
6016 KB |
Output is correct |
5 |
Correct |
152 ms |
19252 KB |
Output is correct |
6 |
Correct |
40 ms |
5572 KB |
Output is correct |
7 |
Correct |
3 ms |
1016 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1228 KB |
Output is correct |
2 |
Correct |
7 ms |
1228 KB |
Output is correct |
3 |
Correct |
5 ms |
848 KB |
Output is correct |
4 |
Correct |
6 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1535 ms |
90396 KB |
Output is correct |
2 |
Correct |
1460 ms |
90016 KB |
Output is correct |
3 |
Correct |
775 ms |
38184 KB |
Output is correct |
4 |
Correct |
898 ms |
49044 KB |
Output is correct |
5 |
Correct |
247 ms |
25072 KB |
Output is correct |
6 |
Correct |
94 ms |
9916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5664 ms |
168968 KB |
Output is correct |
2 |
Correct |
5810 ms |
173052 KB |
Output is correct |
3 |
Correct |
5595 ms |
172084 KB |
Output is correct |
4 |
Correct |
2459 ms |
102172 KB |
Output is correct |
5 |
Correct |
3361 ms |
158096 KB |
Output is correct |
6 |
Correct |
657 ms |
46756 KB |
Output is correct |
7 |
Correct |
237 ms |
20024 KB |
Output is correct |