#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N=50005;
const int INF=1e9;
const int K=__lg(N)+1;
vector jump(N, vector (K,vector(5,vector(5,INF))));
void f(vector<vector<int>> &a,vector<vector<int>> &b,vector<vector<int>> &c,int k){
//i -> p -> j
for(int i=0;i<k;i++){
for(int p=0;p<k;p++){
for(int j=0;j<k;j++){
c[i][j]=min(c[i][j],a[i][p]+b[p][j]);
}
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int k,n,m,q;cin>>k>>n>>m>>q;
for(int i=0;i<m;i++){
int a,b,c;cin>>a>>b>>c;
int t=a/k;
jump[t][0][a%k][b%k]=c;
}
for(int j=1;j<K;j++){
for(int i=0;i+(1<<j-1)<N;i++){
f(jump[i][j-1],jump[i+(1<<j-1)][j-1],jump[i][j],k);
}
}
for(int i=0;i<q;i++){
vector ans(5,vector(5,INF));
int a,b;cin>>a>>b;
ans[a%k][a%k]=0;
int t=(b/k)-(a/k);
if(t==0){
cout<<-1<<endl;
continue;
}
int now=a/k;
for(int j=0;j<K;j++){
if(t&(1<<j)){
vector<vector<int>> y(5);
for(int j1=0;j1<5;j1++){
y[j1]=ans[j1];
ans[j1].assign(5,INF);
}
f(y,jump[now][j],ans,k);
now+=(1<<j);
}
}
int ans1=ans[a%k][b%k];
cout<<(ans1>=INF?-1:ans1)<<endl;
}
return 0;
}
Compilation message
toll.cpp: In function 'int32_t main()':
toll.cpp:29:28: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
29 | for(int i=0;i+(1<<j-1)<N;i++){
| ~^~
toll.cpp:30:40: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
30 | f(jump[i][j-1],jump[i+(1<<j-1)][j-1],jump[i][j],k);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
309148 KB |
Output is correct |
2 |
Correct |
293 ms |
309120 KB |
Output is correct |
3 |
Correct |
270 ms |
309060 KB |
Output is correct |
4 |
Correct |
312 ms |
309044 KB |
Output is correct |
5 |
Correct |
284 ms |
309092 KB |
Output is correct |
6 |
Correct |
298 ms |
309156 KB |
Output is correct |
7 |
Correct |
316 ms |
309160 KB |
Output is correct |
8 |
Correct |
381 ms |
309160 KB |
Output is correct |
9 |
Correct |
355 ms |
309156 KB |
Output is correct |
10 |
Correct |
322 ms |
309156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
400 ms |
309156 KB |
Output is correct |
2 |
Correct |
278 ms |
309048 KB |
Output is correct |
3 |
Correct |
349 ms |
309076 KB |
Output is correct |
4 |
Correct |
370 ms |
309068 KB |
Output is correct |
5 |
Correct |
465 ms |
309052 KB |
Output is correct |
6 |
Correct |
480 ms |
309036 KB |
Output is correct |
7 |
Correct |
303 ms |
309164 KB |
Output is correct |
8 |
Correct |
343 ms |
309204 KB |
Output is correct |
9 |
Correct |
340 ms |
310040 KB |
Output is correct |
10 |
Correct |
447 ms |
311440 KB |
Output is correct |
11 |
Correct |
383 ms |
310908 KB |
Output is correct |
12 |
Correct |
476 ms |
310452 KB |
Output is correct |
13 |
Correct |
555 ms |
311548 KB |
Output is correct |
14 |
Correct |
407 ms |
310604 KB |
Output is correct |
15 |
Correct |
520 ms |
310368 KB |
Output is correct |
16 |
Correct |
500 ms |
310376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
291 ms |
309116 KB |
Output is correct |
2 |
Correct |
316 ms |
309064 KB |
Output is correct |
3 |
Correct |
409 ms |
309220 KB |
Output is correct |
4 |
Correct |
429 ms |
309152 KB |
Output is correct |
5 |
Correct |
505 ms |
309084 KB |
Output is correct |
6 |
Correct |
308 ms |
309076 KB |
Output is correct |
7 |
Correct |
362 ms |
309192 KB |
Output is correct |
8 |
Correct |
496 ms |
309144 KB |
Output is correct |
9 |
Correct |
409 ms |
309100 KB |
Output is correct |
10 |
Correct |
308 ms |
309912 KB |
Output is correct |
11 |
Correct |
375 ms |
310592 KB |
Output is correct |
12 |
Correct |
456 ms |
311248 KB |
Output is correct |
13 |
Correct |
442 ms |
311452 KB |
Output is correct |
14 |
Correct |
416 ms |
310932 KB |
Output is correct |
15 |
Correct |
502 ms |
310340 KB |
Output is correct |
16 |
Correct |
500 ms |
310296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
291 ms |
309116 KB |
Output is correct |
2 |
Correct |
316 ms |
309064 KB |
Output is correct |
3 |
Correct |
409 ms |
309220 KB |
Output is correct |
4 |
Correct |
429 ms |
309152 KB |
Output is correct |
5 |
Correct |
505 ms |
309084 KB |
Output is correct |
6 |
Correct |
308 ms |
309076 KB |
Output is correct |
7 |
Correct |
362 ms |
309192 KB |
Output is correct |
8 |
Correct |
496 ms |
309144 KB |
Output is correct |
9 |
Correct |
409 ms |
309100 KB |
Output is correct |
10 |
Correct |
308 ms |
309912 KB |
Output is correct |
11 |
Correct |
375 ms |
310592 KB |
Output is correct |
12 |
Correct |
456 ms |
311248 KB |
Output is correct |
13 |
Correct |
442 ms |
311452 KB |
Output is correct |
14 |
Correct |
416 ms |
310932 KB |
Output is correct |
15 |
Correct |
502 ms |
310340 KB |
Output is correct |
16 |
Correct |
500 ms |
310296 KB |
Output is correct |
17 |
Correct |
392 ms |
310656 KB |
Output is correct |
18 |
Correct |
286 ms |
309136 KB |
Output is correct |
19 |
Correct |
335 ms |
309048 KB |
Output is correct |
20 |
Correct |
428 ms |
309096 KB |
Output is correct |
21 |
Correct |
430 ms |
309028 KB |
Output is correct |
22 |
Correct |
483 ms |
309152 KB |
Output is correct |
23 |
Correct |
312 ms |
309192 KB |
Output is correct |
24 |
Correct |
379 ms |
309208 KB |
Output is correct |
25 |
Correct |
513 ms |
309236 KB |
Output is correct |
26 |
Correct |
465 ms |
309152 KB |
Output is correct |
27 |
Correct |
370 ms |
310004 KB |
Output is correct |
28 |
Correct |
482 ms |
311392 KB |
Output is correct |
29 |
Correct |
514 ms |
311544 KB |
Output is correct |
30 |
Correct |
631 ms |
311008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
309148 KB |
Output is correct |
2 |
Correct |
293 ms |
309120 KB |
Output is correct |
3 |
Correct |
270 ms |
309060 KB |
Output is correct |
4 |
Correct |
312 ms |
309044 KB |
Output is correct |
5 |
Correct |
284 ms |
309092 KB |
Output is correct |
6 |
Correct |
298 ms |
309156 KB |
Output is correct |
7 |
Correct |
316 ms |
309160 KB |
Output is correct |
8 |
Correct |
381 ms |
309160 KB |
Output is correct |
9 |
Correct |
355 ms |
309156 KB |
Output is correct |
10 |
Correct |
322 ms |
309156 KB |
Output is correct |
11 |
Correct |
400 ms |
309156 KB |
Output is correct |
12 |
Correct |
278 ms |
309048 KB |
Output is correct |
13 |
Correct |
349 ms |
309076 KB |
Output is correct |
14 |
Correct |
370 ms |
309068 KB |
Output is correct |
15 |
Correct |
465 ms |
309052 KB |
Output is correct |
16 |
Correct |
480 ms |
309036 KB |
Output is correct |
17 |
Correct |
303 ms |
309164 KB |
Output is correct |
18 |
Correct |
343 ms |
309204 KB |
Output is correct |
19 |
Correct |
340 ms |
310040 KB |
Output is correct |
20 |
Correct |
447 ms |
311440 KB |
Output is correct |
21 |
Correct |
383 ms |
310908 KB |
Output is correct |
22 |
Correct |
476 ms |
310452 KB |
Output is correct |
23 |
Correct |
555 ms |
311548 KB |
Output is correct |
24 |
Correct |
407 ms |
310604 KB |
Output is correct |
25 |
Correct |
520 ms |
310368 KB |
Output is correct |
26 |
Correct |
500 ms |
310376 KB |
Output is correct |
27 |
Correct |
291 ms |
309116 KB |
Output is correct |
28 |
Correct |
316 ms |
309064 KB |
Output is correct |
29 |
Correct |
409 ms |
309220 KB |
Output is correct |
30 |
Correct |
429 ms |
309152 KB |
Output is correct |
31 |
Correct |
505 ms |
309084 KB |
Output is correct |
32 |
Correct |
308 ms |
309076 KB |
Output is correct |
33 |
Correct |
362 ms |
309192 KB |
Output is correct |
34 |
Correct |
496 ms |
309144 KB |
Output is correct |
35 |
Correct |
409 ms |
309100 KB |
Output is correct |
36 |
Correct |
308 ms |
309912 KB |
Output is correct |
37 |
Correct |
375 ms |
310592 KB |
Output is correct |
38 |
Correct |
456 ms |
311248 KB |
Output is correct |
39 |
Correct |
442 ms |
311452 KB |
Output is correct |
40 |
Correct |
416 ms |
310932 KB |
Output is correct |
41 |
Correct |
502 ms |
310340 KB |
Output is correct |
42 |
Correct |
500 ms |
310296 KB |
Output is correct |
43 |
Correct |
392 ms |
310656 KB |
Output is correct |
44 |
Correct |
286 ms |
309136 KB |
Output is correct |
45 |
Correct |
335 ms |
309048 KB |
Output is correct |
46 |
Correct |
428 ms |
309096 KB |
Output is correct |
47 |
Correct |
430 ms |
309028 KB |
Output is correct |
48 |
Correct |
483 ms |
309152 KB |
Output is correct |
49 |
Correct |
312 ms |
309192 KB |
Output is correct |
50 |
Correct |
379 ms |
309208 KB |
Output is correct |
51 |
Correct |
513 ms |
309236 KB |
Output is correct |
52 |
Correct |
465 ms |
309152 KB |
Output is correct |
53 |
Correct |
370 ms |
310004 KB |
Output is correct |
54 |
Correct |
482 ms |
311392 KB |
Output is correct |
55 |
Correct |
514 ms |
311544 KB |
Output is correct |
56 |
Correct |
631 ms |
311008 KB |
Output is correct |
57 |
Correct |
597 ms |
312508 KB |
Output is correct |
58 |
Correct |
410 ms |
310032 KB |
Output is correct |
59 |
Correct |
488 ms |
310880 KB |
Output is correct |