#include <bits/stdc++.h>
using namespace std;
#define task "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
int k, n, m, o;
int dp[50000][16][5][5], ans[5][5], tmp[5][5];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> k >> n >> m >> o;
memset(dp, 0x3f, sizeof(dp));
while (m--) {
int a, b, t;
cin >> a >> b >> t;
dp[a / k][0][a % k][b % k] = t;
}
for (int j = 1; j <= 15; j++) for (int i = 0; i + (1 << j) < (n + k - 1) / k; i++) {
for (int x = 0; x < k; x++) for (int y = 0; y < k; y++) for (int z = 0; z < k; z++) {
dp[i][j][x][y] = min(dp[i][j][x][y], dp[i][j - 1][x][z] + dp[i + (1 << j - 1)][j - 1][z][y]);
}
}
while (o--) {
int a, b; cin >> a >> b;
int u = a / k, v = b / k;
memset(ans, 0x3f, sizeof(ans));
for (int i = 0; i < k; i++) ans[i][i] = 0;
for (int j = 15; j >= 0; j--) {
if (u + (1 << j) <= v) {
memset(tmp, 0x3f, sizeof(tmp));
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
for (int z = 0; z < k; z++) {
tmp[x][y] = min(tmp[x][y], ans[x][z] + dp[u][j][z][y]);
}
}
}
memcpy(ans, tmp, sizeof(ans));
u += 1 << j;
}
}
cout << (ans[a % k][b % k] == 0x3f3f3f3f ? -1 : ans[a % k][b % k]) << '\n';
}
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:37:79: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
37 | dp[i][j][x][y] = min(dp[i][j][x][y], dp[i][j - 1][x][z] + dp[i + (1 << j - 1)][j - 1][z][y]);
| ~~^~~
toll.cpp:24:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:25:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
79568 KB |
Output is correct |
2 |
Correct |
31 ms |
78692 KB |
Output is correct |
3 |
Correct |
31 ms |
78480 KB |
Output is correct |
4 |
Correct |
31 ms |
78548 KB |
Output is correct |
5 |
Correct |
32 ms |
78548 KB |
Output is correct |
6 |
Correct |
31 ms |
78588 KB |
Output is correct |
7 |
Correct |
33 ms |
78632 KB |
Output is correct |
8 |
Correct |
69 ms |
79432 KB |
Output is correct |
9 |
Correct |
71 ms |
79524 KB |
Output is correct |
10 |
Correct |
50 ms |
78680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
80240 KB |
Output is correct |
2 |
Correct |
31 ms |
78516 KB |
Output is correct |
3 |
Correct |
31 ms |
78520 KB |
Output is correct |
4 |
Correct |
32 ms |
78568 KB |
Output is correct |
5 |
Correct |
31 ms |
78552 KB |
Output is correct |
6 |
Correct |
30 ms |
78512 KB |
Output is correct |
7 |
Correct |
34 ms |
78676 KB |
Output is correct |
8 |
Correct |
35 ms |
78668 KB |
Output is correct |
9 |
Correct |
62 ms |
79472 KB |
Output is correct |
10 |
Correct |
98 ms |
80872 KB |
Output is correct |
11 |
Correct |
81 ms |
80364 KB |
Output is correct |
12 |
Correct |
77 ms |
79832 KB |
Output is correct |
13 |
Correct |
80 ms |
80992 KB |
Output is correct |
14 |
Correct |
61 ms |
80016 KB |
Output is correct |
15 |
Correct |
63 ms |
79788 KB |
Output is correct |
16 |
Correct |
70 ms |
79760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
78568 KB |
Output is correct |
2 |
Correct |
31 ms |
78516 KB |
Output is correct |
3 |
Correct |
32 ms |
78568 KB |
Output is correct |
4 |
Correct |
31 ms |
78540 KB |
Output is correct |
5 |
Correct |
33 ms |
78564 KB |
Output is correct |
6 |
Correct |
32 ms |
78568 KB |
Output is correct |
7 |
Correct |
33 ms |
78604 KB |
Output is correct |
8 |
Correct |
32 ms |
78636 KB |
Output is correct |
9 |
Correct |
32 ms |
78536 KB |
Output is correct |
10 |
Correct |
59 ms |
79300 KB |
Output is correct |
11 |
Correct |
74 ms |
80140 KB |
Output is correct |
12 |
Correct |
86 ms |
80872 KB |
Output is correct |
13 |
Correct |
91 ms |
80980 KB |
Output is correct |
14 |
Correct |
78 ms |
80332 KB |
Output is correct |
15 |
Correct |
65 ms |
79772 KB |
Output is correct |
16 |
Correct |
59 ms |
79724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
78568 KB |
Output is correct |
2 |
Correct |
31 ms |
78516 KB |
Output is correct |
3 |
Correct |
32 ms |
78568 KB |
Output is correct |
4 |
Correct |
31 ms |
78540 KB |
Output is correct |
5 |
Correct |
33 ms |
78564 KB |
Output is correct |
6 |
Correct |
32 ms |
78568 KB |
Output is correct |
7 |
Correct |
33 ms |
78604 KB |
Output is correct |
8 |
Correct |
32 ms |
78636 KB |
Output is correct |
9 |
Correct |
32 ms |
78536 KB |
Output is correct |
10 |
Correct |
59 ms |
79300 KB |
Output is correct |
11 |
Correct |
74 ms |
80140 KB |
Output is correct |
12 |
Correct |
86 ms |
80872 KB |
Output is correct |
13 |
Correct |
91 ms |
80980 KB |
Output is correct |
14 |
Correct |
78 ms |
80332 KB |
Output is correct |
15 |
Correct |
65 ms |
79772 KB |
Output is correct |
16 |
Correct |
59 ms |
79724 KB |
Output is correct |
17 |
Correct |
89 ms |
80052 KB |
Output is correct |
18 |
Correct |
35 ms |
78512 KB |
Output is correct |
19 |
Correct |
34 ms |
78468 KB |
Output is correct |
20 |
Correct |
36 ms |
78504 KB |
Output is correct |
21 |
Correct |
30 ms |
78508 KB |
Output is correct |
22 |
Correct |
31 ms |
78500 KB |
Output is correct |
23 |
Correct |
32 ms |
78556 KB |
Output is correct |
24 |
Correct |
38 ms |
78548 KB |
Output is correct |
25 |
Correct |
34 ms |
78684 KB |
Output is correct |
26 |
Correct |
33 ms |
78652 KB |
Output is correct |
27 |
Correct |
62 ms |
79352 KB |
Output is correct |
28 |
Correct |
86 ms |
80776 KB |
Output is correct |
29 |
Correct |
90 ms |
81004 KB |
Output is correct |
30 |
Correct |
90 ms |
80368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
79568 KB |
Output is correct |
2 |
Correct |
31 ms |
78692 KB |
Output is correct |
3 |
Correct |
31 ms |
78480 KB |
Output is correct |
4 |
Correct |
31 ms |
78548 KB |
Output is correct |
5 |
Correct |
32 ms |
78548 KB |
Output is correct |
6 |
Correct |
31 ms |
78588 KB |
Output is correct |
7 |
Correct |
33 ms |
78632 KB |
Output is correct |
8 |
Correct |
69 ms |
79432 KB |
Output is correct |
9 |
Correct |
71 ms |
79524 KB |
Output is correct |
10 |
Correct |
50 ms |
78680 KB |
Output is correct |
11 |
Correct |
91 ms |
80240 KB |
Output is correct |
12 |
Correct |
31 ms |
78516 KB |
Output is correct |
13 |
Correct |
31 ms |
78520 KB |
Output is correct |
14 |
Correct |
32 ms |
78568 KB |
Output is correct |
15 |
Correct |
31 ms |
78552 KB |
Output is correct |
16 |
Correct |
30 ms |
78512 KB |
Output is correct |
17 |
Correct |
34 ms |
78676 KB |
Output is correct |
18 |
Correct |
35 ms |
78668 KB |
Output is correct |
19 |
Correct |
62 ms |
79472 KB |
Output is correct |
20 |
Correct |
98 ms |
80872 KB |
Output is correct |
21 |
Correct |
81 ms |
80364 KB |
Output is correct |
22 |
Correct |
77 ms |
79832 KB |
Output is correct |
23 |
Correct |
80 ms |
80992 KB |
Output is correct |
24 |
Correct |
61 ms |
80016 KB |
Output is correct |
25 |
Correct |
63 ms |
79788 KB |
Output is correct |
26 |
Correct |
70 ms |
79760 KB |
Output is correct |
27 |
Correct |
31 ms |
78568 KB |
Output is correct |
28 |
Correct |
31 ms |
78516 KB |
Output is correct |
29 |
Correct |
32 ms |
78568 KB |
Output is correct |
30 |
Correct |
31 ms |
78540 KB |
Output is correct |
31 |
Correct |
33 ms |
78564 KB |
Output is correct |
32 |
Correct |
32 ms |
78568 KB |
Output is correct |
33 |
Correct |
33 ms |
78604 KB |
Output is correct |
34 |
Correct |
32 ms |
78636 KB |
Output is correct |
35 |
Correct |
32 ms |
78536 KB |
Output is correct |
36 |
Correct |
59 ms |
79300 KB |
Output is correct |
37 |
Correct |
74 ms |
80140 KB |
Output is correct |
38 |
Correct |
86 ms |
80872 KB |
Output is correct |
39 |
Correct |
91 ms |
80980 KB |
Output is correct |
40 |
Correct |
78 ms |
80332 KB |
Output is correct |
41 |
Correct |
65 ms |
79772 KB |
Output is correct |
42 |
Correct |
59 ms |
79724 KB |
Output is correct |
43 |
Correct |
89 ms |
80052 KB |
Output is correct |
44 |
Correct |
35 ms |
78512 KB |
Output is correct |
45 |
Correct |
34 ms |
78468 KB |
Output is correct |
46 |
Correct |
36 ms |
78504 KB |
Output is correct |
47 |
Correct |
30 ms |
78508 KB |
Output is correct |
48 |
Correct |
31 ms |
78500 KB |
Output is correct |
49 |
Correct |
32 ms |
78556 KB |
Output is correct |
50 |
Correct |
38 ms |
78548 KB |
Output is correct |
51 |
Correct |
34 ms |
78684 KB |
Output is correct |
52 |
Correct |
33 ms |
78652 KB |
Output is correct |
53 |
Correct |
62 ms |
79352 KB |
Output is correct |
54 |
Correct |
86 ms |
80776 KB |
Output is correct |
55 |
Correct |
90 ms |
81004 KB |
Output is correct |
56 |
Correct |
90 ms |
80368 KB |
Output is correct |
57 |
Correct |
111 ms |
81860 KB |
Output is correct |
58 |
Correct |
65 ms |
79508 KB |
Output is correct |
59 |
Correct |
82 ms |
80328 KB |
Output is correct |