#include <bits/stdc++.h>
using namespace std;
int k,n,m,o;
long long d[50001][16][6][6];
void read()
{
cin>>k>>n>>m>>o;
for(int b=0;b<=(n-1)/k;b++)
for(int i1=0;i1<k;i1++)
for(int i2=0;i2<k;i2++)
d[b][0][i1][i2]=1e9;
for(int i=1;i<=m;i++)
{
int a,b;
long long t;
cin>>a>>b>>t;
int b1=a/k;
int i1=a%k;
int i2=b%k;
d[b1][0][i1][i2]=min(d[b1][0][i1][i2],t);
}
}
void prec()
{
for(int lg=1;lg<16;lg++)
for(int i1=0;i1<k;i1++)
for(int i2=0;i2<k;i2++)
for(int b=0;b<=(n-1)/k;b++)
{
d[b][lg][i1][i2]=1e9;
for(int i3=0;i3<k;i3++)
d[b][lg][i1][i2]=min(d[b][lg-1][i1][i3]
+d[min(49999,b+(1<<(lg-1)))][lg-1][i3][i2],
d[b][lg][i1][i2]);
}
}
long long ans[8],ans2[8];
void compute(int a,int b)
{
for(int i=0;i<k;i++)
ans[i]=1e9;
ans[a%k]=0;
int jump=b/k-a/k;
int block=a/k;
for(int lg=0;lg<16;lg++)
{
if((1<<lg)&jump)
{
for(int i=0;i<k;i++)
ans2[i]=ans[i];
for(int i2=0;i2<k;i2++)
{
ans[i2]=1e9;
for(int i1=0;i1<k;i1++)
ans[i2]=min(ans[i2],ans2[i1]+d[block][lg][i1][i2]);
}
block+=(1<<lg);
}
}
int i=b%k;
if(ans[i]>=1e9)cout<<-1<<endl;
else cout<<ans[i]<<endl;
}
void solve()
{
for(int i=1;i<=o;i++)
{
int a,b;
cin>>a>>b;
compute(a,b);
}
}
int main() {
read();
prec();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
225928 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
4 ms |
6736 KB |
Output is correct |
6 |
Correct |
4 ms |
6736 KB |
Output is correct |
7 |
Correct |
4 ms |
6736 KB |
Output is correct |
8 |
Correct |
76 ms |
225864 KB |
Output is correct |
9 |
Correct |
78 ms |
225868 KB |
Output is correct |
10 |
Correct |
51 ms |
225868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
113456 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
20 ms |
6864 KB |
Output is correct |
8 |
Correct |
24 ms |
4688 KB |
Output is correct |
9 |
Correct |
81 ms |
225864 KB |
Output is correct |
10 |
Correct |
131 ms |
76524 KB |
Output is correct |
11 |
Correct |
97 ms |
113716 KB |
Output is correct |
12 |
Correct |
90 ms |
76616 KB |
Output is correct |
13 |
Correct |
109 ms |
29324 KB |
Output is correct |
14 |
Correct |
65 ms |
45908 KB |
Output is correct |
15 |
Correct |
73 ms |
29340 KB |
Output is correct |
16 |
Correct |
77 ms |
29344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
3 ms |
2640 KB |
Output is correct |
9 |
Correct |
3 ms |
2808 KB |
Output is correct |
10 |
Correct |
56 ms |
225868 KB |
Output is correct |
11 |
Correct |
81 ms |
113452 KB |
Output is correct |
12 |
Correct |
91 ms |
76616 KB |
Output is correct |
13 |
Correct |
102 ms |
76616 KB |
Output is correct |
14 |
Correct |
86 ms |
76520 KB |
Output is correct |
15 |
Correct |
62 ms |
29264 KB |
Output is correct |
16 |
Correct |
66 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
3 ms |
2640 KB |
Output is correct |
9 |
Correct |
3 ms |
2808 KB |
Output is correct |
10 |
Correct |
56 ms |
225868 KB |
Output is correct |
11 |
Correct |
81 ms |
113452 KB |
Output is correct |
12 |
Correct |
91 ms |
76616 KB |
Output is correct |
13 |
Correct |
102 ms |
76616 KB |
Output is correct |
14 |
Correct |
86 ms |
76520 KB |
Output is correct |
15 |
Correct |
62 ms |
29264 KB |
Output is correct |
16 |
Correct |
66 ms |
29264 KB |
Output is correct |
17 |
Correct |
86 ms |
113456 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
7 ms |
6736 KB |
Output is correct |
24 |
Correct |
7 ms |
4688 KB |
Output is correct |
25 |
Correct |
8 ms |
2640 KB |
Output is correct |
26 |
Correct |
8 ms |
2644 KB |
Output is correct |
27 |
Correct |
62 ms |
225700 KB |
Output is correct |
28 |
Correct |
112 ms |
76532 KB |
Output is correct |
29 |
Correct |
106 ms |
76536 KB |
Output is correct |
30 |
Correct |
90 ms |
76628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
225928 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
4 ms |
6736 KB |
Output is correct |
6 |
Correct |
4 ms |
6736 KB |
Output is correct |
7 |
Correct |
4 ms |
6736 KB |
Output is correct |
8 |
Correct |
76 ms |
225864 KB |
Output is correct |
9 |
Correct |
78 ms |
225868 KB |
Output is correct |
10 |
Correct |
51 ms |
225868 KB |
Output is correct |
11 |
Correct |
89 ms |
113456 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
20 ms |
6864 KB |
Output is correct |
18 |
Correct |
24 ms |
4688 KB |
Output is correct |
19 |
Correct |
81 ms |
225864 KB |
Output is correct |
20 |
Correct |
131 ms |
76524 KB |
Output is correct |
21 |
Correct |
97 ms |
113716 KB |
Output is correct |
22 |
Correct |
90 ms |
76616 KB |
Output is correct |
23 |
Correct |
109 ms |
29324 KB |
Output is correct |
24 |
Correct |
65 ms |
45908 KB |
Output is correct |
25 |
Correct |
73 ms |
29340 KB |
Output is correct |
26 |
Correct |
77 ms |
29344 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
2 ms |
6736 KB |
Output is correct |
33 |
Correct |
2 ms |
4688 KB |
Output is correct |
34 |
Correct |
3 ms |
2640 KB |
Output is correct |
35 |
Correct |
3 ms |
2808 KB |
Output is correct |
36 |
Correct |
56 ms |
225868 KB |
Output is correct |
37 |
Correct |
81 ms |
113452 KB |
Output is correct |
38 |
Correct |
91 ms |
76616 KB |
Output is correct |
39 |
Correct |
102 ms |
76616 KB |
Output is correct |
40 |
Correct |
86 ms |
76520 KB |
Output is correct |
41 |
Correct |
62 ms |
29264 KB |
Output is correct |
42 |
Correct |
66 ms |
29264 KB |
Output is correct |
43 |
Correct |
86 ms |
113456 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
512 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
7 ms |
6736 KB |
Output is correct |
50 |
Correct |
7 ms |
4688 KB |
Output is correct |
51 |
Correct |
8 ms |
2640 KB |
Output is correct |
52 |
Correct |
8 ms |
2644 KB |
Output is correct |
53 |
Correct |
62 ms |
225700 KB |
Output is correct |
54 |
Correct |
112 ms |
76532 KB |
Output is correct |
55 |
Correct |
106 ms |
76536 KB |
Output is correct |
56 |
Correct |
90 ms |
76628 KB |
Output is correct |
57 |
Correct |
148 ms |
58064 KB |
Output is correct |
58 |
Correct |
77 ms |
225876 KB |
Output is correct |
59 |
Correct |
99 ms |
113496 KB |
Output is correct |