#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 |
136 ms |
196932 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
4176 KB |
Output is correct |
6 |
Correct |
4 ms |
4172 KB |
Output is correct |
7 |
Correct |
3 ms |
4172 KB |
Output is correct |
8 |
Correct |
136 ms |
196960 KB |
Output is correct |
9 |
Correct |
139 ms |
196876 KB |
Output is correct |
10 |
Correct |
117 ms |
196124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
99936 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
9 ms |
4300 KB |
Output is correct |
8 |
Correct |
14 ms |
2380 KB |
Output is correct |
9 |
Correct |
131 ms |
196868 KB |
Output is correct |
10 |
Correct |
147 ms |
67884 KB |
Output is correct |
11 |
Correct |
140 ms |
99844 KB |
Output is correct |
12 |
Correct |
112 ms |
66800 KB |
Output is correct |
13 |
Correct |
101 ms |
26228 KB |
Output is correct |
14 |
Correct |
69 ms |
40812 KB |
Output is correct |
15 |
Correct |
70 ms |
25008 KB |
Output is correct |
16 |
Correct |
75 ms |
25024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
4172 KB |
Output is correct |
7 |
Correct |
3 ms |
2252 KB |
Output is correct |
8 |
Correct |
2 ms |
1100 KB |
Output is correct |
9 |
Correct |
3 ms |
1356 KB |
Output is correct |
10 |
Correct |
135 ms |
196756 KB |
Output is correct |
11 |
Correct |
139 ms |
99680 KB |
Output is correct |
12 |
Correct |
126 ms |
67696 KB |
Output is correct |
13 |
Correct |
143 ms |
67956 KB |
Output is correct |
14 |
Correct |
114 ms |
67344 KB |
Output is correct |
15 |
Correct |
55 ms |
24976 KB |
Output is correct |
16 |
Correct |
56 ms |
24932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
4172 KB |
Output is correct |
7 |
Correct |
3 ms |
2252 KB |
Output is correct |
8 |
Correct |
2 ms |
1100 KB |
Output is correct |
9 |
Correct |
3 ms |
1356 KB |
Output is correct |
10 |
Correct |
135 ms |
196756 KB |
Output is correct |
11 |
Correct |
139 ms |
99680 KB |
Output is correct |
12 |
Correct |
126 ms |
67696 KB |
Output is correct |
13 |
Correct |
143 ms |
67956 KB |
Output is correct |
14 |
Correct |
114 ms |
67344 KB |
Output is correct |
15 |
Correct |
55 ms |
24976 KB |
Output is correct |
16 |
Correct |
56 ms |
24932 KB |
Output is correct |
17 |
Correct |
128 ms |
99732 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
4 ms |
4172 KB |
Output is correct |
24 |
Correct |
4 ms |
2252 KB |
Output is correct |
25 |
Correct |
8 ms |
1104 KB |
Output is correct |
26 |
Correct |
7 ms |
1364 KB |
Output is correct |
27 |
Correct |
135 ms |
196832 KB |
Output is correct |
28 |
Correct |
121 ms |
67736 KB |
Output is correct |
29 |
Correct |
139 ms |
67952 KB |
Output is correct |
30 |
Correct |
143 ms |
67380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
196932 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
4176 KB |
Output is correct |
6 |
Correct |
4 ms |
4172 KB |
Output is correct |
7 |
Correct |
3 ms |
4172 KB |
Output is correct |
8 |
Correct |
136 ms |
196960 KB |
Output is correct |
9 |
Correct |
139 ms |
196876 KB |
Output is correct |
10 |
Correct |
117 ms |
196124 KB |
Output is correct |
11 |
Correct |
130 ms |
99936 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
9 ms |
4300 KB |
Output is correct |
18 |
Correct |
14 ms |
2380 KB |
Output is correct |
19 |
Correct |
131 ms |
196868 KB |
Output is correct |
20 |
Correct |
147 ms |
67884 KB |
Output is correct |
21 |
Correct |
140 ms |
99844 KB |
Output is correct |
22 |
Correct |
112 ms |
66800 KB |
Output is correct |
23 |
Correct |
101 ms |
26228 KB |
Output is correct |
24 |
Correct |
69 ms |
40812 KB |
Output is correct |
25 |
Correct |
70 ms |
25008 KB |
Output is correct |
26 |
Correct |
75 ms |
25024 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
4172 KB |
Output is correct |
33 |
Correct |
3 ms |
2252 KB |
Output is correct |
34 |
Correct |
2 ms |
1100 KB |
Output is correct |
35 |
Correct |
3 ms |
1356 KB |
Output is correct |
36 |
Correct |
135 ms |
196756 KB |
Output is correct |
37 |
Correct |
139 ms |
99680 KB |
Output is correct |
38 |
Correct |
126 ms |
67696 KB |
Output is correct |
39 |
Correct |
143 ms |
67956 KB |
Output is correct |
40 |
Correct |
114 ms |
67344 KB |
Output is correct |
41 |
Correct |
55 ms |
24976 KB |
Output is correct |
42 |
Correct |
56 ms |
24932 KB |
Output is correct |
43 |
Correct |
128 ms |
99732 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
4 ms |
4172 KB |
Output is correct |
50 |
Correct |
4 ms |
2252 KB |
Output is correct |
51 |
Correct |
8 ms |
1104 KB |
Output is correct |
52 |
Correct |
7 ms |
1364 KB |
Output is correct |
53 |
Correct |
135 ms |
196832 KB |
Output is correct |
54 |
Correct |
121 ms |
67736 KB |
Output is correct |
55 |
Correct |
139 ms |
67952 KB |
Output is correct |
56 |
Correct |
143 ms |
67380 KB |
Output is correct |
57 |
Correct |
152 ms |
52524 KB |
Output is correct |
58 |
Correct |
147 ms |
196856 KB |
Output is correct |
59 |
Correct |
164 ms |
99900 KB |
Output is correct |