#include <bits/stdc++.h>
#define int int64_t
using namespace std;
const int INF = 1e9;
const int LG = 21;
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){
if(middle + ((1<<(jumps-1))*K) > n) break;
up[i][jumps][k] = min(up[i][jumps][k], up[i][jumps-1][middle] + up[middle + ((1<<(jumps-1))*K)][jumps-1][k]);
}
}
}
}
/*
for(int i = 0; i<3; ++i){
for(int k = 0; k<K; ++k) cout<<up[0][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);
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 |
Incorrect |
35 ms |
41560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
41556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
41304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
41304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
41560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |