#include <bits/stdc++.h>
using namespace std;
const int N = 5e4;
struct query
{
int a, b, id, ans;
} queries[N];
int k, n, m, o, cost[N][5][5];
void dnc(int l, int r, int level = 0)
{
if (l == r) return;
int mid = (l+r)/2, L[n/k+1][k][k], R[n/k+1][k][k];
for (int i = l; i <= r; i++)
for (int a = 0; a < k; a++)
for (int b = 0; b < k; b++)
L[i][a][b] = R[i][a][b] = 1e9;
for (int a = 0; a < k; a++)
L[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++)
L[i][a][c] = min(L[i][a][c], L[i+1][b][c]+cost[i][a][b]);
for (int a = 0; a < k; a++)
for (int b = 0; b < k; b++)
R[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++)
R[i][c][a] = min(R[i][c][a], R[i-1][c][b]+cost[i-1][b][a]);
for (int i = 0; i < o; i++)
{
int a = queries[i].a, b = queries[i].b;
if (queries[i].ans == 1e9 && l <= a/k && a/k <= mid && mid+1 <= b/k && b/k <= r)
for (int j = 0; j < k; j++)
queries[i].ans = min(queries[i].ans, L[a/k][a%k][j]+R[b/k][j][b%k]);
}
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 i = 0; i < m; i++)
{
int a, b, t;
cin >> a >> b >> t;
cost[a/k][a%k][b%k] = t;
}
for (int i = 0; i < o; i++)
cin >> queries[i].a >> queries[i].b, queries[i].id = i, queries[i].ans = 1e9;
dnc(0, n/k);
for (int i = 0; i < o; i++)
cout << (queries[i].ans == 1e9 ? -1 : queries[i].ans) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
700 ms |
12664 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
676 KB |
Output is correct |
6 |
Correct |
6 ms |
604 KB |
Output is correct |
7 |
Correct |
6 ms |
604 KB |
Output is correct |
8 |
Execution timed out |
3056 ms |
12636 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
709 ms |
19028 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
45 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
860 KB |
Output is correct |
9 |
Correct |
2231 ms |
12636 KB |
Output is correct |
10 |
Correct |
188 ms |
25428 KB |
Output is correct |
11 |
Correct |
200 ms |
18772 KB |
Output is correct |
12 |
Correct |
828 ms |
24380 KB |
Output is correct |
13 |
Correct |
80 ms |
22264 KB |
Output is correct |
14 |
Correct |
65 ms |
15956 KB |
Output is correct |
15 |
Correct |
70 ms |
21268 KB |
Output is correct |
16 |
Correct |
71 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
125 ms |
12500 KB |
Output is correct |
11 |
Correct |
113 ms |
18600 KB |
Output is correct |
12 |
Correct |
122 ms |
25072 KB |
Output is correct |
13 |
Correct |
125 ms |
25168 KB |
Output is correct |
14 |
Correct |
106 ms |
24748 KB |
Output is correct |
15 |
Correct |
61 ms |
21084 KB |
Output is correct |
16 |
Correct |
62 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
125 ms |
12500 KB |
Output is correct |
11 |
Correct |
113 ms |
18600 KB |
Output is correct |
12 |
Correct |
122 ms |
25072 KB |
Output is correct |
13 |
Correct |
125 ms |
25168 KB |
Output is correct |
14 |
Correct |
106 ms |
24748 KB |
Output is correct |
15 |
Correct |
61 ms |
21084 KB |
Output is correct |
16 |
Correct |
62 ms |
21084 KB |
Output is correct |
17 |
Correct |
203 ms |
18624 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
600 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
17 ms |
720 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
856 KB |
Output is correct |
27 |
Correct |
1014 ms |
12480 KB |
Output is correct |
28 |
Correct |
182 ms |
25188 KB |
Output is correct |
29 |
Correct |
182 ms |
25668 KB |
Output is correct |
30 |
Correct |
314 ms |
24828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
700 ms |
12664 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
676 KB |
Output is correct |
6 |
Correct |
6 ms |
604 KB |
Output is correct |
7 |
Correct |
6 ms |
604 KB |
Output is correct |
8 |
Execution timed out |
3056 ms |
12636 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |