// #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,lzcnt,mmx,abm,avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
vector<vector<pair<int,int>>> G;
vector<vector<int>> dp;
vector<int> ct;
void dijkstra(int s){
priority_queue<tuple<int,int,int>>pq;
pq.push({0,s,0}); // cost node msk
while(!pq.empty()){
int w,u,msk; tie(w,u,msk) = pq.top(); pq.pop();
if(w > dp[u][msk]) continue;
for(auto p : G[u]){
int v = p.first; int cost = p.second;
for(int i = 0; i<6; i++){
if(i > 0 && msk & (1<<(i-1)) || ct[i] == -1) {
continue;
}
int t = cost-(i*(cost/10)) + ct[i];
if(t > cost) continue;
if(!i){
if(dp[v][msk] > dp[u][msk] + t){
dp[v][msk] = dp[u][msk]+t;
pq.push({-dp[v][msk],v,msk});
}
}
else{
if(dp[v][msk ^ (1<<(i-1))] > dp[u][msk] + t){
dp[v][msk ^ (1<<(i-1))] = dp[u][msk]+t;
pq.push({-dp[u][msk]-t, v,(msk ^ (1<<(i-1))) });
}
}
}
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,e,k; cin>>n>>e>>k;
vector<int> good;
for(int i = 0; i<k; i++){
int x; cin>>x;
good.push_back(x);
}
G.resize(n+1);
for(int i = 0; i<e; i++){
int u,v,w; cin>>u>>v>>w;
G[u].push_back({v,w});
}
int q; cin>>q;
while(q--){
ct.assign(6,0);
dp.assign(n+1, vector<int>(32, inf));
int s; cin>>s;
// cerr<<"S: "<<s<<endl;
for(int i = 1; i<=5; i++) cin>>ct[i];
dp[s][0] = 0;
dijkstra(s);
int ans = inf;
for(int i = 0; i<k; i++){
// cerr<<good[i]<<endl;
for(int j = 0; j<32; j++){
ans = min(ans,dp[good[i]][j]);
}
}
if(ans == inf) ans = -1;
cout<<ans<<"\n";
}
}
Compilation message
Main.cpp: In function 'void dijkstra(long long int)':
Main.cpp:20:14: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
20 | if(i > 0 && msk & (1<<(i-1)) || ct[i] == -1) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2140 KB |
Output is correct |
2 |
Correct |
3 ms |
2136 KB |
Output is correct |
3 |
Correct |
4 ms |
2136 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2140 KB |
Output is correct |
2 |
Correct |
3 ms |
2136 KB |
Output is correct |
3 |
Correct |
4 ms |
2136 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
51 ms |
2136 KB |
Output is correct |
7 |
Correct |
15 ms |
2140 KB |
Output is correct |
8 |
Correct |
70 ms |
2140 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2140 KB |
Output is correct |
2 |
Correct |
3 ms |
2136 KB |
Output is correct |
3 |
Correct |
4 ms |
2136 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
51 ms |
2136 KB |
Output is correct |
7 |
Correct |
15 ms |
2140 KB |
Output is correct |
8 |
Correct |
70 ms |
2140 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
59 ms |
2140 KB |
Output is correct |
12 |
Correct |
15 ms |
2140 KB |
Output is correct |
13 |
Correct |
78 ms |
2328 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2140 KB |
Output is correct |
2 |
Correct |
3 ms |
2136 KB |
Output is correct |
3 |
Correct |
4 ms |
2136 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
2392 KB |
Output is correct |
7 |
Correct |
2 ms |
2140 KB |
Output is correct |
8 |
Correct |
4 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2140 KB |
Output is correct |
2 |
Correct |
3 ms |
2136 KB |
Output is correct |
3 |
Correct |
4 ms |
2136 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
51 ms |
2136 KB |
Output is correct |
7 |
Correct |
15 ms |
2140 KB |
Output is correct |
8 |
Correct |
70 ms |
2140 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
2392 KB |
Output is correct |
12 |
Correct |
2 ms |
2140 KB |
Output is correct |
13 |
Correct |
4 ms |
2140 KB |
Output is correct |
14 |
Correct |
230 ms |
3380 KB |
Output is correct |
15 |
Correct |
50 ms |
2380 KB |
Output is correct |
16 |
Correct |
174 ms |
2912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
2352 KB |
Output is correct |
2 |
Correct |
61 ms |
2348 KB |
Output is correct |
3 |
Correct |
17 ms |
2140 KB |
Output is correct |
4 |
Correct |
84 ms |
2428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
348 KB |
Output is correct |
2 |
Correct |
12 ms |
552 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
348 KB |
Output is correct |
5 |
Correct |
12 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
20 ms |
560 KB |
Output is correct |
8 |
Correct |
19 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2140 KB |
Output is correct |
2 |
Correct |
3 ms |
2136 KB |
Output is correct |
3 |
Correct |
4 ms |
2136 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
51 ms |
2136 KB |
Output is correct |
7 |
Correct |
15 ms |
2140 KB |
Output is correct |
8 |
Correct |
70 ms |
2140 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
59 ms |
2140 KB |
Output is correct |
12 |
Correct |
15 ms |
2140 KB |
Output is correct |
13 |
Correct |
78 ms |
2328 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
7 ms |
2392 KB |
Output is correct |
17 |
Correct |
2 ms |
2140 KB |
Output is correct |
18 |
Correct |
4 ms |
2140 KB |
Output is correct |
19 |
Correct |
230 ms |
3380 KB |
Output is correct |
20 |
Correct |
50 ms |
2380 KB |
Output is correct |
21 |
Correct |
174 ms |
2912 KB |
Output is correct |
22 |
Correct |
61 ms |
2352 KB |
Output is correct |
23 |
Correct |
61 ms |
2348 KB |
Output is correct |
24 |
Correct |
17 ms |
2140 KB |
Output is correct |
25 |
Correct |
84 ms |
2428 KB |
Output is correct |
26 |
Correct |
20 ms |
348 KB |
Output is correct |
27 |
Correct |
12 ms |
552 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
11 ms |
348 KB |
Output is correct |
30 |
Correct |
12 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
20 ms |
560 KB |
Output is correct |
33 |
Correct |
19 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
215 ms |
3008 KB |
Output is correct |
38 |
Correct |
159 ms |
2536 KB |
Output is correct |
39 |
Correct |
245 ms |
3268 KB |
Output is correct |
40 |
Correct |
5 ms |
1884 KB |
Output is correct |
41 |
Correct |
26 ms |
2068 KB |
Output is correct |
42 |
Correct |
191 ms |
3200 KB |
Output is correct |
43 |
Correct |
237 ms |
3236 KB |
Output is correct |
44 |
Correct |
156 ms |
2836 KB |
Output is correct |
45 |
Correct |
26 ms |
2140 KB |
Output is correct |