#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
using namespace std;
ll dp[50001][17][5][5];int k;
void pre(int i,int j,int ii,int jj,int iii,int jjj){
for(int x=0;x<k;x++){
for(int y=0;y<k;y++){
for(int z=0;z<k;z++){
dp[i][j][x][y]=min(dp[i][j][x][y],dp[ii][jj][x][z]+dp[iii][jjj][z][y]);
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m,o;cin>>k>>n>>m>>o;
for(int i=0;i<50001;i++)for(int j=0;j<17;j++)for(int t=0;t<5;t++)for(int l=0;l<5;l++)dp[i][j][t][l]=1e15;
while(m--){
int a,b,t;cin>>a>>b>>t;
dp[a/k][0][a%k][b%k]=t;
}
for(int j=1;j<17;j++){
for(int i=0;i<(n+k-1)/k;i++){
if(i+(1<<(j-1))<50001)pre(i,j,i ,j-1,i+(1<<(j-1)),j-1);
}
}ll ans[5][5],tmp[5][5];
while(o--){
int a,b;cin>>a>>b;
for(int x=0;x<k;x++)for(int y=0;y<k;y++)ans[x][y]=1e15;
for(int i=0;i<k;i++)ans[i][i]=0;
int cur=a/k,dest=b/k;
for(int i=16;i>=0;i--){
if(cur+(1<<i)<=dest){
for(int x=0;x<k;x++)for(int y=0;y<k;y++)tmp[x][y]=1e15;
for(int x=0;x<k;x++){
for(int y=0;y<k;y++){
for(int z=0;z<k;z++){
tmp[x][y]=min(tmp[x][y],ans[x][z]+dp[cur][i][z][y]);
}
}
}for(int x=0;x<k;x++)for(int y=0;y<k;y++)ans[x][y]=tmp[x][y];
cur+=(1<<i);
}
}cout<<(ans[a%k][b%k]==1e15?-1:ans[a%k][b%k])<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
166976 KB |
Output is correct |
2 |
Correct |
30 ms |
166736 KB |
Output is correct |
3 |
Correct |
28 ms |
166744 KB |
Output is correct |
4 |
Correct |
28 ms |
166524 KB |
Output is correct |
5 |
Correct |
30 ms |
166584 KB |
Output is correct |
6 |
Correct |
31 ms |
166740 KB |
Output is correct |
7 |
Correct |
33 ms |
166744 KB |
Output is correct |
8 |
Correct |
78 ms |
166776 KB |
Output is correct |
9 |
Correct |
77 ms |
166780 KB |
Output is correct |
10 |
Correct |
63 ms |
166588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
166736 KB |
Output is correct |
2 |
Correct |
39 ms |
166740 KB |
Output is correct |
3 |
Correct |
31 ms |
166740 KB |
Output is correct |
4 |
Correct |
31 ms |
166780 KB |
Output is correct |
5 |
Correct |
31 ms |
166572 KB |
Output is correct |
6 |
Correct |
31 ms |
166684 KB |
Output is correct |
7 |
Correct |
34 ms |
166860 KB |
Output is correct |
8 |
Correct |
37 ms |
166880 KB |
Output is correct |
9 |
Correct |
71 ms |
167508 KB |
Output is correct |
10 |
Correct |
128 ms |
169048 KB |
Output is correct |
11 |
Correct |
100 ms |
168532 KB |
Output is correct |
12 |
Correct |
104 ms |
168060 KB |
Output is correct |
13 |
Correct |
89 ms |
169076 KB |
Output is correct |
14 |
Correct |
81 ms |
168044 KB |
Output is correct |
15 |
Correct |
78 ms |
167788 KB |
Output is correct |
16 |
Correct |
68 ms |
167980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
166716 KB |
Output is correct |
2 |
Correct |
29 ms |
166744 KB |
Output is correct |
3 |
Correct |
30 ms |
166740 KB |
Output is correct |
4 |
Correct |
30 ms |
166740 KB |
Output is correct |
5 |
Correct |
29 ms |
166732 KB |
Output is correct |
6 |
Correct |
32 ms |
166740 KB |
Output is correct |
7 |
Correct |
30 ms |
166748 KB |
Output is correct |
8 |
Correct |
31 ms |
166704 KB |
Output is correct |
9 |
Correct |
32 ms |
166796 KB |
Output is correct |
10 |
Correct |
68 ms |
167364 KB |
Output is correct |
11 |
Correct |
96 ms |
168300 KB |
Output is correct |
12 |
Correct |
114 ms |
168956 KB |
Output is correct |
13 |
Correct |
118 ms |
169192 KB |
Output is correct |
14 |
Correct |
114 ms |
168588 KB |
Output is correct |
15 |
Correct |
79 ms |
167840 KB |
Output is correct |
16 |
Correct |
68 ms |
167764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
166716 KB |
Output is correct |
2 |
Correct |
29 ms |
166744 KB |
Output is correct |
3 |
Correct |
30 ms |
166740 KB |
Output is correct |
4 |
Correct |
30 ms |
166740 KB |
Output is correct |
5 |
Correct |
29 ms |
166732 KB |
Output is correct |
6 |
Correct |
32 ms |
166740 KB |
Output is correct |
7 |
Correct |
30 ms |
166748 KB |
Output is correct |
8 |
Correct |
31 ms |
166704 KB |
Output is correct |
9 |
Correct |
32 ms |
166796 KB |
Output is correct |
10 |
Correct |
68 ms |
167364 KB |
Output is correct |
11 |
Correct |
96 ms |
168300 KB |
Output is correct |
12 |
Correct |
114 ms |
168956 KB |
Output is correct |
13 |
Correct |
118 ms |
169192 KB |
Output is correct |
14 |
Correct |
114 ms |
168588 KB |
Output is correct |
15 |
Correct |
79 ms |
167840 KB |
Output is correct |
16 |
Correct |
68 ms |
167764 KB |
Output is correct |
17 |
Correct |
98 ms |
168340 KB |
Output is correct |
18 |
Correct |
31 ms |
166692 KB |
Output is correct |
19 |
Correct |
30 ms |
166740 KB |
Output is correct |
20 |
Correct |
30 ms |
166740 KB |
Output is correct |
21 |
Correct |
30 ms |
166740 KB |
Output is correct |
22 |
Correct |
29 ms |
166740 KB |
Output is correct |
23 |
Correct |
31 ms |
166740 KB |
Output is correct |
24 |
Correct |
31 ms |
166748 KB |
Output is correct |
25 |
Correct |
34 ms |
166740 KB |
Output is correct |
26 |
Correct |
32 ms |
166808 KB |
Output is correct |
27 |
Correct |
67 ms |
167508 KB |
Output is correct |
28 |
Correct |
126 ms |
168992 KB |
Output is correct |
29 |
Correct |
123 ms |
169428 KB |
Output is correct |
30 |
Correct |
92 ms |
168532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
166976 KB |
Output is correct |
2 |
Correct |
30 ms |
166736 KB |
Output is correct |
3 |
Correct |
28 ms |
166744 KB |
Output is correct |
4 |
Correct |
28 ms |
166524 KB |
Output is correct |
5 |
Correct |
30 ms |
166584 KB |
Output is correct |
6 |
Correct |
31 ms |
166740 KB |
Output is correct |
7 |
Correct |
33 ms |
166744 KB |
Output is correct |
8 |
Correct |
78 ms |
166776 KB |
Output is correct |
9 |
Correct |
77 ms |
166780 KB |
Output is correct |
10 |
Correct |
63 ms |
166588 KB |
Output is correct |
11 |
Correct |
115 ms |
166736 KB |
Output is correct |
12 |
Correct |
39 ms |
166740 KB |
Output is correct |
13 |
Correct |
31 ms |
166740 KB |
Output is correct |
14 |
Correct |
31 ms |
166780 KB |
Output is correct |
15 |
Correct |
31 ms |
166572 KB |
Output is correct |
16 |
Correct |
31 ms |
166684 KB |
Output is correct |
17 |
Correct |
34 ms |
166860 KB |
Output is correct |
18 |
Correct |
37 ms |
166880 KB |
Output is correct |
19 |
Correct |
71 ms |
167508 KB |
Output is correct |
20 |
Correct |
128 ms |
169048 KB |
Output is correct |
21 |
Correct |
100 ms |
168532 KB |
Output is correct |
22 |
Correct |
104 ms |
168060 KB |
Output is correct |
23 |
Correct |
89 ms |
169076 KB |
Output is correct |
24 |
Correct |
81 ms |
168044 KB |
Output is correct |
25 |
Correct |
78 ms |
167788 KB |
Output is correct |
26 |
Correct |
68 ms |
167980 KB |
Output is correct |
27 |
Correct |
29 ms |
166716 KB |
Output is correct |
28 |
Correct |
29 ms |
166744 KB |
Output is correct |
29 |
Correct |
30 ms |
166740 KB |
Output is correct |
30 |
Correct |
30 ms |
166740 KB |
Output is correct |
31 |
Correct |
29 ms |
166732 KB |
Output is correct |
32 |
Correct |
32 ms |
166740 KB |
Output is correct |
33 |
Correct |
30 ms |
166748 KB |
Output is correct |
34 |
Correct |
31 ms |
166704 KB |
Output is correct |
35 |
Correct |
32 ms |
166796 KB |
Output is correct |
36 |
Correct |
68 ms |
167364 KB |
Output is correct |
37 |
Correct |
96 ms |
168300 KB |
Output is correct |
38 |
Correct |
114 ms |
168956 KB |
Output is correct |
39 |
Correct |
118 ms |
169192 KB |
Output is correct |
40 |
Correct |
114 ms |
168588 KB |
Output is correct |
41 |
Correct |
79 ms |
167840 KB |
Output is correct |
42 |
Correct |
68 ms |
167764 KB |
Output is correct |
43 |
Correct |
98 ms |
168340 KB |
Output is correct |
44 |
Correct |
31 ms |
166692 KB |
Output is correct |
45 |
Correct |
30 ms |
166740 KB |
Output is correct |
46 |
Correct |
30 ms |
166740 KB |
Output is correct |
47 |
Correct |
30 ms |
166740 KB |
Output is correct |
48 |
Correct |
29 ms |
166740 KB |
Output is correct |
49 |
Correct |
31 ms |
166740 KB |
Output is correct |
50 |
Correct |
31 ms |
166748 KB |
Output is correct |
51 |
Correct |
34 ms |
166740 KB |
Output is correct |
52 |
Correct |
32 ms |
166808 KB |
Output is correct |
53 |
Correct |
67 ms |
167508 KB |
Output is correct |
54 |
Correct |
126 ms |
168992 KB |
Output is correct |
55 |
Correct |
123 ms |
169428 KB |
Output is correct |
56 |
Correct |
92 ms |
168532 KB |
Output is correct |
57 |
Correct |
114 ms |
170068 KB |
Output is correct |
58 |
Correct |
72 ms |
167508 KB |
Output is correct |
59 |
Correct |
129 ms |
168532 KB |
Output is correct |