#include <bits/stdc++.h>
#define ll long long
#define INF 1e18
using namespace std;
ll dp[50001][17][5][5],K,n,m,Q, ans[5][5], tmp[5][5];
void combine(ll target[5][5], ll a[5][5], ll b[5][5]) {
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++) {
target[i][j] = INF;
for (int mid = 0; mid < K; mid++)
target[i][j] = min(target[i][j],a[i][mid] + b[mid][j]);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> K >> n >> m >> Q;
int maxU = n/K + (n % K != 0);
for (int i = 0; i <= maxU; i++)
for (int j = 0; j <= 16; j++)
for (int k = 0; k < K; k++)
for (int l = 0; l < K; l++) dp[i][j][k][l] = INF;
for (int i = 1; i <= m; i++) {
int u,v,w;
cin >> u >> v >> w;
dp[u/K][0][u%K][v%K] = w;
}
for (int i = maxU; i >= 0; i--) {
for (int j = 1; i + (1 << j) - 1 <= maxU; j++) {
combine(dp[i][j],dp[i][j-1],dp[i + (1 << (j-1))][j-1]);
}
}
while (Q--) {
int u,v;
cin >> u >> v;
int U = u/K, V = v/K;
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++)
if (i != j) ans[i][j] = INF;
else ans[i][j] = 0;
for (int i = 16; i >= 0; i--) {
if (U + (1 << i) <= V) {
for (int i = 0; i < K; i++)
for (int j = 0; j < K; j++) tmp[i][j] = ans[i][j];
combine(ans,tmp,dp[U][i]);
U += (1 << i);
}
}
if (ans[u % K][v % K] == INF) cout << "-1\n";
else cout << ans[u % K][v % K] << '\n';
}
return 0;
}
/*
5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
167560 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
3664 KB |
Output is correct |
6 |
Correct |
2 ms |
3668 KB |
Output is correct |
7 |
Correct |
3 ms |
3668 KB |
Output is correct |
8 |
Correct |
124 ms |
167560 KB |
Output is correct |
9 |
Correct |
106 ms |
167524 KB |
Output is correct |
10 |
Correct |
96 ms |
166696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
85056 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
6 ms |
3668 KB |
Output is correct |
8 |
Correct |
5 ms |
2132 KB |
Output is correct |
9 |
Correct |
123 ms |
167520 KB |
Output is correct |
10 |
Correct |
107 ms |
58060 KB |
Output is correct |
11 |
Correct |
114 ms |
85168 KB |
Output is correct |
12 |
Correct |
68 ms |
57024 KB |
Output is correct |
13 |
Correct |
67 ms |
22696 KB |
Output is correct |
14 |
Correct |
56 ms |
34980 KB |
Output is correct |
15 |
Correct |
60 ms |
21440 KB |
Output is correct |
16 |
Correct |
56 ms |
21420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
2 ms |
3668 KB |
Output is correct |
7 |
Correct |
3 ms |
1968 KB |
Output is correct |
8 |
Correct |
2 ms |
1060 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
97 ms |
167328 KB |
Output is correct |
11 |
Correct |
85 ms |
85004 KB |
Output is correct |
12 |
Correct |
75 ms |
57924 KB |
Output is correct |
13 |
Correct |
86 ms |
58164 KB |
Output is correct |
14 |
Correct |
79 ms |
57572 KB |
Output is correct |
15 |
Correct |
44 ms |
21464 KB |
Output is correct |
16 |
Correct |
44 ms |
21460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
2 ms |
3668 KB |
Output is correct |
7 |
Correct |
3 ms |
1968 KB |
Output is correct |
8 |
Correct |
2 ms |
1060 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
97 ms |
167328 KB |
Output is correct |
11 |
Correct |
85 ms |
85004 KB |
Output is correct |
12 |
Correct |
75 ms |
57924 KB |
Output is correct |
13 |
Correct |
86 ms |
58164 KB |
Output is correct |
14 |
Correct |
79 ms |
57572 KB |
Output is correct |
15 |
Correct |
44 ms |
21464 KB |
Output is correct |
16 |
Correct |
44 ms |
21460 KB |
Output is correct |
17 |
Correct |
75 ms |
85032 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
3 ms |
3668 KB |
Output is correct |
24 |
Correct |
2 ms |
2000 KB |
Output is correct |
25 |
Correct |
4 ms |
980 KB |
Output is correct |
26 |
Correct |
3 ms |
1236 KB |
Output is correct |
27 |
Correct |
109 ms |
167484 KB |
Output is correct |
28 |
Correct |
80 ms |
57920 KB |
Output is correct |
29 |
Correct |
108 ms |
58180 KB |
Output is correct |
30 |
Correct |
73 ms |
57544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
167560 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
3664 KB |
Output is correct |
6 |
Correct |
2 ms |
3668 KB |
Output is correct |
7 |
Correct |
3 ms |
3668 KB |
Output is correct |
8 |
Correct |
124 ms |
167560 KB |
Output is correct |
9 |
Correct |
106 ms |
167524 KB |
Output is correct |
10 |
Correct |
96 ms |
166696 KB |
Output is correct |
11 |
Correct |
87 ms |
85056 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
6 ms |
3668 KB |
Output is correct |
18 |
Correct |
5 ms |
2132 KB |
Output is correct |
19 |
Correct |
123 ms |
167520 KB |
Output is correct |
20 |
Correct |
107 ms |
58060 KB |
Output is correct |
21 |
Correct |
114 ms |
85168 KB |
Output is correct |
22 |
Correct |
68 ms |
57024 KB |
Output is correct |
23 |
Correct |
67 ms |
22696 KB |
Output is correct |
24 |
Correct |
56 ms |
34980 KB |
Output is correct |
25 |
Correct |
60 ms |
21440 KB |
Output is correct |
26 |
Correct |
56 ms |
21420 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
328 KB |
Output is correct |
29 |
Correct |
1 ms |
328 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
2 ms |
3668 KB |
Output is correct |
33 |
Correct |
3 ms |
1968 KB |
Output is correct |
34 |
Correct |
2 ms |
1060 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
97 ms |
167328 KB |
Output is correct |
37 |
Correct |
85 ms |
85004 KB |
Output is correct |
38 |
Correct |
75 ms |
57924 KB |
Output is correct |
39 |
Correct |
86 ms |
58164 KB |
Output is correct |
40 |
Correct |
79 ms |
57572 KB |
Output is correct |
41 |
Correct |
44 ms |
21464 KB |
Output is correct |
42 |
Correct |
44 ms |
21460 KB |
Output is correct |
43 |
Correct |
75 ms |
85032 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
3 ms |
3668 KB |
Output is correct |
50 |
Correct |
2 ms |
2000 KB |
Output is correct |
51 |
Correct |
4 ms |
980 KB |
Output is correct |
52 |
Correct |
3 ms |
1236 KB |
Output is correct |
53 |
Correct |
109 ms |
167484 KB |
Output is correct |
54 |
Correct |
80 ms |
57920 KB |
Output is correct |
55 |
Correct |
108 ms |
58180 KB |
Output is correct |
56 |
Correct |
73 ms |
57544 KB |
Output is correct |
57 |
Correct |
99 ms |
45216 KB |
Output is correct |
58 |
Correct |
122 ms |
167524 KB |
Output is correct |
59 |
Correct |
90 ms |
85244 KB |
Output is correct |