This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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]){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |