#include <bits/stdc++.h>
using namespace std;
#define task ""
const int INF = 1e9 + 7;
const int MAX = 50005;
int k, n, m, q;
int dp[16][MAX][5][5];
int tmp[5][5], newTmp[5][5];
bool minimize(int &x, const int &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
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;
memset(dp, 0x3f, sizeof dp);
while (m--) {
int a, b, t; cin >> a >> b >> t;
minimize(dp[0][a / k][a % k][b % k], t);
}
for (int j = 1; j <= 15; ++j) {
for (int i = 0; i < n; ++i) if (i / k + (1 << j) <= (n - 1) / k) {
int a = i / k, b = i % k;
for (int c = 0; c < k; ++c) {
for (int d = 0; d < k; ++d)
minimize(dp[j][a][b][d], dp[j - 1][a][b][c] + dp[j - 1][a + (1 << j - 1)][c][d]);
}
}
}
while (q--) {
int a, b; cin >> a >> b; int s = a / k, t = b / k;
if (s == t) {
cout << (a != b ? -1 : 0) << '\n';
continue;
}
memset(tmp, 0x3f, sizeof tmp);
for (int i = 0; i < k; ++i) tmp[i][i] = 0;
for (int j = 0, l = t - s; (1 << j) <= l; ++j) {
if (l >> j & 1) {
memset(newTmp, 0x3f, sizeof newTmp);
for (int a = 0; a < k; ++a) {
for (int b = 0; b < k; ++b) {
for (int c = 0; c < k; ++c)
minimize(newTmp[a][c], tmp[a][b] + dp[j][s][b][c]);
}
}
for (int a = 0; a < k; ++a) {
for (int b = 0; b < k; ++b)
tmp[a][b] = newTmp[a][b];
}
s += 1 << j;
}
}
cout << (tmp[a % k][b % k] >= INF ? -1 : tmp[a % k][b % k]) << '\n';
}
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:38:89: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
38 | minimize(dp[j][a][b][d], dp[j - 1][a][b][c] + dp[j - 1][a + (1 << j - 1)][c][d]);
| ~~^~~
toll.cpp:23:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
78680 KB |
Output is correct |
2 |
Correct |
9 ms |
78680 KB |
Output is correct |
3 |
Correct |
9 ms |
78684 KB |
Output is correct |
4 |
Correct |
10 ms |
78684 KB |
Output is correct |
5 |
Correct |
10 ms |
78564 KB |
Output is correct |
6 |
Correct |
9 ms |
78684 KB |
Output is correct |
7 |
Correct |
10 ms |
78684 KB |
Output is correct |
8 |
Correct |
36 ms |
78744 KB |
Output is correct |
9 |
Correct |
33 ms |
78684 KB |
Output is correct |
10 |
Correct |
23 ms |
78736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
78564 KB |
Output is correct |
2 |
Correct |
9 ms |
78684 KB |
Output is correct |
3 |
Correct |
9 ms |
78684 KB |
Output is correct |
4 |
Correct |
9 ms |
78680 KB |
Output is correct |
5 |
Correct |
10 ms |
78684 KB |
Output is correct |
6 |
Correct |
9 ms |
78544 KB |
Output is correct |
7 |
Correct |
12 ms |
78684 KB |
Output is correct |
8 |
Correct |
12 ms |
78844 KB |
Output is correct |
9 |
Correct |
35 ms |
79504 KB |
Output is correct |
10 |
Correct |
57 ms |
80012 KB |
Output is correct |
11 |
Correct |
42 ms |
79608 KB |
Output is correct |
12 |
Correct |
40 ms |
79696 KB |
Output is correct |
13 |
Correct |
52 ms |
80216 KB |
Output is correct |
14 |
Correct |
34 ms |
79748 KB |
Output is correct |
15 |
Correct |
43 ms |
79368 KB |
Output is correct |
16 |
Correct |
42 ms |
79452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
78684 KB |
Output is correct |
2 |
Correct |
9 ms |
78684 KB |
Output is correct |
3 |
Correct |
9 ms |
78544 KB |
Output is correct |
4 |
Correct |
9 ms |
78936 KB |
Output is correct |
5 |
Correct |
9 ms |
78540 KB |
Output is correct |
6 |
Correct |
10 ms |
78684 KB |
Output is correct |
7 |
Correct |
11 ms |
78684 KB |
Output is correct |
8 |
Correct |
11 ms |
78800 KB |
Output is correct |
9 |
Correct |
11 ms |
78552 KB |
Output is correct |
10 |
Correct |
30 ms |
79380 KB |
Output is correct |
11 |
Correct |
36 ms |
79696 KB |
Output is correct |
12 |
Correct |
46 ms |
80004 KB |
Output is correct |
13 |
Correct |
50 ms |
80224 KB |
Output is correct |
14 |
Correct |
44 ms |
79964 KB |
Output is correct |
15 |
Correct |
39 ms |
79440 KB |
Output is correct |
16 |
Correct |
39 ms |
79452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
78684 KB |
Output is correct |
2 |
Correct |
9 ms |
78684 KB |
Output is correct |
3 |
Correct |
9 ms |
78544 KB |
Output is correct |
4 |
Correct |
9 ms |
78936 KB |
Output is correct |
5 |
Correct |
9 ms |
78540 KB |
Output is correct |
6 |
Correct |
10 ms |
78684 KB |
Output is correct |
7 |
Correct |
11 ms |
78684 KB |
Output is correct |
8 |
Correct |
11 ms |
78800 KB |
Output is correct |
9 |
Correct |
11 ms |
78552 KB |
Output is correct |
10 |
Correct |
30 ms |
79380 KB |
Output is correct |
11 |
Correct |
36 ms |
79696 KB |
Output is correct |
12 |
Correct |
46 ms |
80004 KB |
Output is correct |
13 |
Correct |
50 ms |
80224 KB |
Output is correct |
14 |
Correct |
44 ms |
79964 KB |
Output is correct |
15 |
Correct |
39 ms |
79440 KB |
Output is correct |
16 |
Correct |
39 ms |
79452 KB |
Output is correct |
17 |
Correct |
39 ms |
79696 KB |
Output is correct |
18 |
Correct |
10 ms |
78500 KB |
Output is correct |
19 |
Correct |
9 ms |
78608 KB |
Output is correct |
20 |
Correct |
9 ms |
78484 KB |
Output is correct |
21 |
Correct |
9 ms |
78936 KB |
Output is correct |
22 |
Correct |
9 ms |
78544 KB |
Output is correct |
23 |
Correct |
10 ms |
78696 KB |
Output is correct |
24 |
Correct |
10 ms |
78684 KB |
Output is correct |
25 |
Correct |
14 ms |
78680 KB |
Output is correct |
26 |
Correct |
12 ms |
78684 KB |
Output is correct |
27 |
Correct |
32 ms |
79452 KB |
Output is correct |
28 |
Correct |
49 ms |
79868 KB |
Output is correct |
29 |
Correct |
53 ms |
80256 KB |
Output is correct |
30 |
Correct |
47 ms |
80216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
78680 KB |
Output is correct |
2 |
Correct |
9 ms |
78680 KB |
Output is correct |
3 |
Correct |
9 ms |
78684 KB |
Output is correct |
4 |
Correct |
10 ms |
78684 KB |
Output is correct |
5 |
Correct |
10 ms |
78564 KB |
Output is correct |
6 |
Correct |
9 ms |
78684 KB |
Output is correct |
7 |
Correct |
10 ms |
78684 KB |
Output is correct |
8 |
Correct |
36 ms |
78744 KB |
Output is correct |
9 |
Correct |
33 ms |
78684 KB |
Output is correct |
10 |
Correct |
23 ms |
78736 KB |
Output is correct |
11 |
Correct |
41 ms |
78564 KB |
Output is correct |
12 |
Correct |
9 ms |
78684 KB |
Output is correct |
13 |
Correct |
9 ms |
78684 KB |
Output is correct |
14 |
Correct |
9 ms |
78680 KB |
Output is correct |
15 |
Correct |
10 ms |
78684 KB |
Output is correct |
16 |
Correct |
9 ms |
78544 KB |
Output is correct |
17 |
Correct |
12 ms |
78684 KB |
Output is correct |
18 |
Correct |
12 ms |
78844 KB |
Output is correct |
19 |
Correct |
35 ms |
79504 KB |
Output is correct |
20 |
Correct |
57 ms |
80012 KB |
Output is correct |
21 |
Correct |
42 ms |
79608 KB |
Output is correct |
22 |
Correct |
40 ms |
79696 KB |
Output is correct |
23 |
Correct |
52 ms |
80216 KB |
Output is correct |
24 |
Correct |
34 ms |
79748 KB |
Output is correct |
25 |
Correct |
43 ms |
79368 KB |
Output is correct |
26 |
Correct |
42 ms |
79452 KB |
Output is correct |
27 |
Correct |
9 ms |
78684 KB |
Output is correct |
28 |
Correct |
9 ms |
78684 KB |
Output is correct |
29 |
Correct |
9 ms |
78544 KB |
Output is correct |
30 |
Correct |
9 ms |
78936 KB |
Output is correct |
31 |
Correct |
9 ms |
78540 KB |
Output is correct |
32 |
Correct |
10 ms |
78684 KB |
Output is correct |
33 |
Correct |
11 ms |
78684 KB |
Output is correct |
34 |
Correct |
11 ms |
78800 KB |
Output is correct |
35 |
Correct |
11 ms |
78552 KB |
Output is correct |
36 |
Correct |
30 ms |
79380 KB |
Output is correct |
37 |
Correct |
36 ms |
79696 KB |
Output is correct |
38 |
Correct |
46 ms |
80004 KB |
Output is correct |
39 |
Correct |
50 ms |
80224 KB |
Output is correct |
40 |
Correct |
44 ms |
79964 KB |
Output is correct |
41 |
Correct |
39 ms |
79440 KB |
Output is correct |
42 |
Correct |
39 ms |
79452 KB |
Output is correct |
43 |
Correct |
39 ms |
79696 KB |
Output is correct |
44 |
Correct |
10 ms |
78500 KB |
Output is correct |
45 |
Correct |
9 ms |
78608 KB |
Output is correct |
46 |
Correct |
9 ms |
78484 KB |
Output is correct |
47 |
Correct |
9 ms |
78936 KB |
Output is correct |
48 |
Correct |
9 ms |
78544 KB |
Output is correct |
49 |
Correct |
10 ms |
78696 KB |
Output is correct |
50 |
Correct |
10 ms |
78684 KB |
Output is correct |
51 |
Correct |
14 ms |
78680 KB |
Output is correct |
52 |
Correct |
12 ms |
78684 KB |
Output is correct |
53 |
Correct |
32 ms |
79452 KB |
Output is correct |
54 |
Correct |
49 ms |
79868 KB |
Output is correct |
55 |
Correct |
53 ms |
80256 KB |
Output is correct |
56 |
Correct |
47 ms |
80216 KB |
Output is correct |
57 |
Correct |
76 ms |
80476 KB |
Output is correct |
58 |
Correct |
42 ms |
79696 KB |
Output is correct |
59 |
Correct |
45 ms |
79696 KB |
Output is correct |