#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int dp[100001][5][16][5];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int k,n,m,o,x,y,z,a,b;
cin>>k>>n>>m>>o;
for (int g=0;g<100001;++g)
for (int h=0;h<5;++h)
for (int i=0;i<16;++i)
for (int j=0;j<5;++j)
dp[g][h][i][j]=INF;
while (m--)
{
cin>>x>>y>>z;
dp[x/k][x%k][0][y%k]=z;
}
for (int i=1;i<16;++i)
for (int g=0;g<=n/k;++g)
for (int h=0;h<k;++h)
for (int j=0;j<k;++j)
for (int f=0;f<k;++f)
dp[g][h][i][j]=min(dp[g][h][i][j],dp[g][h][i-1][f]+dp[g+(1<<(i-1))][f][i-1][j]);
while (o--)
{
cin>>x>>y;
a=x/k;
b=y/k;
vector<int> v(k,INF);
v[x%k]=0;
for (int h=15;h>=0;--h)
if (a+(1<<h)<=b)
{
vector<int> w(k,INF);
for (int i=0;i<k;++i)
for (int j=0;j<k;++j)
w[j]=min(w[j],v[i]+dp[a][i][h][j]);
swap(v,w);
a+=(1<<h);
}
if (v[y%k]==INF)
v[y%k]=-1;
cout<<v[y%k]<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
157860 KB |
Output is correct |
2 |
Correct |
88 ms |
156760 KB |
Output is correct |
3 |
Correct |
106 ms |
156844 KB |
Output is correct |
4 |
Correct |
88 ms |
156740 KB |
Output is correct |
5 |
Correct |
90 ms |
156788 KB |
Output is correct |
6 |
Correct |
90 ms |
156868 KB |
Output is correct |
7 |
Correct |
90 ms |
156864 KB |
Output is correct |
8 |
Correct |
164 ms |
157788 KB |
Output is correct |
9 |
Correct |
132 ms |
157676 KB |
Output is correct |
10 |
Correct |
112 ms |
156904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
158040 KB |
Output is correct |
2 |
Correct |
88 ms |
156740 KB |
Output is correct |
3 |
Correct |
122 ms |
156740 KB |
Output is correct |
4 |
Correct |
87 ms |
156752 KB |
Output is correct |
5 |
Correct |
89 ms |
156740 KB |
Output is correct |
6 |
Correct |
89 ms |
156832 KB |
Output is correct |
7 |
Correct |
95 ms |
156856 KB |
Output is correct |
8 |
Correct |
95 ms |
156996 KB |
Output is correct |
9 |
Correct |
134 ms |
157664 KB |
Output is correct |
10 |
Correct |
187 ms |
159088 KB |
Output is correct |
11 |
Correct |
172 ms |
158528 KB |
Output is correct |
12 |
Correct |
161 ms |
158132 KB |
Output is correct |
13 |
Correct |
175 ms |
159172 KB |
Output is correct |
14 |
Correct |
151 ms |
158232 KB |
Output is correct |
15 |
Correct |
147 ms |
158060 KB |
Output is correct |
16 |
Correct |
144 ms |
158120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
156752 KB |
Output is correct |
2 |
Correct |
88 ms |
156776 KB |
Output is correct |
3 |
Correct |
107 ms |
156828 KB |
Output is correct |
4 |
Correct |
88 ms |
156776 KB |
Output is correct |
5 |
Correct |
87 ms |
156848 KB |
Output is correct |
6 |
Correct |
95 ms |
156864 KB |
Output is correct |
7 |
Correct |
90 ms |
156848 KB |
Output is correct |
8 |
Correct |
104 ms |
156808 KB |
Output is correct |
9 |
Correct |
90 ms |
156884 KB |
Output is correct |
10 |
Correct |
137 ms |
157636 KB |
Output is correct |
11 |
Correct |
155 ms |
158344 KB |
Output is correct |
12 |
Correct |
183 ms |
159024 KB |
Output is correct |
13 |
Correct |
188 ms |
159272 KB |
Output is correct |
14 |
Correct |
168 ms |
158592 KB |
Output is correct |
15 |
Correct |
138 ms |
158024 KB |
Output is correct |
16 |
Correct |
153 ms |
158048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
156752 KB |
Output is correct |
2 |
Correct |
88 ms |
156776 KB |
Output is correct |
3 |
Correct |
107 ms |
156828 KB |
Output is correct |
4 |
Correct |
88 ms |
156776 KB |
Output is correct |
5 |
Correct |
87 ms |
156848 KB |
Output is correct |
6 |
Correct |
95 ms |
156864 KB |
Output is correct |
7 |
Correct |
90 ms |
156848 KB |
Output is correct |
8 |
Correct |
104 ms |
156808 KB |
Output is correct |
9 |
Correct |
90 ms |
156884 KB |
Output is correct |
10 |
Correct |
137 ms |
157636 KB |
Output is correct |
11 |
Correct |
155 ms |
158344 KB |
Output is correct |
12 |
Correct |
183 ms |
159024 KB |
Output is correct |
13 |
Correct |
188 ms |
159272 KB |
Output is correct |
14 |
Correct |
168 ms |
158592 KB |
Output is correct |
15 |
Correct |
138 ms |
158024 KB |
Output is correct |
16 |
Correct |
153 ms |
158048 KB |
Output is correct |
17 |
Correct |
164 ms |
158404 KB |
Output is correct |
18 |
Correct |
88 ms |
156856 KB |
Output is correct |
19 |
Correct |
89 ms |
156800 KB |
Output is correct |
20 |
Correct |
92 ms |
156852 KB |
Output is correct |
21 |
Correct |
89 ms |
156828 KB |
Output is correct |
22 |
Correct |
105 ms |
156764 KB |
Output is correct |
23 |
Correct |
93 ms |
156812 KB |
Output is correct |
24 |
Correct |
92 ms |
156836 KB |
Output is correct |
25 |
Correct |
90 ms |
156952 KB |
Output is correct |
26 |
Correct |
92 ms |
156936 KB |
Output is correct |
27 |
Correct |
125 ms |
157672 KB |
Output is correct |
28 |
Correct |
185 ms |
159072 KB |
Output is correct |
29 |
Correct |
210 ms |
159308 KB |
Output is correct |
30 |
Correct |
215 ms |
158624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
157860 KB |
Output is correct |
2 |
Correct |
88 ms |
156760 KB |
Output is correct |
3 |
Correct |
106 ms |
156844 KB |
Output is correct |
4 |
Correct |
88 ms |
156740 KB |
Output is correct |
5 |
Correct |
90 ms |
156788 KB |
Output is correct |
6 |
Correct |
90 ms |
156868 KB |
Output is correct |
7 |
Correct |
90 ms |
156864 KB |
Output is correct |
8 |
Correct |
164 ms |
157788 KB |
Output is correct |
9 |
Correct |
132 ms |
157676 KB |
Output is correct |
10 |
Correct |
112 ms |
156904 KB |
Output is correct |
11 |
Correct |
168 ms |
158040 KB |
Output is correct |
12 |
Correct |
88 ms |
156740 KB |
Output is correct |
13 |
Correct |
122 ms |
156740 KB |
Output is correct |
14 |
Correct |
87 ms |
156752 KB |
Output is correct |
15 |
Correct |
89 ms |
156740 KB |
Output is correct |
16 |
Correct |
89 ms |
156832 KB |
Output is correct |
17 |
Correct |
95 ms |
156856 KB |
Output is correct |
18 |
Correct |
95 ms |
156996 KB |
Output is correct |
19 |
Correct |
134 ms |
157664 KB |
Output is correct |
20 |
Correct |
187 ms |
159088 KB |
Output is correct |
21 |
Correct |
172 ms |
158528 KB |
Output is correct |
22 |
Correct |
161 ms |
158132 KB |
Output is correct |
23 |
Correct |
175 ms |
159172 KB |
Output is correct |
24 |
Correct |
151 ms |
158232 KB |
Output is correct |
25 |
Correct |
147 ms |
158060 KB |
Output is correct |
26 |
Correct |
144 ms |
158120 KB |
Output is correct |
27 |
Correct |
89 ms |
156752 KB |
Output is correct |
28 |
Correct |
88 ms |
156776 KB |
Output is correct |
29 |
Correct |
107 ms |
156828 KB |
Output is correct |
30 |
Correct |
88 ms |
156776 KB |
Output is correct |
31 |
Correct |
87 ms |
156848 KB |
Output is correct |
32 |
Correct |
95 ms |
156864 KB |
Output is correct |
33 |
Correct |
90 ms |
156848 KB |
Output is correct |
34 |
Correct |
104 ms |
156808 KB |
Output is correct |
35 |
Correct |
90 ms |
156884 KB |
Output is correct |
36 |
Correct |
137 ms |
157636 KB |
Output is correct |
37 |
Correct |
155 ms |
158344 KB |
Output is correct |
38 |
Correct |
183 ms |
159024 KB |
Output is correct |
39 |
Correct |
188 ms |
159272 KB |
Output is correct |
40 |
Correct |
168 ms |
158592 KB |
Output is correct |
41 |
Correct |
138 ms |
158024 KB |
Output is correct |
42 |
Correct |
153 ms |
158048 KB |
Output is correct |
43 |
Correct |
164 ms |
158404 KB |
Output is correct |
44 |
Correct |
88 ms |
156856 KB |
Output is correct |
45 |
Correct |
89 ms |
156800 KB |
Output is correct |
46 |
Correct |
92 ms |
156852 KB |
Output is correct |
47 |
Correct |
89 ms |
156828 KB |
Output is correct |
48 |
Correct |
105 ms |
156764 KB |
Output is correct |
49 |
Correct |
93 ms |
156812 KB |
Output is correct |
50 |
Correct |
92 ms |
156836 KB |
Output is correct |
51 |
Correct |
90 ms |
156952 KB |
Output is correct |
52 |
Correct |
92 ms |
156936 KB |
Output is correct |
53 |
Correct |
125 ms |
157672 KB |
Output is correct |
54 |
Correct |
185 ms |
159072 KB |
Output is correct |
55 |
Correct |
210 ms |
159308 KB |
Output is correct |
56 |
Correct |
215 ms |
158624 KB |
Output is correct |
57 |
Correct |
219 ms |
160196 KB |
Output is correct |
58 |
Correct |
140 ms |
157876 KB |
Output is correct |
59 |
Correct |
248 ms |
158608 KB |
Output is correct |