#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]});
}
auto mergeMask = [&](int u){
for(int mask = 1; mask < (1 << K); ++mask){
if(!visited[mask][u]){
for(int s = mask - 1 & (mask); s > 0; s = (s - 1) & (mask)){
if(minimize(d[mask][u], d[s][u] + d[mask ^ s][u])){
pq.push({d[mask][u], mask, u});
}
}
}
}
};
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;
mergeMask(u);
for(auto [v, w] : adj[u]){
if(minimize(d[mask][v], d[mask][u] + w)){
pq.push({d[mask][v], mask, 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 lambda function:
cities.cpp:47:34: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
47 | for(int s = mask - 1 & (mask); s > 0; s = (s - 1) & (mask)){
| ~~~~~^~~
cities.cpp: In function 'int main()':
cities.cpp:64:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | 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 |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
403 ms |
38324 KB |
Output is correct |
2 |
Correct |
359 ms |
38044 KB |
Output is correct |
3 |
Correct |
200 ms |
21556 KB |
Output is correct |
4 |
Correct |
44 ms |
8900 KB |
Output is correct |
5 |
Correct |
156 ms |
23528 KB |
Output is correct |
6 |
Correct |
38 ms |
8776 KB |
Output is correct |
7 |
Correct |
3 ms |
848 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 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 |
5 ms |
924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1670 ms |
93520 KB |
Output is correct |
2 |
Correct |
1500 ms |
61420 KB |
Output is correct |
3 |
Correct |
1052 ms |
56608 KB |
Output is correct |
4 |
Correct |
800 ms |
53280 KB |
Output is correct |
5 |
Correct |
159 ms |
20784 KB |
Output is correct |
6 |
Correct |
56 ms |
10824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6070 ms |
172864 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |