#include <bits/stdc++.h>
using namespace std;
int const N=2e5;
int const inf=1e9+5;
int wei[N][20][6];
int main(){
int k,n,m,o;
cin>>k>>n>>m>>o;
for(int i=0;i<n;i++)
for(int j=0;j<=16;j++)
for(int kk=0;kk<=5;kk++)
wei[i][j][kk]=inf;
for(int i=0;i<m;i++){
int a,b,w;
cin>>a>>b>>w;
// swap(a,b);//switch directions;
wei[a][0][b%k]=w;
}
//compute
int midnode=0;
for(int par=1;par<=15;par++){
for(int node=0;node<n;node++){
midnode=(((1<<(par-1))+(node/k))*k);
for(int md=0;md<k;md++){
for(int d=0;d<k;d++){
wei[node][par][md]=min(wei[node][par][md],(wei[node][par-1][d]+wei[midnode+d][par-1][md]));
}
}
}
}
while(o--){
int a,b;
cin>>a>>b;
int d=(b/k)-(a/k);
if(d<0){
cout<<-1<<endl;
continue;
}
int w[k],ww[k];
for(int i=0;i<k;i++)
w[i]=inf;
w[a%k]=0;
int cur=a/k;
while(d){
int par=31-(__builtin_clz(d));
d-=(1<<par);
// cout<<"par "<<par<<endl;
for(int i=0;i<k;i++){
int mn=inf;
for(int j=0;j<k;j++)
mn=min(mn,w[j]+wei[(cur*k)+j][par][i]);
ww[i]=mn;
}
for(int i=0;i<k;i++)
w[i]=ww[i];
cur+=(1<<par);
}
if(w[b%k]>=inf)
w[b%k]=-1;
cout<<w[b%k]<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
25180 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
2648 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2904 KB |
Output is correct |
8 |
Correct |
58 ms |
25180 KB |
Output is correct |
9 |
Correct |
55 ms |
25236 KB |
Output is correct |
10 |
Correct |
28 ms |
25176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
25232 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
16 ms |
2652 KB |
Output is correct |
8 |
Correct |
21 ms |
2652 KB |
Output is correct |
9 |
Correct |
61 ms |
25220 KB |
Output is correct |
10 |
Correct |
116 ms |
25176 KB |
Output is correct |
11 |
Correct |
87 ms |
25236 KB |
Output is correct |
12 |
Correct |
94 ms |
25180 KB |
Output is correct |
13 |
Correct |
135 ms |
14936 KB |
Output is correct |
14 |
Correct |
66 ms |
14956 KB |
Output is correct |
15 |
Correct |
76 ms |
14960 KB |
Output is correct |
16 |
Correct |
86 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
2444 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
3 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
45 ms |
25428 KB |
Output is correct |
11 |
Correct |
77 ms |
25216 KB |
Output is correct |
12 |
Correct |
98 ms |
25232 KB |
Output is correct |
13 |
Correct |
110 ms |
25220 KB |
Output is correct |
14 |
Correct |
93 ms |
25220 KB |
Output is correct |
15 |
Correct |
61 ms |
14936 KB |
Output is correct |
16 |
Correct |
64 ms |
15192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
2444 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
3 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
45 ms |
25428 KB |
Output is correct |
11 |
Correct |
77 ms |
25216 KB |
Output is correct |
12 |
Correct |
98 ms |
25232 KB |
Output is correct |
13 |
Correct |
110 ms |
25220 KB |
Output is correct |
14 |
Correct |
93 ms |
25220 KB |
Output is correct |
15 |
Correct |
61 ms |
14936 KB |
Output is correct |
16 |
Correct |
64 ms |
15192 KB |
Output is correct |
17 |
Correct |
81 ms |
25220 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
6 ms |
2652 KB |
Output is correct |
24 |
Correct |
6 ms |
2652 KB |
Output is correct |
25 |
Correct |
8 ms |
2652 KB |
Output is correct |
26 |
Correct |
7 ms |
2716 KB |
Output is correct |
27 |
Correct |
45 ms |
25240 KB |
Output is correct |
28 |
Correct |
105 ms |
25180 KB |
Output is correct |
29 |
Correct |
113 ms |
25484 KB |
Output is correct |
30 |
Correct |
95 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
25180 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
2648 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2904 KB |
Output is correct |
8 |
Correct |
58 ms |
25180 KB |
Output is correct |
9 |
Correct |
55 ms |
25236 KB |
Output is correct |
10 |
Correct |
28 ms |
25176 KB |
Output is correct |
11 |
Correct |
85 ms |
25232 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
16 ms |
2652 KB |
Output is correct |
18 |
Correct |
21 ms |
2652 KB |
Output is correct |
19 |
Correct |
61 ms |
25220 KB |
Output is correct |
20 |
Correct |
116 ms |
25176 KB |
Output is correct |
21 |
Correct |
87 ms |
25236 KB |
Output is correct |
22 |
Correct |
94 ms |
25180 KB |
Output is correct |
23 |
Correct |
135 ms |
14936 KB |
Output is correct |
24 |
Correct |
66 ms |
14956 KB |
Output is correct |
25 |
Correct |
76 ms |
14960 KB |
Output is correct |
26 |
Correct |
86 ms |
14940 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
2444 KB |
Output is correct |
33 |
Correct |
2 ms |
2648 KB |
Output is correct |
34 |
Correct |
3 ms |
2652 KB |
Output is correct |
35 |
Correct |
3 ms |
2652 KB |
Output is correct |
36 |
Correct |
45 ms |
25428 KB |
Output is correct |
37 |
Correct |
77 ms |
25216 KB |
Output is correct |
38 |
Correct |
98 ms |
25232 KB |
Output is correct |
39 |
Correct |
110 ms |
25220 KB |
Output is correct |
40 |
Correct |
93 ms |
25220 KB |
Output is correct |
41 |
Correct |
61 ms |
14936 KB |
Output is correct |
42 |
Correct |
64 ms |
15192 KB |
Output is correct |
43 |
Correct |
81 ms |
25220 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
6 ms |
2652 KB |
Output is correct |
50 |
Correct |
6 ms |
2652 KB |
Output is correct |
51 |
Correct |
8 ms |
2652 KB |
Output is correct |
52 |
Correct |
7 ms |
2716 KB |
Output is correct |
53 |
Correct |
45 ms |
25240 KB |
Output is correct |
54 |
Correct |
105 ms |
25180 KB |
Output is correct |
55 |
Correct |
113 ms |
25484 KB |
Output is correct |
56 |
Correct |
95 ms |
25180 KB |
Output is correct |
57 |
Correct |
177 ms |
25180 KB |
Output is correct |
58 |
Correct |
58 ms |
25176 KB |
Output is correct |
59 |
Correct |
98 ms |
25680 KB |
Output is correct |