#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
const int INF = 1000000000;
ll dp[50500][20][5][5];
const int MXLOG = 20;
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int k, n, q, m;
cin >> k >> n >> m >> q;
for(int i=0; i<=n/k; i++){
for(int j=0; j<k; j++){
for(int g=0; g<k; g++){
dp[i][0][j][g] = INF;
}
}
}
for(int i=0; i<m; i++){
int a, b, c;
cin >> a >> b >> c;
dp[a/k][0][a%k][b%k] = c;
}
for(int j=1; j<MXLOG; j++){
for(int i=0; i<=n/k; i++){
for(int md=0; md<k; md++){
for(int fn=0; fn<k; fn++){
dp[i][j][md][fn] = INF;
if(i+(1<<j) > n/k) continue;
for(int sr=0; sr<k; sr++){
dp[i][j][md][fn] = min(dp[i][j][md][fn], dp[i][j-1][md][sr] + dp[i+(1<<(j-1))][j-1][sr][fn]);
}
}
}
}
}
while(q--){
int a, b;
cin >> a >> b;
int x = a/k;
int y = b/k;
vector <ll> res;
for(int i=0; i<k; i++) res.push_back(INF);
res[a%k] = 0;
for(int st=MXLOG-1; st>=0; st--){
if(y-x >= (1<<st)){
vector <ll> nres;
for(int gg=0; gg<k; gg++) nres.push_back(INF);
for(int pc=0; pc<k; pc++){
for(int fn=0; fn<k; fn++){
nres[fn] = min(nres[fn], res[pc] + dp[x][st][pc][fn]);
}
}
res = nres;
x += (1<<st);
}
}
if(res[b%k] >= INF) cout << "-1\n";
else cout << res[b%k] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
197100 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
4 ms |
4332 KB |
Output is correct |
6 |
Correct |
3 ms |
4332 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
8 |
Correct |
158 ms |
196972 KB |
Output is correct |
9 |
Correct |
152 ms |
196972 KB |
Output is correct |
10 |
Correct |
135 ms |
196332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
99180 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
4332 KB |
Output is correct |
8 |
Correct |
10 ms |
2412 KB |
Output is correct |
9 |
Correct |
147 ms |
197116 KB |
Output is correct |
10 |
Correct |
136 ms |
66516 KB |
Output is correct |
11 |
Correct |
139 ms |
99200 KB |
Output is correct |
12 |
Correct |
112 ms |
66540 KB |
Output is correct |
13 |
Correct |
90 ms |
24812 KB |
Output is correct |
14 |
Correct |
76 ms |
40428 KB |
Output is correct |
15 |
Correct |
67 ms |
24800 KB |
Output is correct |
16 |
Correct |
67 ms |
24812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
3 ms |
4332 KB |
Output is correct |
7 |
Correct |
2 ms |
2284 KB |
Output is correct |
8 |
Correct |
2 ms |
1132 KB |
Output is correct |
9 |
Correct |
2 ms |
1388 KB |
Output is correct |
10 |
Correct |
136 ms |
196844 KB |
Output is correct |
11 |
Correct |
127 ms |
99180 KB |
Output is correct |
12 |
Correct |
118 ms |
66560 KB |
Output is correct |
13 |
Correct |
130 ms |
66540 KB |
Output is correct |
14 |
Correct |
112 ms |
66412 KB |
Output is correct |
15 |
Correct |
62 ms |
24684 KB |
Output is correct |
16 |
Correct |
61 ms |
24684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
3 ms |
4332 KB |
Output is correct |
7 |
Correct |
2 ms |
2284 KB |
Output is correct |
8 |
Correct |
2 ms |
1132 KB |
Output is correct |
9 |
Correct |
2 ms |
1388 KB |
Output is correct |
10 |
Correct |
136 ms |
196844 KB |
Output is correct |
11 |
Correct |
127 ms |
99180 KB |
Output is correct |
12 |
Correct |
118 ms |
66560 KB |
Output is correct |
13 |
Correct |
130 ms |
66540 KB |
Output is correct |
14 |
Correct |
112 ms |
66412 KB |
Output is correct |
15 |
Correct |
62 ms |
24684 KB |
Output is correct |
16 |
Correct |
61 ms |
24684 KB |
Output is correct |
17 |
Correct |
131 ms |
99180 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
4 ms |
4332 KB |
Output is correct |
24 |
Correct |
4 ms |
2412 KB |
Output is correct |
25 |
Correct |
6 ms |
1260 KB |
Output is correct |
26 |
Correct |
5 ms |
1388 KB |
Output is correct |
27 |
Correct |
141 ms |
196844 KB |
Output is correct |
28 |
Correct |
126 ms |
66540 KB |
Output is correct |
29 |
Correct |
133 ms |
66540 KB |
Output is correct |
30 |
Correct |
119 ms |
66540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
197100 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
4 ms |
4332 KB |
Output is correct |
6 |
Correct |
3 ms |
4332 KB |
Output is correct |
7 |
Correct |
4 ms |
4332 KB |
Output is correct |
8 |
Correct |
158 ms |
196972 KB |
Output is correct |
9 |
Correct |
152 ms |
196972 KB |
Output is correct |
10 |
Correct |
135 ms |
196332 KB |
Output is correct |
11 |
Correct |
140 ms |
99180 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
7 ms |
4332 KB |
Output is correct |
18 |
Correct |
10 ms |
2412 KB |
Output is correct |
19 |
Correct |
147 ms |
197116 KB |
Output is correct |
20 |
Correct |
136 ms |
66516 KB |
Output is correct |
21 |
Correct |
139 ms |
99200 KB |
Output is correct |
22 |
Correct |
112 ms |
66540 KB |
Output is correct |
23 |
Correct |
90 ms |
24812 KB |
Output is correct |
24 |
Correct |
76 ms |
40428 KB |
Output is correct |
25 |
Correct |
67 ms |
24800 KB |
Output is correct |
26 |
Correct |
67 ms |
24812 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
3 ms |
4332 KB |
Output is correct |
33 |
Correct |
2 ms |
2284 KB |
Output is correct |
34 |
Correct |
2 ms |
1132 KB |
Output is correct |
35 |
Correct |
2 ms |
1388 KB |
Output is correct |
36 |
Correct |
136 ms |
196844 KB |
Output is correct |
37 |
Correct |
127 ms |
99180 KB |
Output is correct |
38 |
Correct |
118 ms |
66560 KB |
Output is correct |
39 |
Correct |
130 ms |
66540 KB |
Output is correct |
40 |
Correct |
112 ms |
66412 KB |
Output is correct |
41 |
Correct |
62 ms |
24684 KB |
Output is correct |
42 |
Correct |
61 ms |
24684 KB |
Output is correct |
43 |
Correct |
131 ms |
99180 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
4 ms |
4332 KB |
Output is correct |
50 |
Correct |
4 ms |
2412 KB |
Output is correct |
51 |
Correct |
6 ms |
1260 KB |
Output is correct |
52 |
Correct |
5 ms |
1388 KB |
Output is correct |
53 |
Correct |
141 ms |
196844 KB |
Output is correct |
54 |
Correct |
126 ms |
66540 KB |
Output is correct |
55 |
Correct |
133 ms |
66540 KB |
Output is correct |
56 |
Correct |
119 ms |
66540 KB |
Output is correct |
57 |
Correct |
157 ms |
50284 KB |
Output is correct |
58 |
Correct |
153 ms |
196972 KB |
Output is correct |
59 |
Correct |
149 ms |
99180 KB |
Output is correct |