#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int INF = 1e14;
signed main() {
int k,n,m,o;cin>>k>>n>>m>>o;
int cl=(n/k)+1;
vector<vector<vector<vector<int>>>> lft(cl, vector<vector<vector<int>>>(k, vector<vector<int>>(18, vector<int>(k, INF))));
for (int i=0;i<m;i++) {
int a,b,t;cin>>a>>b>>t;
lft[a/k][a%k][0][b%k]=t;
}
for (int j=1;j<18;j++) {
for (int i=0;i<cl;i++) {
if (i + (1<<j) >= cl)break;
for (int t=0;t<k;t++) {
for (int md=0;md<k;md++) {
for (int tr=0;tr<k;tr++) {
lft[i][t][j][tr] = min(lft[i][t][j][tr], lft[i][t][j-1][md] + lft[i+(1<<(j-1))][md][j-1][tr]);
}
}
}
}
}
while (o--) {
int a,b;cin>>a>>b;
if ((a/k) == (b/k)) {
cout<<"-1\n";
continue;
}
int rw = a/k, co=a%k;
int steps = (b/k) - (a/k);
vector<int> jmp(k);
for (int i=0;i<k;i++) {
jmp[i] = lft[rw][co][0][i];
}
steps--;
rw++;
int cnt=0;
while (steps) {
if ((steps&1)==1) {
vector<int> tmpjmp(k, INF);
for (int i=0;i<k;i++) {
// use i as a starting point
for (int j=0;j<k;j++) {
// use j as an ending point
tmpjmp[j] = min(tmpjmp[j], jmp[i] + lft[rw][i][cnt][j]);
}
}
jmp = tmpjmp;
rw+=(1<<cnt);
}
steps>>=1;
cnt++;
}
if (jmp[b%k] >= INF) {
cout<<-1<<"\n";
continue;
}
cout<<jmp[b%k]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
53328 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 |
4 ms |
1372 KB |
Output is correct |
6 |
Correct |
5 ms |
1284 KB |
Output is correct |
7 |
Correct |
5 ms |
1528 KB |
Output is correct |
8 |
Correct |
151 ms |
53076 KB |
Output is correct |
9 |
Correct |
148 ms |
53080 KB |
Output is correct |
10 |
Correct |
108 ms |
53060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
52696 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
18 ms |
1368 KB |
Output is correct |
8 |
Correct |
19 ms |
1628 KB |
Output is correct |
9 |
Correct |
136 ms |
54136 KB |
Output is correct |
10 |
Correct |
308 ms |
54492 KB |
Output is correct |
11 |
Correct |
220 ms |
54356 KB |
Output is correct |
12 |
Correct |
234 ms |
53328 KB |
Output is correct |
13 |
Correct |
188 ms |
42068 KB |
Output is correct |
14 |
Correct |
184 ms |
32976 KB |
Output is correct |
15 |
Correct |
150 ms |
41052 KB |
Output is correct |
16 |
Correct |
143 ms |
41056 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
1368 KB |
Output is correct |
7 |
Correct |
3 ms |
1368 KB |
Output is correct |
8 |
Correct |
4 ms |
1760 KB |
Output is correct |
9 |
Correct |
4 ms |
1628 KB |
Output is correct |
10 |
Correct |
115 ms |
53252 KB |
Output is correct |
11 |
Correct |
205 ms |
52572 KB |
Output is correct |
12 |
Correct |
281 ms |
52304 KB |
Output is correct |
13 |
Correct |
256 ms |
52220 KB |
Output is correct |
14 |
Correct |
250 ms |
52308 KB |
Output is correct |
15 |
Correct |
140 ms |
39844 KB |
Output is correct |
16 |
Correct |
139 ms |
39836 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
1368 KB |
Output is correct |
7 |
Correct |
3 ms |
1368 KB |
Output is correct |
8 |
Correct |
4 ms |
1760 KB |
Output is correct |
9 |
Correct |
4 ms |
1628 KB |
Output is correct |
10 |
Correct |
115 ms |
53252 KB |
Output is correct |
11 |
Correct |
205 ms |
52572 KB |
Output is correct |
12 |
Correct |
281 ms |
52304 KB |
Output is correct |
13 |
Correct |
256 ms |
52220 KB |
Output is correct |
14 |
Correct |
250 ms |
52308 KB |
Output is correct |
15 |
Correct |
140 ms |
39844 KB |
Output is correct |
16 |
Correct |
139 ms |
39836 KB |
Output is correct |
17 |
Correct |
222 ms |
52680 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
7 ms |
1372 KB |
Output is correct |
24 |
Correct |
7 ms |
1372 KB |
Output is correct |
25 |
Correct |
9 ms |
1624 KB |
Output is correct |
26 |
Correct |
8 ms |
1628 KB |
Output is correct |
27 |
Correct |
136 ms |
53260 KB |
Output is correct |
28 |
Correct |
294 ms |
52048 KB |
Output is correct |
29 |
Correct |
269 ms |
52060 KB |
Output is correct |
30 |
Correct |
259 ms |
54072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
53328 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 |
4 ms |
1372 KB |
Output is correct |
6 |
Correct |
5 ms |
1284 KB |
Output is correct |
7 |
Correct |
5 ms |
1528 KB |
Output is correct |
8 |
Correct |
151 ms |
53076 KB |
Output is correct |
9 |
Correct |
148 ms |
53080 KB |
Output is correct |
10 |
Correct |
108 ms |
53060 KB |
Output is correct |
11 |
Correct |
229 ms |
52696 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
18 ms |
1368 KB |
Output is correct |
18 |
Correct |
19 ms |
1628 KB |
Output is correct |
19 |
Correct |
136 ms |
54136 KB |
Output is correct |
20 |
Correct |
308 ms |
54492 KB |
Output is correct |
21 |
Correct |
220 ms |
54356 KB |
Output is correct |
22 |
Correct |
234 ms |
53328 KB |
Output is correct |
23 |
Correct |
188 ms |
42068 KB |
Output is correct |
24 |
Correct |
184 ms |
32976 KB |
Output is correct |
25 |
Correct |
150 ms |
41052 KB |
Output is correct |
26 |
Correct |
143 ms |
41056 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 |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
1368 KB |
Output is correct |
33 |
Correct |
3 ms |
1368 KB |
Output is correct |
34 |
Correct |
4 ms |
1760 KB |
Output is correct |
35 |
Correct |
4 ms |
1628 KB |
Output is correct |
36 |
Correct |
115 ms |
53252 KB |
Output is correct |
37 |
Correct |
205 ms |
52572 KB |
Output is correct |
38 |
Correct |
281 ms |
52304 KB |
Output is correct |
39 |
Correct |
256 ms |
52220 KB |
Output is correct |
40 |
Correct |
250 ms |
52308 KB |
Output is correct |
41 |
Correct |
140 ms |
39844 KB |
Output is correct |
42 |
Correct |
139 ms |
39836 KB |
Output is correct |
43 |
Correct |
222 ms |
52680 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
7 ms |
1372 KB |
Output is correct |
50 |
Correct |
7 ms |
1372 KB |
Output is correct |
51 |
Correct |
9 ms |
1624 KB |
Output is correct |
52 |
Correct |
8 ms |
1628 KB |
Output is correct |
53 |
Correct |
136 ms |
53260 KB |
Output is correct |
54 |
Correct |
294 ms |
52048 KB |
Output is correct |
55 |
Correct |
269 ms |
52060 KB |
Output is correct |
56 |
Correct |
259 ms |
54072 KB |
Output is correct |
57 |
Correct |
362 ms |
69556 KB |
Output is correct |
58 |
Correct |
148 ms |
54208 KB |
Output is correct |
59 |
Correct |
247 ms |
54360 KB |
Output is correct |