/*
Picture the nodes are arranged in a 2D matrix with K rows
where each column contains nodes with the same floor(index/K).
dp[node][jump][k] = minimum cost to go from node "node" to the k-th node located in column node/K + 2^jump.
*/
#include<bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define MAXN 50000
#define MAXLG 15
#define MAXK 5
int K, N, M, Q, dp[MAXN + 3][MAXLG + 3][MAXK + 3];
int main() {
cin >> K >> N >> M >> Q;
for (int i = 0; i < N; i++) {
for (int jump = 0; jump <= MAXLG; jump++) {
for (int k = 0; k <= K; k++) {
dp[i][jump][k] = INF;
}
}
}
for (int i = 0; i < M; i++) {
int a, b, t;
cin >> a >> b >> t;
dp[a][0][b%K] = t;
}
int numberOfColumns = N/K + (N%K == 0 ? 0 : 1);
for (int jump = 1; jump <= MAXLG; jump++) {
for (int i = 0; i < N; i++) {
int currentColumn = i/K;
int destinationColumn = currentColumn + (1 << jump);
if (destinationColumn >= numberOfColumns) {
continue;
}
for (int destinationK = 0; destinationK < K; destinationK++) {
for (int transitK = 0; transitK < K; transitK++) {
int transitColumn = currentColumn + (1 << (jump - 1));
int transitNode = transitColumn * K + transitK;
dp[i][jump][destinationK] = min(dp[i][jump][destinationK], dp[i][jump - 1][transitK] + dp[transitNode][jump - 1][destinationK]);
}
}
}
}
for (int q = 0; q < Q; q++) {
int a, b;
cin >> a >> b;
int currentColumn = a/K, destinationColumn = b/K;
int columnDiff = destinationColumn - currentColumn;
map<int, int> currentNodes;
currentNodes[a] = 0;
for (int jump = 0; jump <= MAXLG; jump++) {
if (columnDiff & (1 << jump)) {
map<int, int> newNodes;
for (auto [currentNode, currentCost]: currentNodes) {
for (int destinationK = 0; destinationK < K; destinationK++) {
int destinationColumn = currentColumn + (1 << jump);
int destinationNode = destinationColumn * K + destinationK;
int newCost = min(INF, currentCost + dp[currentNode][jump][destinationK]);
if (newNodes.find(destinationNode) == newNodes.end()) {
newNodes[destinationNode] = newCost;
} else {
newNodes[destinationNode] = min(newNodes[destinationNode], newCost);
}
}
}
swap(currentNodes, newNodes);
currentColumn += (1 << jump);
}
}
if (currentNodes.find(b) != currentNodes.end() && currentNodes[b] < INF) {
cout << currentNodes[b] << endl;
} else {
cout << -1 << endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
28460 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
94 ms |
28492 KB |
Output is correct |
9 |
Correct |
89 ms |
28472 KB |
Output is correct |
10 |
Correct |
51 ms |
28492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
28508 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
22 ms |
884 KB |
Output is correct |
8 |
Correct |
26 ms |
912 KB |
Output is correct |
9 |
Correct |
91 ms |
28492 KB |
Output is correct |
10 |
Correct |
193 ms |
28456 KB |
Output is correct |
11 |
Correct |
134 ms |
28588 KB |
Output is correct |
12 |
Correct |
129 ms |
28492 KB |
Output is correct |
13 |
Correct |
167 ms |
17220 KB |
Output is correct |
14 |
Correct |
121 ms |
17228 KB |
Output is correct |
15 |
Correct |
97 ms |
17192 KB |
Output is correct |
16 |
Correct |
92 ms |
17212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
852 KB |
Output is correct |
10 |
Correct |
79 ms |
28372 KB |
Output is correct |
11 |
Correct |
104 ms |
28456 KB |
Output is correct |
12 |
Correct |
155 ms |
28464 KB |
Output is correct |
13 |
Correct |
163 ms |
28372 KB |
Output is correct |
14 |
Correct |
126 ms |
28460 KB |
Output is correct |
15 |
Correct |
89 ms |
17192 KB |
Output is correct |
16 |
Correct |
98 ms |
17188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
852 KB |
Output is correct |
10 |
Correct |
79 ms |
28372 KB |
Output is correct |
11 |
Correct |
104 ms |
28456 KB |
Output is correct |
12 |
Correct |
155 ms |
28464 KB |
Output is correct |
13 |
Correct |
163 ms |
28372 KB |
Output is correct |
14 |
Correct |
126 ms |
28460 KB |
Output is correct |
15 |
Correct |
89 ms |
17192 KB |
Output is correct |
16 |
Correct |
98 ms |
17188 KB |
Output is correct |
17 |
Correct |
117 ms |
28460 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
8 ms |
852 KB |
Output is correct |
24 |
Correct |
14 ms |
880 KB |
Output is correct |
25 |
Correct |
23 ms |
880 KB |
Output is correct |
26 |
Correct |
13 ms |
852 KB |
Output is correct |
27 |
Correct |
79 ms |
28468 KB |
Output is correct |
28 |
Correct |
165 ms |
28456 KB |
Output is correct |
29 |
Correct |
194 ms |
28456 KB |
Output is correct |
30 |
Correct |
159 ms |
28460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
28460 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
94 ms |
28492 KB |
Output is correct |
9 |
Correct |
89 ms |
28472 KB |
Output is correct |
10 |
Correct |
51 ms |
28492 KB |
Output is correct |
11 |
Correct |
151 ms |
28508 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
22 ms |
884 KB |
Output is correct |
18 |
Correct |
26 ms |
912 KB |
Output is correct |
19 |
Correct |
91 ms |
28492 KB |
Output is correct |
20 |
Correct |
193 ms |
28456 KB |
Output is correct |
21 |
Correct |
134 ms |
28588 KB |
Output is correct |
22 |
Correct |
129 ms |
28492 KB |
Output is correct |
23 |
Correct |
167 ms |
17220 KB |
Output is correct |
24 |
Correct |
121 ms |
17228 KB |
Output is correct |
25 |
Correct |
97 ms |
17192 KB |
Output is correct |
26 |
Correct |
92 ms |
17212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
224 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
852 KB |
Output is correct |
33 |
Correct |
2 ms |
852 KB |
Output is correct |
34 |
Correct |
4 ms |
852 KB |
Output is correct |
35 |
Correct |
4 ms |
852 KB |
Output is correct |
36 |
Correct |
79 ms |
28372 KB |
Output is correct |
37 |
Correct |
104 ms |
28456 KB |
Output is correct |
38 |
Correct |
155 ms |
28464 KB |
Output is correct |
39 |
Correct |
163 ms |
28372 KB |
Output is correct |
40 |
Correct |
126 ms |
28460 KB |
Output is correct |
41 |
Correct |
89 ms |
17192 KB |
Output is correct |
42 |
Correct |
98 ms |
17188 KB |
Output is correct |
43 |
Correct |
117 ms |
28460 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
8 ms |
852 KB |
Output is correct |
50 |
Correct |
14 ms |
880 KB |
Output is correct |
51 |
Correct |
23 ms |
880 KB |
Output is correct |
52 |
Correct |
13 ms |
852 KB |
Output is correct |
53 |
Correct |
79 ms |
28468 KB |
Output is correct |
54 |
Correct |
165 ms |
28456 KB |
Output is correct |
55 |
Correct |
194 ms |
28456 KB |
Output is correct |
56 |
Correct |
159 ms |
28460 KB |
Output is correct |
57 |
Correct |
291 ms |
28520 KB |
Output is correct |
58 |
Correct |
98 ms |
28480 KB |
Output is correct |
59 |
Correct |
171 ms |
28464 KB |
Output is correct |