#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=(int)ceil(n/k);
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 |
173 ms |
54060 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
392 KB |
Output is correct |
5 |
Correct |
3 ms |
1372 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
4 ms |
1372 KB |
Output is correct |
8 |
Correct |
158 ms |
54180 KB |
Output is correct |
9 |
Correct |
157 ms |
53844 KB |
Output is correct |
10 |
Correct |
117 ms |
53276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
53700 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
4 ms |
1624 KB |
Output is correct |
9 |
Correct |
4 ms |
1628 KB |
Output is correct |
10 |
Correct |
131 ms |
54260 KB |
Output is correct |
11 |
Correct |
217 ms |
53588 KB |
Output is correct |
12 |
Correct |
285 ms |
53688 KB |
Output is correct |
13 |
Correct |
307 ms |
53868 KB |
Output is correct |
14 |
Correct |
244 ms |
53368 KB |
Output is correct |
15 |
Correct |
161 ms |
40528 KB |
Output is correct |
16 |
Correct |
147 ms |
40736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1372 KB |
Output is correct |
8 |
Correct |
4 ms |
1624 KB |
Output is correct |
9 |
Correct |
4 ms |
1628 KB |
Output is correct |
10 |
Correct |
131 ms |
54260 KB |
Output is correct |
11 |
Correct |
217 ms |
53588 KB |
Output is correct |
12 |
Correct |
285 ms |
53688 KB |
Output is correct |
13 |
Correct |
307 ms |
53868 KB |
Output is correct |
14 |
Correct |
244 ms |
53368 KB |
Output is correct |
15 |
Correct |
161 ms |
40528 KB |
Output is correct |
16 |
Correct |
147 ms |
40736 KB |
Output is correct |
17 |
Correct |
223 ms |
53588 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 |
344 KB |
Output is correct |
21 |
Correct |
0 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 |
8 ms |
1372 KB |
Output is correct |
25 |
Correct |
9 ms |
1628 KB |
Output is correct |
26 |
Correct |
11 ms |
2080 KB |
Output is correct |
27 |
Correct |
134 ms |
53852 KB |
Output is correct |
28 |
Correct |
308 ms |
53496 KB |
Output is correct |
29 |
Incorrect |
307 ms |
53760 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
54060 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
392 KB |
Output is correct |
5 |
Correct |
3 ms |
1372 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
4 ms |
1372 KB |
Output is correct |
8 |
Correct |
158 ms |
54180 KB |
Output is correct |
9 |
Correct |
157 ms |
53844 KB |
Output is correct |
10 |
Correct |
117 ms |
53276 KB |
Output is correct |
11 |
Correct |
245 ms |
53700 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |