#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e10;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int k,n,m,q;
cin >> k >> n >> m >> q;
auto combine = [&](vector<vector<int>> DPa,vector<vector<int>> DPb){
vector DPres(k,vector(k,INF));
for(int l=0;l<k;l++){
for(int r=0;r<k;r++){
for(int x=0;x<k;x++){
DPres[l][r]=min(DPres[l][r],DPa[l][x]+DPb[x][r]);
}
}
}
return DPres;
};
int totBlocks = n/k;
vector lift(totBlocks+1,vector(16,vector(k,vector(k,INF))));
for(int i=1;i<=m;i++){
int a,b,t;cin>>a>>b>>t;
int block = a/k;
a%=k;b%=k;
lift[block][0][a][b]=t;
}
for(int bit=1;bit<16;bit++){
for(int i=0;i<=totBlocks;i++){
if(i+(1<<bit)>totBlocks)continue;
lift[i][bit]=combine(lift[i][bit-1],lift[i+(1<<(bit-1))][bit-1]);
}
}
for(int i=1;i<=q;i++){
int a,b;cin>>a>>b;
int blockA = a/k;a%=k;
int blockB = b/k;b%=k;
int tar = blockB-blockA;
vector res(k,vector(k,INF));
for(int j=0;j<k;j++)res[j][j]=0;
for(int bit=0;bit<16;bit++)if(tar&(1<<bit)){
res=combine(res,lift[blockA][bit]);
blockA+=(1<<bit);
}
if(res[a][b]>=INF)cout<<"-1\n";
else cout<<res[a][b]<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
71200 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
1628 KB |
Output is correct |
6 |
Correct |
2 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1884 KB |
Output is correct |
8 |
Correct |
188 ms |
71248 KB |
Output is correct |
9 |
Correct |
189 ms |
71148 KB |
Output is correct |
10 |
Correct |
223 ms |
71248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
60752 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
8 ms |
1884 KB |
Output is correct |
8 |
Correct |
10 ms |
1744 KB |
Output is correct |
9 |
Correct |
189 ms |
71288 KB |
Output is correct |
10 |
Correct |
156 ms |
53308 KB |
Output is correct |
11 |
Correct |
150 ms |
60844 KB |
Output is correct |
12 |
Correct |
127 ms |
53072 KB |
Output is correct |
13 |
Correct |
89 ms |
37468 KB |
Output is correct |
14 |
Correct |
71 ms |
32148 KB |
Output is correct |
15 |
Correct |
73 ms |
37464 KB |
Output is correct |
16 |
Correct |
87 ms |
37516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
3 ms |
1872 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
186 ms |
71240 KB |
Output is correct |
11 |
Correct |
135 ms |
60752 KB |
Output is correct |
12 |
Correct |
124 ms |
53048 KB |
Output is correct |
13 |
Correct |
127 ms |
53072 KB |
Output is correct |
14 |
Correct |
127 ms |
53204 KB |
Output is correct |
15 |
Correct |
76 ms |
37464 KB |
Output is correct |
16 |
Correct |
64 ms |
37468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
3 ms |
1872 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
186 ms |
71240 KB |
Output is correct |
11 |
Correct |
135 ms |
60752 KB |
Output is correct |
12 |
Correct |
124 ms |
53048 KB |
Output is correct |
13 |
Correct |
127 ms |
53072 KB |
Output is correct |
14 |
Correct |
127 ms |
53204 KB |
Output is correct |
15 |
Correct |
76 ms |
37464 KB |
Output is correct |
16 |
Correct |
64 ms |
37468 KB |
Output is correct |
17 |
Correct |
146 ms |
60752 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
1884 KB |
Output is correct |
24 |
Correct |
5 ms |
1676 KB |
Output is correct |
25 |
Correct |
7 ms |
1712 KB |
Output is correct |
26 |
Correct |
5 ms |
1628 KB |
Output is correct |
27 |
Correct |
199 ms |
71284 KB |
Output is correct |
28 |
Correct |
139 ms |
53084 KB |
Output is correct |
29 |
Correct |
137 ms |
53192 KB |
Output is correct |
30 |
Correct |
134 ms |
53080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
71200 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
1628 KB |
Output is correct |
6 |
Correct |
2 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1884 KB |
Output is correct |
8 |
Correct |
188 ms |
71248 KB |
Output is correct |
9 |
Correct |
189 ms |
71148 KB |
Output is correct |
10 |
Correct |
223 ms |
71248 KB |
Output is correct |
11 |
Correct |
146 ms |
60752 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
8 ms |
1884 KB |
Output is correct |
18 |
Correct |
10 ms |
1744 KB |
Output is correct |
19 |
Correct |
189 ms |
71288 KB |
Output is correct |
20 |
Correct |
156 ms |
53308 KB |
Output is correct |
21 |
Correct |
150 ms |
60844 KB |
Output is correct |
22 |
Correct |
127 ms |
53072 KB |
Output is correct |
23 |
Correct |
89 ms |
37468 KB |
Output is correct |
24 |
Correct |
71 ms |
32148 KB |
Output is correct |
25 |
Correct |
73 ms |
37464 KB |
Output is correct |
26 |
Correct |
87 ms |
37516 KB |
Output is correct |
27 |
Correct |
0 ms |
344 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 |
3 ms |
1872 KB |
Output is correct |
33 |
Correct |
2 ms |
1628 KB |
Output is correct |
34 |
Correct |
2 ms |
1628 KB |
Output is correct |
35 |
Correct |
2 ms |
1628 KB |
Output is correct |
36 |
Correct |
186 ms |
71240 KB |
Output is correct |
37 |
Correct |
135 ms |
60752 KB |
Output is correct |
38 |
Correct |
124 ms |
53048 KB |
Output is correct |
39 |
Correct |
127 ms |
53072 KB |
Output is correct |
40 |
Correct |
127 ms |
53204 KB |
Output is correct |
41 |
Correct |
76 ms |
37464 KB |
Output is correct |
42 |
Correct |
64 ms |
37468 KB |
Output is correct |
43 |
Correct |
146 ms |
60752 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
5 ms |
1884 KB |
Output is correct |
50 |
Correct |
5 ms |
1676 KB |
Output is correct |
51 |
Correct |
7 ms |
1712 KB |
Output is correct |
52 |
Correct |
5 ms |
1628 KB |
Output is correct |
53 |
Correct |
199 ms |
71284 KB |
Output is correct |
54 |
Correct |
139 ms |
53084 KB |
Output is correct |
55 |
Correct |
137 ms |
53192 KB |
Output is correct |
56 |
Correct |
134 ms |
53080 KB |
Output is correct |
57 |
Correct |
166 ms |
65064 KB |
Output is correct |
58 |
Correct |
204 ms |
71172 KB |
Output is correct |
59 |
Correct |
159 ms |
61008 KB |
Output is correct |