#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, E, K; cin >> N >> E >> K;
vector<int> T(K);
for(auto &i : T) cin >> i;
vector<vector<pair<int, int>>> adj(N);
for(int i = 0; i < E; i++){
int u, v, c; cin >> u >> v >> c;
adj[v].push_back({u, c});
}
vector<vector<long long>> dist(32, vector<long long>(N, 1e18));
vector<vector<bool>> vis(32, vector<bool>(N, false));
priority_queue<tuple<long long, int, int>> q;
for(int i = 0; i < K; i++){
dist[0][T[i]] = 0;
q.push({0, 0, T[i]});
}
while(!q.empty()){
int mask = get<1>(q.top()); int x = get<2>(q.top()); q.pop();
if(vis[mask][x]) continue;
vis[mask][x] = true;
for(pair<int, int> p : adj[x]){
int node = p.first; int c = p.second;
if(dist[mask][node] > dist[mask][x] + c){
dist[mask][node] = dist[mask][x] + c;
q.push({-dist[mask][node], mask, node});
}
for(int b = 0; b < 5; b++){
if(!((1 << b) & mask)){
int nmask = mask + (1 << b);
long long w = (c * (9 - b)) / 10;
if(dist[nmask][node] > dist[mask][x] + w){
dist[nmask][node] = dist[mask][x] + w;
q.push({-dist[nmask][node], nmask, node});
}
}
}
}
}
int Q; cin >> Q;
while(Q--){
int S; cin >> S;
vector<int> P(5);
for(auto &i : P) cin >> i;
long long ans = 1e18;
for(int i = 0; i < 32; i++){
long long cost = dist[i][S];
for(int b = 0; b < 5; b++){
if((1 << b) & i){
if(P[b] == -1) cost = 1e18;
else cost += P[b];
}
}
ans = min(ans, cost);
}
if(ans == 1e18) cout << -1 << "\n";
else cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
3796 KB |
Output is correct |
2 |
Correct |
13 ms |
1884 KB |
Output is correct |
3 |
Correct |
44 ms |
3912 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
3796 KB |
Output is correct |
2 |
Correct |
13 ms |
1884 KB |
Output is correct |
3 |
Correct |
44 ms |
3912 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
46 ms |
3808 KB |
Output is correct |
7 |
Correct |
15 ms |
1880 KB |
Output is correct |
8 |
Correct |
44 ms |
3804 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
3796 KB |
Output is correct |
2 |
Correct |
13 ms |
1884 KB |
Output is correct |
3 |
Correct |
44 ms |
3912 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
46 ms |
3808 KB |
Output is correct |
7 |
Correct |
15 ms |
1880 KB |
Output is correct |
8 |
Correct |
44 ms |
3804 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
69 ms |
9084 KB |
Output is correct |
12 |
Correct |
16 ms |
1880 KB |
Output is correct |
13 |
Correct |
57 ms |
3988 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
3796 KB |
Output is correct |
2 |
Correct |
13 ms |
1884 KB |
Output is correct |
3 |
Correct |
44 ms |
3912 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
4 ms |
2244 KB |
Output is correct |
7 |
Correct |
13 ms |
2120 KB |
Output is correct |
8 |
Correct |
48 ms |
3808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
3796 KB |
Output is correct |
2 |
Correct |
13 ms |
1884 KB |
Output is correct |
3 |
Correct |
44 ms |
3912 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
46 ms |
3808 KB |
Output is correct |
7 |
Correct |
15 ms |
1880 KB |
Output is correct |
8 |
Correct |
44 ms |
3804 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
2244 KB |
Output is correct |
12 |
Correct |
13 ms |
2120 KB |
Output is correct |
13 |
Correct |
48 ms |
3808 KB |
Output is correct |
14 |
Correct |
40 ms |
3808 KB |
Output is correct |
15 |
Correct |
15 ms |
1884 KB |
Output is correct |
16 |
Correct |
58 ms |
3808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
5748 KB |
Output is correct |
2 |
Correct |
65 ms |
10012 KB |
Output is correct |
3 |
Correct |
31 ms |
2600 KB |
Output is correct |
4 |
Correct |
53 ms |
3804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1048 KB |
Output is correct |
2 |
Correct |
3 ms |
1056 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
1048 KB |
Output is correct |
5 |
Correct |
4 ms |
1052 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
1052 KB |
Output is correct |
8 |
Correct |
3 ms |
1052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
3796 KB |
Output is correct |
2 |
Correct |
13 ms |
1884 KB |
Output is correct |
3 |
Correct |
44 ms |
3912 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
46 ms |
3808 KB |
Output is correct |
7 |
Correct |
15 ms |
1880 KB |
Output is correct |
8 |
Correct |
44 ms |
3804 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
69 ms |
9084 KB |
Output is correct |
12 |
Correct |
16 ms |
1880 KB |
Output is correct |
13 |
Correct |
57 ms |
3988 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
2244 KB |
Output is correct |
17 |
Correct |
13 ms |
2120 KB |
Output is correct |
18 |
Correct |
48 ms |
3808 KB |
Output is correct |
19 |
Correct |
40 ms |
3808 KB |
Output is correct |
20 |
Correct |
15 ms |
1884 KB |
Output is correct |
21 |
Correct |
58 ms |
3808 KB |
Output is correct |
22 |
Correct |
64 ms |
5748 KB |
Output is correct |
23 |
Correct |
65 ms |
10012 KB |
Output is correct |
24 |
Correct |
31 ms |
2600 KB |
Output is correct |
25 |
Correct |
53 ms |
3804 KB |
Output is correct |
26 |
Correct |
3 ms |
1048 KB |
Output is correct |
27 |
Correct |
3 ms |
1056 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
1048 KB |
Output is correct |
30 |
Correct |
4 ms |
1052 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
4 ms |
1052 KB |
Output is correct |
33 |
Correct |
3 ms |
1052 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
66 ms |
8556 KB |
Output is correct |
38 |
Correct |
66 ms |
10444 KB |
Output is correct |
39 |
Correct |
50 ms |
3804 KB |
Output is correct |
40 |
Correct |
1 ms |
1624 KB |
Output is correct |
41 |
Correct |
2 ms |
2392 KB |
Output is correct |
42 |
Correct |
72 ms |
8408 KB |
Output is correct |
43 |
Correct |
70 ms |
9684 KB |
Output is correct |
44 |
Correct |
51 ms |
3780 KB |
Output is correct |
45 |
Correct |
27 ms |
2396 KB |
Output is correct |