#include <bits/stdc++.h>
#define int int64_t
using namespace std;
const int INF = 1e10;
const int LG = 20;
const int MAX_N = 5e4+4;
const int MAX_K = 5;
int up[MAX_N][LG][MAX_K];
int dijkstra(int s, int e, int K, auto&graph){
vector<int>dist(LG*K, -1);
priority_queue<pair<int, int>>pq;
pq.push({0, s});
while(pq.size()){
auto [d, u] = pq.top();
pq.pop();
if(dist[u] != -1) continue;
d = -d;
dist[u] = d;
for(auto [v, w]: graph[u]){
pq.push({-(w+d), v});
}
}
return dist[e];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
int K, n, m, q; cin>>K>>n>>m>>q;
for(int i = 0; i<MAX_N; ++i){
for(int j = 0; j<LG; ++j){
for(int k = 0; k<MAX_K; ++k) up[i][j][k] = INF;
}
}
for(int i = 0; i<m; ++i){
int a, b, c; cin>>a>>b>>c;
up[a][0][b%K] = c;
}
for(int jumps = 1; jumps<LG; ++jumps){
for(int i = 0; i<n; ++i){
for(int k = 0; k<K; ++k){
for(int middle = 0; middle < K; ++middle){
int mid_node = i/K*K + ((1<<(jumps-1))*K) + middle;
if(mid_node > n) break;
up[i][jumps][k] = min(up[i][jumps][k], up[i][jumps-1][middle] + up[mid_node][jumps-1][k]);
}
}
}
}
/*
for(int i = 0; i<5; ++i){
for(int k = 0; k<K; ++k) cout<<up[0][i][k]<<" ";
cout<<endl;
}
cout<<endl;
for(int i = 0; i<5; ++i){
for(int k = 0; k<K; ++k) cout<<up[2][i][k]<<" ";
cout<<endl;
}
cout<<endl;
for(int i = 0; i<5; ++i){
for(int k = 0; k<K; ++k) cout<<up[3][i][k]<<" ";
cout<<endl;
}
cout<<endl;
*/
for(int i = 0; i<q; ++i){
int a, b; cin>>a>>b;
vector<vector<pair<int, int>>>graph(LG*K);
int cnt = 0;
int group = a/K*K;
int cur_jumps = (b/K) - (a/K);
if(!cur_jumps){
cout<<-1<<'\n';
continue;
}
int start = a%K;
int maxi = 0;
for(int j = LG-1; j>=0; --j){
if(cur_jumps - (1<<j) >= 0){
cur_jumps -= (1<<j);
for(int k1 = 0; k1<K; ++k1){
for(int k2 = 0; k2<K; ++k2){
if(group + k1 > n) break;
int node1 = cnt + k1;
int node2 = cnt + K + k2;
maxi = node2;
graph[node1].push_back({node2, up[group+k1][j][k2]});
}
}
cnt += K;
group += (1<<j)*K;
}
}
/*
for(int j = 0; j<10; ++j){
cout<<"j "<<j<<endl;
for(auto u: graph[j]) cout<<u.first<<" "<<u.second<<endl;
cout<<endl;
}
*/
int end = maxi-K+1+(b%K);
//cout<<"end "<<end<<endl;
int ans = dijkstra(start, end, K, graph);
if(ans >= INF) cout<<-1<<'\n';
else cout<<ans<<'\n';
}
//cout<<'\n';
}
Compilation message
toll.cpp:12:35: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
12 | int dijkstra(int s, int e, int K, auto&graph){
| ^~~~
toll.cpp: In function 'int64_t dijkstra(int64_t, int64_t, int64_t, auto:1&)':
toll.cpp:19:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
19 | auto [d, u] = pq.top();
| ^
toll.cpp:27:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
27 | for(auto [v, w]: graph[u]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
39516 KB |
Output is correct |
2 |
Correct |
6 ms |
39516 KB |
Output is correct |
3 |
Correct |
7 ms |
39348 KB |
Output is correct |
4 |
Correct |
6 ms |
39516 KB |
Output is correct |
5 |
Correct |
7 ms |
39516 KB |
Output is correct |
6 |
Correct |
7 ms |
39516 KB |
Output is correct |
7 |
Correct |
7 ms |
39588 KB |
Output is correct |
8 |
Correct |
34 ms |
39592 KB |
Output is correct |
9 |
Correct |
41 ms |
39588 KB |
Output is correct |
10 |
Correct |
24 ms |
39516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
39516 KB |
Output is correct |
2 |
Correct |
6 ms |
39516 KB |
Output is correct |
3 |
Correct |
6 ms |
39516 KB |
Output is correct |
4 |
Correct |
6 ms |
39516 KB |
Output is correct |
5 |
Correct |
6 ms |
39388 KB |
Output is correct |
6 |
Correct |
6 ms |
39516 KB |
Output is correct |
7 |
Correct |
12 ms |
39704 KB |
Output is correct |
8 |
Correct |
20 ms |
39516 KB |
Output is correct |
9 |
Correct |
38 ms |
40272 KB |
Output is correct |
10 |
Correct |
108 ms |
41836 KB |
Output is correct |
11 |
Correct |
63 ms |
41284 KB |
Output is correct |
12 |
Correct |
93 ms |
40864 KB |
Output is correct |
13 |
Correct |
84 ms |
41812 KB |
Output is correct |
14 |
Correct |
49 ms |
41000 KB |
Output is correct |
15 |
Correct |
74 ms |
40860 KB |
Output is correct |
16 |
Correct |
74 ms |
40796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
39512 KB |
Output is correct |
2 |
Correct |
6 ms |
39516 KB |
Output is correct |
3 |
Correct |
7 ms |
39576 KB |
Output is correct |
4 |
Correct |
7 ms |
39512 KB |
Output is correct |
5 |
Correct |
6 ms |
39512 KB |
Output is correct |
6 |
Correct |
7 ms |
39672 KB |
Output is correct |
7 |
Correct |
7 ms |
39516 KB |
Output is correct |
8 |
Correct |
10 ms |
39644 KB |
Output is correct |
9 |
Correct |
8 ms |
39516 KB |
Output is correct |
10 |
Correct |
26 ms |
40352 KB |
Output is correct |
11 |
Correct |
43 ms |
40988 KB |
Output is correct |
12 |
Correct |
57 ms |
41628 KB |
Output is correct |
13 |
Correct |
59 ms |
41808 KB |
Output is correct |
14 |
Correct |
54 ms |
41240 KB |
Output is correct |
15 |
Correct |
61 ms |
40764 KB |
Output is correct |
16 |
Correct |
42 ms |
40536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
39512 KB |
Output is correct |
2 |
Correct |
6 ms |
39516 KB |
Output is correct |
3 |
Correct |
7 ms |
39576 KB |
Output is correct |
4 |
Correct |
7 ms |
39512 KB |
Output is correct |
5 |
Correct |
6 ms |
39512 KB |
Output is correct |
6 |
Correct |
7 ms |
39672 KB |
Output is correct |
7 |
Correct |
7 ms |
39516 KB |
Output is correct |
8 |
Correct |
10 ms |
39644 KB |
Output is correct |
9 |
Correct |
8 ms |
39516 KB |
Output is correct |
10 |
Correct |
26 ms |
40352 KB |
Output is correct |
11 |
Correct |
43 ms |
40988 KB |
Output is correct |
12 |
Correct |
57 ms |
41628 KB |
Output is correct |
13 |
Correct |
59 ms |
41808 KB |
Output is correct |
14 |
Correct |
54 ms |
41240 KB |
Output is correct |
15 |
Correct |
61 ms |
40764 KB |
Output is correct |
16 |
Correct |
42 ms |
40536 KB |
Output is correct |
17 |
Correct |
55 ms |
41140 KB |
Output is correct |
18 |
Correct |
6 ms |
39512 KB |
Output is correct |
19 |
Correct |
6 ms |
39516 KB |
Output is correct |
20 |
Correct |
7 ms |
39516 KB |
Output is correct |
21 |
Correct |
7 ms |
39512 KB |
Output is correct |
22 |
Correct |
6 ms |
39512 KB |
Output is correct |
23 |
Correct |
9 ms |
39516 KB |
Output is correct |
24 |
Correct |
11 ms |
39516 KB |
Output is correct |
25 |
Correct |
24 ms |
39692 KB |
Output is correct |
26 |
Correct |
18 ms |
39516 KB |
Output is correct |
27 |
Correct |
29 ms |
40284 KB |
Output is correct |
28 |
Correct |
91 ms |
41620 KB |
Output is correct |
29 |
Correct |
76 ms |
41808 KB |
Output is correct |
30 |
Correct |
69 ms |
41444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
39516 KB |
Output is correct |
2 |
Correct |
6 ms |
39516 KB |
Output is correct |
3 |
Correct |
7 ms |
39348 KB |
Output is correct |
4 |
Correct |
6 ms |
39516 KB |
Output is correct |
5 |
Correct |
7 ms |
39516 KB |
Output is correct |
6 |
Correct |
7 ms |
39516 KB |
Output is correct |
7 |
Correct |
7 ms |
39588 KB |
Output is correct |
8 |
Correct |
34 ms |
39592 KB |
Output is correct |
9 |
Correct |
41 ms |
39588 KB |
Output is correct |
10 |
Correct |
24 ms |
39516 KB |
Output is correct |
11 |
Correct |
61 ms |
39516 KB |
Output is correct |
12 |
Correct |
6 ms |
39516 KB |
Output is correct |
13 |
Correct |
6 ms |
39516 KB |
Output is correct |
14 |
Correct |
6 ms |
39516 KB |
Output is correct |
15 |
Correct |
6 ms |
39388 KB |
Output is correct |
16 |
Correct |
6 ms |
39516 KB |
Output is correct |
17 |
Correct |
12 ms |
39704 KB |
Output is correct |
18 |
Correct |
20 ms |
39516 KB |
Output is correct |
19 |
Correct |
38 ms |
40272 KB |
Output is correct |
20 |
Correct |
108 ms |
41836 KB |
Output is correct |
21 |
Correct |
63 ms |
41284 KB |
Output is correct |
22 |
Correct |
93 ms |
40864 KB |
Output is correct |
23 |
Correct |
84 ms |
41812 KB |
Output is correct |
24 |
Correct |
49 ms |
41000 KB |
Output is correct |
25 |
Correct |
74 ms |
40860 KB |
Output is correct |
26 |
Correct |
74 ms |
40796 KB |
Output is correct |
27 |
Correct |
8 ms |
39512 KB |
Output is correct |
28 |
Correct |
6 ms |
39516 KB |
Output is correct |
29 |
Correct |
7 ms |
39576 KB |
Output is correct |
30 |
Correct |
7 ms |
39512 KB |
Output is correct |
31 |
Correct |
6 ms |
39512 KB |
Output is correct |
32 |
Correct |
7 ms |
39672 KB |
Output is correct |
33 |
Correct |
7 ms |
39516 KB |
Output is correct |
34 |
Correct |
10 ms |
39644 KB |
Output is correct |
35 |
Correct |
8 ms |
39516 KB |
Output is correct |
36 |
Correct |
26 ms |
40352 KB |
Output is correct |
37 |
Correct |
43 ms |
40988 KB |
Output is correct |
38 |
Correct |
57 ms |
41628 KB |
Output is correct |
39 |
Correct |
59 ms |
41808 KB |
Output is correct |
40 |
Correct |
54 ms |
41240 KB |
Output is correct |
41 |
Correct |
61 ms |
40764 KB |
Output is correct |
42 |
Correct |
42 ms |
40536 KB |
Output is correct |
43 |
Correct |
55 ms |
41140 KB |
Output is correct |
44 |
Correct |
6 ms |
39512 KB |
Output is correct |
45 |
Correct |
6 ms |
39516 KB |
Output is correct |
46 |
Correct |
7 ms |
39516 KB |
Output is correct |
47 |
Correct |
7 ms |
39512 KB |
Output is correct |
48 |
Correct |
6 ms |
39512 KB |
Output is correct |
49 |
Correct |
9 ms |
39516 KB |
Output is correct |
50 |
Correct |
11 ms |
39516 KB |
Output is correct |
51 |
Correct |
24 ms |
39692 KB |
Output is correct |
52 |
Correct |
18 ms |
39516 KB |
Output is correct |
53 |
Correct |
29 ms |
40284 KB |
Output is correct |
54 |
Correct |
91 ms |
41620 KB |
Output is correct |
55 |
Correct |
76 ms |
41808 KB |
Output is correct |
56 |
Correct |
69 ms |
41444 KB |
Output is correct |
57 |
Correct |
179 ms |
42772 KB |
Output is correct |
58 |
Correct |
36 ms |
40540 KB |
Output is correct |
59 |
Correct |
69 ms |
41372 KB |
Output is correct |