#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/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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
83792 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
4 ms |
23052 KB |
Output is correct |
6 |
Correct |
3 ms |
22876 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
39 ms |
83648 KB |
Output is correct |
9 |
Correct |
40 ms |
83796 KB |
Output is correct |
10 |
Correct |
27 ms |
83800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
83732 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 |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
4 ms |
22872 KB |
Output is correct |
8 |
Correct |
5 ms |
23108 KB |
Output is correct |
9 |
Incorrect |
40 ms |
83824 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 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 |
3 ms |
22876 KB |
Output is correct |
7 |
Correct |
4 ms |
23032 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
37 ms |
83628 KB |
Output is correct |
11 |
Correct |
43 ms |
83636 KB |
Output is correct |
12 |
Correct |
50 ms |
83548 KB |
Output is correct |
13 |
Correct |
54 ms |
83684 KB |
Output is correct |
14 |
Correct |
53 ms |
83628 KB |
Output is correct |
15 |
Correct |
49 ms |
74048 KB |
Output is correct |
16 |
Correct |
41 ms |
74060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 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 |
3 ms |
22876 KB |
Output is correct |
7 |
Correct |
4 ms |
23032 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
37 ms |
83628 KB |
Output is correct |
11 |
Correct |
43 ms |
83636 KB |
Output is correct |
12 |
Correct |
50 ms |
83548 KB |
Output is correct |
13 |
Correct |
54 ms |
83684 KB |
Output is correct |
14 |
Correct |
53 ms |
83628 KB |
Output is correct |
15 |
Correct |
49 ms |
74048 KB |
Output is correct |
16 |
Correct |
41 ms |
74060 KB |
Output is correct |
17 |
Correct |
41 ms |
83720 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10712 KB |
Output is correct |
20 |
Correct |
1 ms |
10584 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
22876 KB |
Output is correct |
24 |
Correct |
4 ms |
22872 KB |
Output is correct |
25 |
Correct |
4 ms |
22996 KB |
Output is correct |
26 |
Correct |
4 ms |
22876 KB |
Output is correct |
27 |
Correct |
39 ms |
83804 KB |
Output is correct |
28 |
Correct |
52 ms |
83636 KB |
Output is correct |
29 |
Correct |
59 ms |
83544 KB |
Output is correct |
30 |
Correct |
47 ms |
83544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
83792 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
4 ms |
23052 KB |
Output is correct |
6 |
Correct |
3 ms |
22876 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
39 ms |
83648 KB |
Output is correct |
9 |
Correct |
40 ms |
83796 KB |
Output is correct |
10 |
Correct |
27 ms |
83800 KB |
Output is correct |
11 |
Correct |
43 ms |
83732 KB |
Output is correct |
12 |
Correct |
1 ms |
10584 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
4 ms |
22872 KB |
Output is correct |
18 |
Correct |
5 ms |
23108 KB |
Output is correct |
19 |
Incorrect |
40 ms |
83824 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |