#include <bits/stdc++.h>
using namespace std;
#define task ""
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const int INF = 1e9 + 7;
const int MAX = 50000;
int k, n, m, q;
int dp[MAX][16][5];
#define ID(i, j, t) (((i) / k + (1 << (j))) * k + (t))
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> k >> n >> m >> q;
for (int j = 0; j <= 15; ++j) {
for (int i = 0; i < n; ++i)
for (int t = 0; t < 5; ++t)
dp[i][j][t] = INF;
}
while (m--) {
int a, b, t; cin >> a >> b >> t;
dp[a][0][b % k] = t;
}
for (int j = 1; j <= 15; ++j) {
for (int i = 0; i < n; ++i) {
for (int m = 0; m < k; ++m) {
for (int t = 0; t < k; ++t) {
if (ID(i, j, t) >= n) continue;
if (dp[i][j - 1][m] >= INF) continue;
if (dp[ID(i, j - 1, m)][j - 1][t] >= INF) continue;
minimize(dp[i][j][t], dp[i][j - 1][m] + dp[ID(i, j - 1, m)][j - 1][t]);
}
}
}
}
while (q--) {
int a, b; cin >> a >> b;
vector<int> curDist(5, INF), nextDist(5, INF);
curDist[a % k] = 0;
int u = a / k, v = b / k;
for (int j = 15; j >= 0; --j) {
if (u + (1 << j) <= v) {
for (int m = 0; m < k; ++m) {
for (int t = 0; t < k; ++t) {
minimize(nextDist[t], curDist[m] + dp[u * k + m][j][t]);
}
}
curDist = nextDist; u += 1 << j;
nextDist.assign(k, INF);
}
}
int res = curDist[b % k];
if (res == INF) res = -1;
cout << res << '\n';
}
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:27:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:28:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
16848 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
32 ms |
16876 KB |
Output is correct |
9 |
Correct |
33 ms |
16852 KB |
Output is correct |
10 |
Correct |
22 ms |
16084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
17612 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
724 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
30 ms |
16848 KB |
Output is correct |
10 |
Correct |
57 ms |
18252 KB |
Output is correct |
11 |
Correct |
45 ms |
17680 KB |
Output is correct |
12 |
Correct |
49 ms |
17236 KB |
Output is correct |
13 |
Correct |
55 ms |
12096 KB |
Output is correct |
14 |
Correct |
31 ms |
11168 KB |
Output is correct |
15 |
Correct |
47 ms |
10860 KB |
Output is correct |
16 |
Correct |
49 ms |
10932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
27 ms |
16716 KB |
Output is correct |
11 |
Correct |
40 ms |
17488 KB |
Output is correct |
12 |
Correct |
52 ms |
18124 KB |
Output is correct |
13 |
Correct |
58 ms |
18248 KB |
Output is correct |
14 |
Correct |
57 ms |
17736 KB |
Output is correct |
15 |
Correct |
47 ms |
10848 KB |
Output is correct |
16 |
Correct |
46 ms |
10852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
27 ms |
16716 KB |
Output is correct |
11 |
Correct |
40 ms |
17488 KB |
Output is correct |
12 |
Correct |
52 ms |
18124 KB |
Output is correct |
13 |
Correct |
58 ms |
18248 KB |
Output is correct |
14 |
Correct |
57 ms |
17736 KB |
Output is correct |
15 |
Correct |
47 ms |
10848 KB |
Output is correct |
16 |
Correct |
46 ms |
10852 KB |
Output is correct |
17 |
Correct |
42 ms |
17484 KB |
Output is correct |
18 |
Correct |
0 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
2 ms |
600 KB |
Output is correct |
24 |
Correct |
2 ms |
688 KB |
Output is correct |
25 |
Correct |
3 ms |
720 KB |
Output is correct |
26 |
Correct |
3 ms |
704 KB |
Output is correct |
27 |
Correct |
29 ms |
16724 KB |
Output is correct |
28 |
Correct |
59 ms |
18116 KB |
Output is correct |
29 |
Correct |
56 ms |
18400 KB |
Output is correct |
30 |
Correct |
51 ms |
17740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
16848 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
32 ms |
16876 KB |
Output is correct |
9 |
Correct |
33 ms |
16852 KB |
Output is correct |
10 |
Correct |
22 ms |
16084 KB |
Output is correct |
11 |
Correct |
42 ms |
17612 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
436 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
5 ms |
724 KB |
Output is correct |
19 |
Correct |
30 ms |
16848 KB |
Output is correct |
20 |
Correct |
57 ms |
18252 KB |
Output is correct |
21 |
Correct |
45 ms |
17680 KB |
Output is correct |
22 |
Correct |
49 ms |
17236 KB |
Output is correct |
23 |
Correct |
55 ms |
12096 KB |
Output is correct |
24 |
Correct |
31 ms |
11168 KB |
Output is correct |
25 |
Correct |
47 ms |
10860 KB |
Output is correct |
26 |
Correct |
49 ms |
10932 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
0 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
27 ms |
16716 KB |
Output is correct |
37 |
Correct |
40 ms |
17488 KB |
Output is correct |
38 |
Correct |
52 ms |
18124 KB |
Output is correct |
39 |
Correct |
58 ms |
18248 KB |
Output is correct |
40 |
Correct |
57 ms |
17736 KB |
Output is correct |
41 |
Correct |
47 ms |
10848 KB |
Output is correct |
42 |
Correct |
46 ms |
10852 KB |
Output is correct |
43 |
Correct |
42 ms |
17484 KB |
Output is correct |
44 |
Correct |
0 ms |
328 KB |
Output is correct |
45 |
Correct |
1 ms |
328 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
328 KB |
Output is correct |
49 |
Correct |
2 ms |
600 KB |
Output is correct |
50 |
Correct |
2 ms |
688 KB |
Output is correct |
51 |
Correct |
3 ms |
720 KB |
Output is correct |
52 |
Correct |
3 ms |
704 KB |
Output is correct |
53 |
Correct |
29 ms |
16724 KB |
Output is correct |
54 |
Correct |
59 ms |
18116 KB |
Output is correct |
55 |
Correct |
56 ms |
18400 KB |
Output is correct |
56 |
Correct |
51 ms |
17740 KB |
Output is correct |
57 |
Correct |
98 ms |
19264 KB |
Output is correct |
58 |
Correct |
38 ms |
16836 KB |
Output is correct |
59 |
Correct |
49 ms |
17728 KB |
Output is correct |