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;
const int N = 1e5 + 5, M = (1 << 5) + 5;
const long long INF = 1e18;
int n, k, m, val[N];
vector < pair < int, int > > g[N];
long long d[M][N];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> k >> m;
for(int i = 0; i < (1 << k); i++){
for(int j = 1; j <= n; j++){
d[i][j] = INF;
}
}
for(int i = 0; i < k; i++){
int x;
cin >> x;
val[x] = (1 << i);
d[val[x]][x] = 0;
}
for(int i = 1; i <= m; i++){
int x, y, z;
cin >> x >> y >> z;
g[x].push_back(make_pair(y, z));
g[y].push_back(make_pair(x, z));
}
for(int mask = 1; mask < (1 << k); mask++){
set < pair < long long, int > > q;
for(int i = 0; i < mask; i++){
if((mask | i) != mask){
continue;
}
for(int j = 1; j <= n; j++){
d[mask][j] = min(d[mask][j], d[i][j] + d[(mask ^ i)][j]);
}
}
for(int i = 1; i <= n; i++){
if(d[mask][i] != INF){
q.insert(make_pair(d[mask][i], i));
}
}
while(!q.empty()){
int v = q.begin()->second;
q.erase(q.begin());
for(auto it : g[v]){
int to = it.first, len = it.second;
if(d[mask][to] > d[mask][v] + len){
q.erase(make_pair(d[mask][to], to));
d[mask][to] = d[mask][v] + len;
q.insert(make_pair(d[mask][to], to));
}
}
}
}
long long ans = INF;
for(int i = 1; i <= n; i++){
ans = min(ans, d[(1 << k) - 1][i]);
}
cout << ans;
}
# | 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... |