#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
const int INF = 1e9;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int k, n, m, _;
cin >> k >> n >> m >> _;
n--;
n /= k;
vector<vector<vector<vector<int>>>> blft(1, vector<vector<vector<int>>>(n + 1, vector<vector<int>>(k, vector<int>(k, INF))));
while (m--)
{
int a, b, d;
cin >> a >> b >> d;
blft[0][a / k][a % k][b % k] = d;
}
int K = 16;
int pw = 1;
while (K--)
{
vector<vector<vector<int>>> bck(n + 1, vector<vector<int>>(k, vector<int>(k, INF)));
for (int i = 2 * pw; i <= n; i++) for (int s = 0; s < k; s++) for (int f = 0; f < k; f++) for (int M = 0; M < k; M++) bck[i - 2 * pw][s][f] =
fmin(bck[i - 2 * pw][s][f], blft.back()[i - 2 * pw][s][M] + blft.back()[i - pw][M][f]);
blft.push_back(bck), pw *= 2;
}
while (_--)
{
int a, b;
cin >> a >> b;
vector<int> ans(k, INF);
int d = (b / k) - (a / k);
int cstt = a / k;
ans[a % k] = 0;
for (int i = 0; i < blft.size(); i++) if (d & (1 << i))
{
vector<int> nans(k, INF);
for (int fnl = 0; fnl < k; fnl++) for (int stt = 0; stt < k; stt++) nans[fnl] = fmin(nans[fnl], ans[stt] + blft[i][cstt][stt][fnl]);
ans = nans, cstt += (1 << i);
}
if (ans[b % k] == INF) cout << "-1\n";
else cout << ans[b % k] << "\n";
}
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:45:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::vector<std::vector<int> > > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int i = 0; i < blft.size(); i++) if (d & (1 << i))
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
78784 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
3 ms |
1880 KB |
Output is correct |
7 |
Correct |
4 ms |
1884 KB |
Output is correct |
8 |
Correct |
182 ms |
78876 KB |
Output is correct |
9 |
Correct |
184 ms |
79036 KB |
Output is correct |
10 |
Correct |
174 ms |
78016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
68868 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
6 ms |
1884 KB |
Output is correct |
8 |
Correct |
6 ms |
1884 KB |
Output is correct |
9 |
Correct |
176 ms |
78656 KB |
Output is correct |
10 |
Correct |
149 ms |
61388 KB |
Output is correct |
11 |
Correct |
148 ms |
69124 KB |
Output is correct |
12 |
Correct |
136 ms |
60248 KB |
Output is correct |
13 |
Correct |
115 ms |
35776 KB |
Output is correct |
14 |
Correct |
84 ms |
36964 KB |
Output is correct |
15 |
Correct |
103 ms |
34756 KB |
Output is correct |
16 |
Correct |
98 ms |
34500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1884 KB |
Output is correct |
7 |
Correct |
3 ms |
1628 KB |
Output is correct |
8 |
Correct |
3 ms |
1372 KB |
Output is correct |
9 |
Correct |
3 ms |
1784 KB |
Output is correct |
10 |
Correct |
158 ms |
78588 KB |
Output is correct |
11 |
Correct |
125 ms |
68868 KB |
Output is correct |
12 |
Correct |
131 ms |
61308 KB |
Output is correct |
13 |
Correct |
131 ms |
61384 KB |
Output is correct |
14 |
Correct |
131 ms |
60756 KB |
Output is correct |
15 |
Correct |
86 ms |
34496 KB |
Output is correct |
16 |
Correct |
93 ms |
34500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1884 KB |
Output is correct |
7 |
Correct |
3 ms |
1628 KB |
Output is correct |
8 |
Correct |
3 ms |
1372 KB |
Output is correct |
9 |
Correct |
3 ms |
1784 KB |
Output is correct |
10 |
Correct |
158 ms |
78588 KB |
Output is correct |
11 |
Correct |
125 ms |
68868 KB |
Output is correct |
12 |
Correct |
131 ms |
61308 KB |
Output is correct |
13 |
Correct |
131 ms |
61384 KB |
Output is correct |
14 |
Correct |
131 ms |
60756 KB |
Output is correct |
15 |
Correct |
86 ms |
34496 KB |
Output is correct |
16 |
Correct |
93 ms |
34500 KB |
Output is correct |
17 |
Correct |
130 ms |
68864 KB |
Output is correct |
18 |
Correct |
0 ms |
344 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 |
4 ms |
1988 KB |
Output is correct |
24 |
Correct |
6 ms |
1680 KB |
Output is correct |
25 |
Correct |
4 ms |
1628 KB |
Output is correct |
26 |
Correct |
4 ms |
1628 KB |
Output is correct |
27 |
Correct |
161 ms |
78688 KB |
Output is correct |
28 |
Correct |
143 ms |
61384 KB |
Output is correct |
29 |
Correct |
148 ms |
61480 KB |
Output is correct |
30 |
Correct |
134 ms |
60980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
78784 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
3 ms |
1880 KB |
Output is correct |
7 |
Correct |
4 ms |
1884 KB |
Output is correct |
8 |
Correct |
182 ms |
78876 KB |
Output is correct |
9 |
Correct |
184 ms |
79036 KB |
Output is correct |
10 |
Correct |
174 ms |
78016 KB |
Output is correct |
11 |
Correct |
142 ms |
68868 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
6 ms |
1884 KB |
Output is correct |
18 |
Correct |
6 ms |
1884 KB |
Output is correct |
19 |
Correct |
176 ms |
78656 KB |
Output is correct |
20 |
Correct |
149 ms |
61388 KB |
Output is correct |
21 |
Correct |
148 ms |
69124 KB |
Output is correct |
22 |
Correct |
136 ms |
60248 KB |
Output is correct |
23 |
Correct |
115 ms |
35776 KB |
Output is correct |
24 |
Correct |
84 ms |
36964 KB |
Output is correct |
25 |
Correct |
103 ms |
34756 KB |
Output is correct |
26 |
Correct |
98 ms |
34500 KB |
Output is correct |
27 |
Correct |
0 ms |
348 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 |
1884 KB |
Output is correct |
33 |
Correct |
3 ms |
1628 KB |
Output is correct |
34 |
Correct |
3 ms |
1372 KB |
Output is correct |
35 |
Correct |
3 ms |
1784 KB |
Output is correct |
36 |
Correct |
158 ms |
78588 KB |
Output is correct |
37 |
Correct |
125 ms |
68868 KB |
Output is correct |
38 |
Correct |
131 ms |
61308 KB |
Output is correct |
39 |
Correct |
131 ms |
61384 KB |
Output is correct |
40 |
Correct |
131 ms |
60756 KB |
Output is correct |
41 |
Correct |
86 ms |
34496 KB |
Output is correct |
42 |
Correct |
93 ms |
34500 KB |
Output is correct |
43 |
Correct |
130 ms |
68864 KB |
Output is correct |
44 |
Correct |
0 ms |
344 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 |
4 ms |
1988 KB |
Output is correct |
50 |
Correct |
6 ms |
1680 KB |
Output is correct |
51 |
Correct |
4 ms |
1628 KB |
Output is correct |
52 |
Correct |
4 ms |
1628 KB |
Output is correct |
53 |
Correct |
161 ms |
78688 KB |
Output is correct |
54 |
Correct |
143 ms |
61384 KB |
Output is correct |
55 |
Correct |
148 ms |
61480 KB |
Output is correct |
56 |
Correct |
134 ms |
60980 KB |
Output is correct |
57 |
Correct |
179 ms |
61780 KB |
Output is correct |
58 |
Correct |
196 ms |
79036 KB |
Output is correct |
59 |
Correct |
155 ms |
68996 KB |
Output is correct |