#include"bits/stdc++.h"
using namespace std;
#define ll long long
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false)
#define inf LLONG_MAX
#define l first
#define r second
const int lmt=50001;
ll dp[lmt][17][5][5];
int main(){
fastio;
for(int i=0;i<lmt;i++){
for(int j=0;j<17;j++){
for(int k=0;k<5;k++){
for(int l=0;l<5;l++) dp[i][j][k][l]=1e15;
}
}
}
int k,n,m,q;
cin>>k>>n>>m>>q;
for(int i=1;i<=m;i++){
ll u,v,t;
cin>>u>>v>>t;
dp[u/k][0][u%k][v%k]=min(dp[u/k][0][u%k][v%k],t);
}
for(int mv=1;mv<17;mv++){
for(int i=0;i<=(n-1)/k;i++){
for(int j=0;j<k;j++){
for(int l=0;l<k;l++){
for(int o=0;o<k;o++){
int b=i+(1<<(mv-1));
if(b>(n-1)/k) continue;
dp[i][mv][j][l]=min(dp[i][mv][j][l],dp[i][mv-1][j][o]+dp[b][mv-1][o][l]);
}
}
}
}
}
/*for(int i=0;i<=(n-1)/k;i++){
for(int j=0;j<k;j++){
for(int l=0;l<k;l++){
for(int mv=0;mv<2;mv++){
if(dp[i][mv][j][l]==1e15) continue;
cout<<dp[i][mv][j][l]<<" "<<i<<" "<<mv<<" "<<j<<" "<<l<<endl;
}
}
}
}*/
while(q--){
int u,v;
cin>>u>>v;
int pre=u/k;
ll val[k];
for(int i=0;i<k;i++) val[i]=1e15;
val[u%k]=0;
int dis=v/k-u/k;
for(int j=16;j>=0;j--){
int b=pre+(1<<j);
if(b>v/k || dis<(1<<j)) continue;
dis-=(1<<j);
ll val2[k];
for(int i=0;i<k;i++) val2[i]=1e15;
for(int i=0;i<k;i++){
for(int l=0;l<k;l++){
val2[i]=min(val2[i],val[l]+dp[pre][j][l][i]);
}
}
for(int i=0;i<k;i++) val[i]=val2[i];
pre=b;
}
ll ans=val[v%k];
cout<<(ans==1e15?-1:ans)<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
166736 KB |
Output is correct |
2 |
Correct |
78 ms |
166604 KB |
Output is correct |
3 |
Correct |
72 ms |
166576 KB |
Output is correct |
4 |
Correct |
67 ms |
166620 KB |
Output is correct |
5 |
Correct |
70 ms |
166604 KB |
Output is correct |
6 |
Correct |
81 ms |
166664 KB |
Output is correct |
7 |
Correct |
85 ms |
166568 KB |
Output is correct |
8 |
Correct |
126 ms |
167564 KB |
Output is correct |
9 |
Correct |
119 ms |
167548 KB |
Output is correct |
10 |
Correct |
113 ms |
166768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
166696 KB |
Output is correct |
2 |
Correct |
67 ms |
166536 KB |
Output is correct |
3 |
Correct |
71 ms |
166628 KB |
Output is correct |
4 |
Correct |
71 ms |
166568 KB |
Output is correct |
5 |
Correct |
67 ms |
166608 KB |
Output is correct |
6 |
Correct |
66 ms |
166560 KB |
Output is correct |
7 |
Correct |
85 ms |
166632 KB |
Output is correct |
8 |
Correct |
87 ms |
166724 KB |
Output is correct |
9 |
Correct |
138 ms |
167436 KB |
Output is correct |
10 |
Correct |
159 ms |
168960 KB |
Output is correct |
11 |
Correct |
157 ms |
168316 KB |
Output is correct |
12 |
Correct |
150 ms |
167908 KB |
Output is correct |
13 |
Correct |
152 ms |
168972 KB |
Output is correct |
14 |
Correct |
128 ms |
168088 KB |
Output is correct |
15 |
Correct |
130 ms |
167908 KB |
Output is correct |
16 |
Correct |
133 ms |
167764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
166596 KB |
Output is correct |
2 |
Correct |
66 ms |
166516 KB |
Output is correct |
3 |
Correct |
71 ms |
166568 KB |
Output is correct |
4 |
Correct |
80 ms |
166588 KB |
Output is correct |
5 |
Correct |
82 ms |
166608 KB |
Output is correct |
6 |
Correct |
72 ms |
166660 KB |
Output is correct |
7 |
Correct |
68 ms |
166672 KB |
Output is correct |
8 |
Correct |
69 ms |
166664 KB |
Output is correct |
9 |
Correct |
69 ms |
166652 KB |
Output is correct |
10 |
Correct |
107 ms |
167496 KB |
Output is correct |
11 |
Correct |
162 ms |
168180 KB |
Output is correct |
12 |
Correct |
140 ms |
168820 KB |
Output is correct |
13 |
Correct |
149 ms |
169052 KB |
Output is correct |
14 |
Correct |
164 ms |
168424 KB |
Output is correct |
15 |
Correct |
112 ms |
167756 KB |
Output is correct |
16 |
Correct |
115 ms |
167788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
166596 KB |
Output is correct |
2 |
Correct |
66 ms |
166516 KB |
Output is correct |
3 |
Correct |
71 ms |
166568 KB |
Output is correct |
4 |
Correct |
80 ms |
166588 KB |
Output is correct |
5 |
Correct |
82 ms |
166608 KB |
Output is correct |
6 |
Correct |
72 ms |
166660 KB |
Output is correct |
7 |
Correct |
68 ms |
166672 KB |
Output is correct |
8 |
Correct |
69 ms |
166664 KB |
Output is correct |
9 |
Correct |
69 ms |
166652 KB |
Output is correct |
10 |
Correct |
107 ms |
167496 KB |
Output is correct |
11 |
Correct |
162 ms |
168180 KB |
Output is correct |
12 |
Correct |
140 ms |
168820 KB |
Output is correct |
13 |
Correct |
149 ms |
169052 KB |
Output is correct |
14 |
Correct |
164 ms |
168424 KB |
Output is correct |
15 |
Correct |
112 ms |
167756 KB |
Output is correct |
16 |
Correct |
115 ms |
167788 KB |
Output is correct |
17 |
Correct |
132 ms |
168164 KB |
Output is correct |
18 |
Correct |
70 ms |
166604 KB |
Output is correct |
19 |
Correct |
71 ms |
166568 KB |
Output is correct |
20 |
Correct |
68 ms |
166636 KB |
Output is correct |
21 |
Correct |
68 ms |
166532 KB |
Output is correct |
22 |
Correct |
72 ms |
166608 KB |
Output is correct |
23 |
Correct |
75 ms |
166600 KB |
Output is correct |
24 |
Correct |
78 ms |
166660 KB |
Output is correct |
25 |
Correct |
80 ms |
166688 KB |
Output is correct |
26 |
Correct |
85 ms |
166604 KB |
Output is correct |
27 |
Correct |
136 ms |
167396 KB |
Output is correct |
28 |
Correct |
161 ms |
168860 KB |
Output is correct |
29 |
Correct |
154 ms |
169144 KB |
Output is correct |
30 |
Correct |
182 ms |
168416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
166736 KB |
Output is correct |
2 |
Correct |
78 ms |
166604 KB |
Output is correct |
3 |
Correct |
72 ms |
166576 KB |
Output is correct |
4 |
Correct |
67 ms |
166620 KB |
Output is correct |
5 |
Correct |
70 ms |
166604 KB |
Output is correct |
6 |
Correct |
81 ms |
166664 KB |
Output is correct |
7 |
Correct |
85 ms |
166568 KB |
Output is correct |
8 |
Correct |
126 ms |
167564 KB |
Output is correct |
9 |
Correct |
119 ms |
167548 KB |
Output is correct |
10 |
Correct |
113 ms |
166768 KB |
Output is correct |
11 |
Correct |
154 ms |
166696 KB |
Output is correct |
12 |
Correct |
67 ms |
166536 KB |
Output is correct |
13 |
Correct |
71 ms |
166628 KB |
Output is correct |
14 |
Correct |
71 ms |
166568 KB |
Output is correct |
15 |
Correct |
67 ms |
166608 KB |
Output is correct |
16 |
Correct |
66 ms |
166560 KB |
Output is correct |
17 |
Correct |
85 ms |
166632 KB |
Output is correct |
18 |
Correct |
87 ms |
166724 KB |
Output is correct |
19 |
Correct |
138 ms |
167436 KB |
Output is correct |
20 |
Correct |
159 ms |
168960 KB |
Output is correct |
21 |
Correct |
157 ms |
168316 KB |
Output is correct |
22 |
Correct |
150 ms |
167908 KB |
Output is correct |
23 |
Correct |
152 ms |
168972 KB |
Output is correct |
24 |
Correct |
128 ms |
168088 KB |
Output is correct |
25 |
Correct |
130 ms |
167908 KB |
Output is correct |
26 |
Correct |
133 ms |
167764 KB |
Output is correct |
27 |
Correct |
70 ms |
166596 KB |
Output is correct |
28 |
Correct |
66 ms |
166516 KB |
Output is correct |
29 |
Correct |
71 ms |
166568 KB |
Output is correct |
30 |
Correct |
80 ms |
166588 KB |
Output is correct |
31 |
Correct |
82 ms |
166608 KB |
Output is correct |
32 |
Correct |
72 ms |
166660 KB |
Output is correct |
33 |
Correct |
68 ms |
166672 KB |
Output is correct |
34 |
Correct |
69 ms |
166664 KB |
Output is correct |
35 |
Correct |
69 ms |
166652 KB |
Output is correct |
36 |
Correct |
107 ms |
167496 KB |
Output is correct |
37 |
Correct |
162 ms |
168180 KB |
Output is correct |
38 |
Correct |
140 ms |
168820 KB |
Output is correct |
39 |
Correct |
149 ms |
169052 KB |
Output is correct |
40 |
Correct |
164 ms |
168424 KB |
Output is correct |
41 |
Correct |
112 ms |
167756 KB |
Output is correct |
42 |
Correct |
115 ms |
167788 KB |
Output is correct |
43 |
Correct |
132 ms |
168164 KB |
Output is correct |
44 |
Correct |
70 ms |
166604 KB |
Output is correct |
45 |
Correct |
71 ms |
166568 KB |
Output is correct |
46 |
Correct |
68 ms |
166636 KB |
Output is correct |
47 |
Correct |
68 ms |
166532 KB |
Output is correct |
48 |
Correct |
72 ms |
166608 KB |
Output is correct |
49 |
Correct |
75 ms |
166600 KB |
Output is correct |
50 |
Correct |
78 ms |
166660 KB |
Output is correct |
51 |
Correct |
80 ms |
166688 KB |
Output is correct |
52 |
Correct |
85 ms |
166604 KB |
Output is correct |
53 |
Correct |
136 ms |
167396 KB |
Output is correct |
54 |
Correct |
161 ms |
168860 KB |
Output is correct |
55 |
Correct |
154 ms |
169144 KB |
Output is correct |
56 |
Correct |
182 ms |
168416 KB |
Output is correct |
57 |
Correct |
199 ms |
169956 KB |
Output is correct |
58 |
Correct |
139 ms |
167480 KB |
Output is correct |
59 |
Correct |
196 ms |
168432 KB |
Output is correct |