# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
979322 |
2024-05-10T15:31:46 Z |
chautanphat |
Toll (BOI17_toll) |
C++14 |
|
74 ms |
83832 KB |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4;
int k, n, m, o, cost[N][5][5], ans[__lg(N)+1][N][5][5], mask[N];
void dnc(int l, int r, int level = 0)
{
if (l == r) return;
int mid = (l+r)/2;
for (int a = 0; a < k; a++)
ans[level][mid][a][a] = 0;
for (int i = mid-1; i >= l; i--)
for (int a = 0; a < k; a++)
for (int b = 0; b < k; b++)
for (int c = 0; c < k; c++)
ans[level][i][a][c] = min(ans[level][i][a][c], ans[level][i+1][b][c]+cost[i][a][b]);
for (int a = 0; a < k; a++)
for (int b = 0; b < k; b++)
ans[level][mid+1][a][b] = cost[mid][a][b];
for (int i = mid+2; i <= r; i++)
for (int a = 0; a < k; a++)
for (int b = 0; b < k; b++)
for (int c = 0; c < k; c++)
ans[level][i][c][a] = min(ans[level][i][c][a], ans[level][i-1][c][b]+cost[i-1][b][a]);
for (int i = mid+1; i <= r; i++) mask[i] |= 1<<level;
dnc(l, mid, level+1);
dnc(mid+1, r, level+1);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> k >> n >> m >> o;
for (int i = 0; i < n; i++)
for (int a = 0; a < k; a++)
for (int b = 0; b < k; b++)
cost[i][a][b] = 1e9;
for (int level = 0; (1<<level) < n; level++)
for (int i = 0; i < n; i++)
for (int a = 0; a < k; a++)
for (int b = 0; b < k; b++)
ans[level][i][a][b] = 1e9;
for (int i = 0; i < m; i++)
{
int a, b, t;
cin >> a >> b >> t;
cost[a/k][a%k][b%k] = t;
}
dnc(0, (n-1)/k);
for (int i = 0; i < o; i++)
{
int a, b;
cin >> a >> b;
if (a/k == b/k)
{
cout << -1 << '\n';
continue;
}
int level = __builtin_ctz(mask[a/k]^mask[b/k]), res = 1e9;
for (int j = 0; j < k; j++)
res = min(res, ans[level][a/k][a%k][j]+ans[level][b/k][j][b%k]);
cout << (res == 1e9 ? -1 : res) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
83792 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
22876 KB |
Output is correct |
6 |
Correct |
4 ms |
22872 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
36 ms |
83636 KB |
Output is correct |
9 |
Correct |
36 ms |
83792 KB |
Output is correct |
10 |
Correct |
23 ms |
83804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
83620 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
36 ms |
83832 KB |
Output is correct |
10 |
Correct |
52 ms |
83544 KB |
Output is correct |
11 |
Correct |
40 ms |
83620 KB |
Output is correct |
12 |
Correct |
42 ms |
83548 KB |
Output is correct |
13 |
Correct |
53 ms |
76380 KB |
Output is correct |
14 |
Correct |
32 ms |
76380 KB |
Output is correct |
15 |
Correct |
40 ms |
76392 KB |
Output is correct |
16 |
Correct |
41 ms |
76380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
22872 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22872 KB |
Output is correct |
10 |
Correct |
33 ms |
83636 KB |
Output is correct |
11 |
Correct |
37 ms |
83544 KB |
Output is correct |
12 |
Correct |
48 ms |
83568 KB |
Output is correct |
13 |
Correct |
49 ms |
83568 KB |
Output is correct |
14 |
Correct |
44 ms |
83548 KB |
Output is correct |
15 |
Correct |
40 ms |
76380 KB |
Output is correct |
16 |
Correct |
39 ms |
76380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
4 ms |
22872 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22872 KB |
Output is correct |
10 |
Correct |
33 ms |
83636 KB |
Output is correct |
11 |
Correct |
37 ms |
83544 KB |
Output is correct |
12 |
Correct |
48 ms |
83568 KB |
Output is correct |
13 |
Correct |
49 ms |
83568 KB |
Output is correct |
14 |
Correct |
44 ms |
83548 KB |
Output is correct |
15 |
Correct |
40 ms |
76380 KB |
Output is correct |
16 |
Correct |
39 ms |
76380 KB |
Output is correct |
17 |
Correct |
38 ms |
83544 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
4 ms |
22876 KB |
Output is correct |
24 |
Correct |
4 ms |
22876 KB |
Output is correct |
25 |
Correct |
4 ms |
22876 KB |
Output is correct |
26 |
Correct |
4 ms |
22872 KB |
Output is correct |
27 |
Correct |
34 ms |
83808 KB |
Output is correct |
28 |
Correct |
48 ms |
83636 KB |
Output is correct |
29 |
Correct |
55 ms |
83636 KB |
Output is correct |
30 |
Correct |
45 ms |
83688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
83792 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
22876 KB |
Output is correct |
6 |
Correct |
4 ms |
22872 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
36 ms |
83636 KB |
Output is correct |
9 |
Correct |
36 ms |
83792 KB |
Output is correct |
10 |
Correct |
23 ms |
83804 KB |
Output is correct |
11 |
Correct |
39 ms |
83620 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
5 ms |
22876 KB |
Output is correct |
18 |
Correct |
5 ms |
22876 KB |
Output is correct |
19 |
Correct |
36 ms |
83832 KB |
Output is correct |
20 |
Correct |
52 ms |
83544 KB |
Output is correct |
21 |
Correct |
40 ms |
83620 KB |
Output is correct |
22 |
Correct |
42 ms |
83548 KB |
Output is correct |
23 |
Correct |
53 ms |
76380 KB |
Output is correct |
24 |
Correct |
32 ms |
76380 KB |
Output is correct |
25 |
Correct |
40 ms |
76392 KB |
Output is correct |
26 |
Correct |
41 ms |
76380 KB |
Output is correct |
27 |
Correct |
1 ms |
10584 KB |
Output is correct |
28 |
Correct |
1 ms |
10584 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
4 ms |
22872 KB |
Output is correct |
33 |
Correct |
3 ms |
22876 KB |
Output is correct |
34 |
Correct |
4 ms |
22876 KB |
Output is correct |
35 |
Correct |
4 ms |
22872 KB |
Output is correct |
36 |
Correct |
33 ms |
83636 KB |
Output is correct |
37 |
Correct |
37 ms |
83544 KB |
Output is correct |
38 |
Correct |
48 ms |
83568 KB |
Output is correct |
39 |
Correct |
49 ms |
83568 KB |
Output is correct |
40 |
Correct |
44 ms |
83548 KB |
Output is correct |
41 |
Correct |
40 ms |
76380 KB |
Output is correct |
42 |
Correct |
39 ms |
76380 KB |
Output is correct |
43 |
Correct |
38 ms |
83544 KB |
Output is correct |
44 |
Correct |
1 ms |
10588 KB |
Output is correct |
45 |
Correct |
1 ms |
10588 KB |
Output is correct |
46 |
Correct |
1 ms |
10588 KB |
Output is correct |
47 |
Correct |
1 ms |
10588 KB |
Output is correct |
48 |
Correct |
1 ms |
10588 KB |
Output is correct |
49 |
Correct |
4 ms |
22876 KB |
Output is correct |
50 |
Correct |
4 ms |
22876 KB |
Output is correct |
51 |
Correct |
4 ms |
22876 KB |
Output is correct |
52 |
Correct |
4 ms |
22872 KB |
Output is correct |
53 |
Correct |
34 ms |
83808 KB |
Output is correct |
54 |
Correct |
48 ms |
83636 KB |
Output is correct |
55 |
Correct |
55 ms |
83636 KB |
Output is correct |
56 |
Correct |
45 ms |
83688 KB |
Output is correct |
57 |
Correct |
74 ms |
83544 KB |
Output is correct |
58 |
Correct |
36 ms |
83816 KB |
Output is correct |
59 |
Correct |
41 ms |
83780 KB |
Output is correct |