#include <bits/stdc++.h>
using namespace std;
int k,n,m,q;
int dis[50001][17][5][5]; //(a,b,x,y) : from ak+x to (a+(1<<b))k+y
void merge(int ans[5][5], int a[5][5], int b[5][5]) {
for (int i=0; i<k; ++i) {
for (int j=0; j<k; ++j) {
//find ans[i][j]
for (int z=0; z<k; ++z) {
ans[i][j]=min(ans[i][j],a[i][z]+b[z][j]);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>k>>n>>m>>q;
memset(dis,0x3f,sizeof(dis));
for (int i=0; i<m; ++i) {
int a,b,w;
cin>>a>>b>>w;
dis[a/k][0][a%k][b%k]=w;
}
for (int i=1; i<17; ++i) {
for (int j=0; j+(1<<i)<=n/k; ++j) {
merge(dis[j][i],dis[j][i-1],dis[j+(1<<(i-1))][i-1]);
}
}
while (q--) {
int x,y; cin>>x>>y;
int group=x/k;
//from x/k to y/k
int ans[5][5],temp[5][5];
memset(ans,0x3f,sizeof(ans));
for (int i=0; i<k; ++i) ans[i][i]=0;
for (int i=16; i>=0; --i) {
if (group+(1<<i)<=y/k) {
memset(temp,0x3f,sizeof(temp));
merge(temp,ans,dis[group][i]);
swap(ans,temp);
group+=(1<<i);
}
}
if (ans[x%k][y%k]==0x3f3f3f3f) cout<<-1<<"\n";
else cout<<ans[x%k][y%k]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
83476 KB |
Output is correct |
2 |
Correct |
32 ms |
83416 KB |
Output is correct |
3 |
Correct |
32 ms |
83404 KB |
Output is correct |
4 |
Correct |
32 ms |
83404 KB |
Output is correct |
5 |
Correct |
34 ms |
83412 KB |
Output is correct |
6 |
Correct |
39 ms |
83404 KB |
Output is correct |
7 |
Correct |
41 ms |
83408 KB |
Output is correct |
8 |
Correct |
82 ms |
84420 KB |
Output is correct |
9 |
Correct |
78 ms |
84372 KB |
Output is correct |
10 |
Correct |
60 ms |
83528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
83488 KB |
Output is correct |
2 |
Correct |
35 ms |
83432 KB |
Output is correct |
3 |
Correct |
34 ms |
83404 KB |
Output is correct |
4 |
Correct |
33 ms |
83400 KB |
Output is correct |
5 |
Correct |
34 ms |
83432 KB |
Output is correct |
6 |
Correct |
34 ms |
83408 KB |
Output is correct |
7 |
Correct |
39 ms |
83532 KB |
Output is correct |
8 |
Correct |
44 ms |
83540 KB |
Output is correct |
9 |
Correct |
86 ms |
84348 KB |
Output is correct |
10 |
Correct |
122 ms |
85808 KB |
Output is correct |
11 |
Correct |
96 ms |
85256 KB |
Output is correct |
12 |
Correct |
89 ms |
84680 KB |
Output is correct |
13 |
Correct |
90 ms |
85764 KB |
Output is correct |
14 |
Correct |
74 ms |
84868 KB |
Output is correct |
15 |
Correct |
69 ms |
84600 KB |
Output is correct |
16 |
Correct |
75 ms |
84672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
83404 KB |
Output is correct |
2 |
Correct |
34 ms |
83444 KB |
Output is correct |
3 |
Correct |
36 ms |
83404 KB |
Output is correct |
4 |
Correct |
34 ms |
83400 KB |
Output is correct |
5 |
Correct |
34 ms |
83452 KB |
Output is correct |
6 |
Correct |
37 ms |
83460 KB |
Output is correct |
7 |
Correct |
35 ms |
83492 KB |
Output is correct |
8 |
Correct |
34 ms |
83476 KB |
Output is correct |
9 |
Correct |
34 ms |
83504 KB |
Output is correct |
10 |
Correct |
65 ms |
84244 KB |
Output is correct |
11 |
Correct |
82 ms |
85000 KB |
Output is correct |
12 |
Correct |
94 ms |
85632 KB |
Output is correct |
13 |
Correct |
98 ms |
85868 KB |
Output is correct |
14 |
Correct |
92 ms |
85248 KB |
Output is correct |
15 |
Correct |
73 ms |
84596 KB |
Output is correct |
16 |
Correct |
65 ms |
84556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
83404 KB |
Output is correct |
2 |
Correct |
34 ms |
83444 KB |
Output is correct |
3 |
Correct |
36 ms |
83404 KB |
Output is correct |
4 |
Correct |
34 ms |
83400 KB |
Output is correct |
5 |
Correct |
34 ms |
83452 KB |
Output is correct |
6 |
Correct |
37 ms |
83460 KB |
Output is correct |
7 |
Correct |
35 ms |
83492 KB |
Output is correct |
8 |
Correct |
34 ms |
83476 KB |
Output is correct |
9 |
Correct |
34 ms |
83504 KB |
Output is correct |
10 |
Correct |
65 ms |
84244 KB |
Output is correct |
11 |
Correct |
82 ms |
85000 KB |
Output is correct |
12 |
Correct |
94 ms |
85632 KB |
Output is correct |
13 |
Correct |
98 ms |
85868 KB |
Output is correct |
14 |
Correct |
92 ms |
85248 KB |
Output is correct |
15 |
Correct |
73 ms |
84596 KB |
Output is correct |
16 |
Correct |
65 ms |
84556 KB |
Output is correct |
17 |
Correct |
90 ms |
85036 KB |
Output is correct |
18 |
Correct |
35 ms |
83372 KB |
Output is correct |
19 |
Correct |
33 ms |
83608 KB |
Output is correct |
20 |
Correct |
33 ms |
83448 KB |
Output is correct |
21 |
Correct |
33 ms |
83404 KB |
Output is correct |
22 |
Correct |
34 ms |
83408 KB |
Output is correct |
23 |
Correct |
44 ms |
83436 KB |
Output is correct |
24 |
Correct |
45 ms |
83532 KB |
Output is correct |
25 |
Correct |
42 ms |
83532 KB |
Output is correct |
26 |
Correct |
39 ms |
83496 KB |
Output is correct |
27 |
Correct |
67 ms |
84228 KB |
Output is correct |
28 |
Correct |
100 ms |
85708 KB |
Output is correct |
29 |
Correct |
112 ms |
85836 KB |
Output is correct |
30 |
Correct |
97 ms |
85244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
83476 KB |
Output is correct |
2 |
Correct |
32 ms |
83416 KB |
Output is correct |
3 |
Correct |
32 ms |
83404 KB |
Output is correct |
4 |
Correct |
32 ms |
83404 KB |
Output is correct |
5 |
Correct |
34 ms |
83412 KB |
Output is correct |
6 |
Correct |
39 ms |
83404 KB |
Output is correct |
7 |
Correct |
41 ms |
83408 KB |
Output is correct |
8 |
Correct |
82 ms |
84420 KB |
Output is correct |
9 |
Correct |
78 ms |
84372 KB |
Output is correct |
10 |
Correct |
60 ms |
83528 KB |
Output is correct |
11 |
Correct |
92 ms |
83488 KB |
Output is correct |
12 |
Correct |
35 ms |
83432 KB |
Output is correct |
13 |
Correct |
34 ms |
83404 KB |
Output is correct |
14 |
Correct |
33 ms |
83400 KB |
Output is correct |
15 |
Correct |
34 ms |
83432 KB |
Output is correct |
16 |
Correct |
34 ms |
83408 KB |
Output is correct |
17 |
Correct |
39 ms |
83532 KB |
Output is correct |
18 |
Correct |
44 ms |
83540 KB |
Output is correct |
19 |
Correct |
86 ms |
84348 KB |
Output is correct |
20 |
Correct |
122 ms |
85808 KB |
Output is correct |
21 |
Correct |
96 ms |
85256 KB |
Output is correct |
22 |
Correct |
89 ms |
84680 KB |
Output is correct |
23 |
Correct |
90 ms |
85764 KB |
Output is correct |
24 |
Correct |
74 ms |
84868 KB |
Output is correct |
25 |
Correct |
69 ms |
84600 KB |
Output is correct |
26 |
Correct |
75 ms |
84672 KB |
Output is correct |
27 |
Correct |
33 ms |
83404 KB |
Output is correct |
28 |
Correct |
34 ms |
83444 KB |
Output is correct |
29 |
Correct |
36 ms |
83404 KB |
Output is correct |
30 |
Correct |
34 ms |
83400 KB |
Output is correct |
31 |
Correct |
34 ms |
83452 KB |
Output is correct |
32 |
Correct |
37 ms |
83460 KB |
Output is correct |
33 |
Correct |
35 ms |
83492 KB |
Output is correct |
34 |
Correct |
34 ms |
83476 KB |
Output is correct |
35 |
Correct |
34 ms |
83504 KB |
Output is correct |
36 |
Correct |
65 ms |
84244 KB |
Output is correct |
37 |
Correct |
82 ms |
85000 KB |
Output is correct |
38 |
Correct |
94 ms |
85632 KB |
Output is correct |
39 |
Correct |
98 ms |
85868 KB |
Output is correct |
40 |
Correct |
92 ms |
85248 KB |
Output is correct |
41 |
Correct |
73 ms |
84596 KB |
Output is correct |
42 |
Correct |
65 ms |
84556 KB |
Output is correct |
43 |
Correct |
90 ms |
85036 KB |
Output is correct |
44 |
Correct |
35 ms |
83372 KB |
Output is correct |
45 |
Correct |
33 ms |
83608 KB |
Output is correct |
46 |
Correct |
33 ms |
83448 KB |
Output is correct |
47 |
Correct |
33 ms |
83404 KB |
Output is correct |
48 |
Correct |
34 ms |
83408 KB |
Output is correct |
49 |
Correct |
44 ms |
83436 KB |
Output is correct |
50 |
Correct |
45 ms |
83532 KB |
Output is correct |
51 |
Correct |
42 ms |
83532 KB |
Output is correct |
52 |
Correct |
39 ms |
83496 KB |
Output is correct |
53 |
Correct |
67 ms |
84228 KB |
Output is correct |
54 |
Correct |
100 ms |
85708 KB |
Output is correct |
55 |
Correct |
112 ms |
85836 KB |
Output is correct |
56 |
Correct |
97 ms |
85244 KB |
Output is correct |
57 |
Correct |
138 ms |
86852 KB |
Output is correct |
58 |
Correct |
78 ms |
84308 KB |
Output is correct |
59 |
Correct |
106 ms |
85252 KB |
Output is correct |