#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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1532 ms |
25544 KB |
Output is correct |
2 |
Correct |
1381 ms |
25284 KB |
Output is correct |
3 |
Correct |
556 ms |
20728 KB |
Output is correct |
4 |
Correct |
81 ms |
10616 KB |
Output is correct |
5 |
Correct |
614 ms |
22436 KB |
Output is correct |
6 |
Correct |
74 ms |
10616 KB |
Output is correct |
7 |
Correct |
8 ms |
2936 KB |
Output is correct |
8 |
Correct |
6 ms |
2876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3064 KB |
Output is correct |
2 |
Correct |
11 ms |
3064 KB |
Output is correct |
3 |
Correct |
8 ms |
3068 KB |
Output is correct |
4 |
Correct |
8 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2967 ms |
31820 KB |
Output is correct |
2 |
Correct |
3127 ms |
31472 KB |
Output is correct |
3 |
Correct |
1501 ms |
26804 KB |
Output is correct |
4 |
Correct |
1434 ms |
21888 KB |
Output is correct |
5 |
Correct |
327 ms |
13052 KB |
Output is correct |
6 |
Correct |
86 ms |
12408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6015 ms |
44408 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |