#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = (1 << 5) + 5;
const long long INF = 1e18;
bool used[N];
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++){
priority_queue < 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++){
used[i] = false;
if(d[mask][i] != INF){
q.push(make_pair(-d[mask][i], i));
}
}
while(!q.empty()){
long long val = -q.top().first;
int v = q.top().second;
q.pop();
if(used[v]){
continue;
}
used[v] = true;
for(auto it : g[v]){
int to = it.first;
long long nxt = val + it.second;
if(d[mask][to] > nxt){
d[mask][to] = nxt;
q.push(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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2808 KB |
Output is correct |
5 |
Correct |
4 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
624 ms |
21724 KB |
Output is correct |
2 |
Correct |
641 ms |
21716 KB |
Output is correct |
3 |
Correct |
369 ms |
16056 KB |
Output is correct |
4 |
Correct |
80 ms |
7512 KB |
Output is correct |
5 |
Correct |
348 ms |
16500 KB |
Output is correct |
6 |
Correct |
72 ms |
7344 KB |
Output is correct |
7 |
Correct |
6 ms |
2936 KB |
Output is correct |
8 |
Correct |
5 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3064 KB |
Output is correct |
2 |
Correct |
8 ms |
3064 KB |
Output is correct |
3 |
Correct |
7 ms |
2936 KB |
Output is correct |
4 |
Correct |
6 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1070 ms |
28020 KB |
Output is correct |
2 |
Correct |
1106 ms |
28124 KB |
Output is correct |
3 |
Correct |
798 ms |
22264 KB |
Output is correct |
4 |
Correct |
679 ms |
18484 KB |
Output is correct |
5 |
Correct |
164 ms |
9780 KB |
Output is correct |
6 |
Correct |
81 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2409 ms |
40760 KB |
Output is correct |
2 |
Correct |
2163 ms |
44888 KB |
Output is correct |
3 |
Correct |
2673 ms |
44880 KB |
Output is correct |
4 |
Correct |
1885 ms |
37100 KB |
Output is correct |
5 |
Correct |
1311 ms |
28984 KB |
Output is correct |
6 |
Correct |
296 ms |
15152 KB |
Output is correct |
7 |
Correct |
87 ms |
12664 KB |
Output is correct |