#include <bits/stdc++.h>
using namespace std;
#define int long long int
vector<vector<int>> calc(vector<vector<int>> a, vector<vector<int>> b, int k) {
vector<vector<int>> ans(k, vector<int>(k, INT_MAX));
for (int x = 0; x < k; x++) {
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
ans[i][j] = min(ans[i][j], a[i][x]+b[x][j]);
}
}
}
return ans;
}
void solve() { // I dont really like this problem
int n, o, m, k;
cin >> k >> n >> m >> o;
vector<vector<vector<vector<int>>>> dp(n+1, vector<vector<vector<int>>>(17, vector<vector<int>>(k, vector<int>(k, INT_MAX))));
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
dp[u/k][0][u%k][v%k] = w;
}
for (int j = 1; j < 17; j++) {
for (int i = 0; i < (n+k-1)/k; i++) {
if (i+(1<<j) >= (n+k-1)/k) {
break;
}
dp[i][j] = calc(dp[i][j-1], dp[i+(1<<(j-1))][j-1], k);
}
}
while(o--) {
int a, b;
cin >> a >> b;
vector<vector<int>> ans(k, vector<int>(k, INT_MAX));
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
ans[i][i] = 0;
}
}
int i = a/k;
for (int j = 16; j >= 0; j--) {
if (i+(1<<j) <= b/k) {
ans = calc(ans, dp[i][j], k);
i += (1<<j);
}
}
if (ans[a%k][b%k] == INT_MAX) {
cout << -1 << endl;
} else {
cout << ans[a%k][b%k] << endl;
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
t = 1;
while(t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
75092 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
3 ms |
1884 KB |
Output is correct |
7 |
Correct |
4 ms |
1884 KB |
Output is correct |
8 |
Correct |
236 ms |
76004 KB |
Output is correct |
9 |
Correct |
212 ms |
76112 KB |
Output is correct |
10 |
Correct |
207 ms |
75344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
128336 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 |
17 ms |
2048 KB |
Output is correct |
8 |
Correct |
19 ms |
3160 KB |
Output is correct |
9 |
Correct |
198 ms |
76096 KB |
Output is correct |
10 |
Correct |
243 ms |
170616 KB |
Output is correct |
11 |
Correct |
231 ms |
130128 KB |
Output is correct |
12 |
Correct |
227 ms |
169552 KB |
Output is correct |
13 |
Correct |
193 ms |
199252 KB |
Output is correct |
14 |
Correct |
123 ms |
102480 KB |
Output is correct |
15 |
Correct |
186 ms |
198036 KB |
Output is correct |
16 |
Correct |
174 ms |
198228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
2 ms |
1880 KB |
Output is correct |
7 |
Correct |
3 ms |
2908 KB |
Output is correct |
8 |
Correct |
5 ms |
7000 KB |
Output is correct |
9 |
Correct |
5 ms |
5916 KB |
Output is correct |
10 |
Correct |
174 ms |
75860 KB |
Output is correct |
11 |
Correct |
182 ms |
129876 KB |
Output is correct |
12 |
Correct |
203 ms |
170320 KB |
Output is correct |
13 |
Correct |
208 ms |
170580 KB |
Output is correct |
14 |
Correct |
196 ms |
170140 KB |
Output is correct |
15 |
Correct |
175 ms |
197960 KB |
Output is correct |
16 |
Correct |
160 ms |
198224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
2 ms |
1880 KB |
Output is correct |
7 |
Correct |
3 ms |
2908 KB |
Output is correct |
8 |
Correct |
5 ms |
7000 KB |
Output is correct |
9 |
Correct |
5 ms |
5916 KB |
Output is correct |
10 |
Correct |
174 ms |
75860 KB |
Output is correct |
11 |
Correct |
182 ms |
129876 KB |
Output is correct |
12 |
Correct |
203 ms |
170320 KB |
Output is correct |
13 |
Correct |
208 ms |
170580 KB |
Output is correct |
14 |
Correct |
196 ms |
170140 KB |
Output is correct |
15 |
Correct |
175 ms |
197960 KB |
Output is correct |
16 |
Correct |
160 ms |
198224 KB |
Output is correct |
17 |
Correct |
195 ms |
129888 KB |
Output is correct |
18 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
7 ms |
1988 KB |
Output is correct |
24 |
Correct |
8 ms |
2908 KB |
Output is correct |
25 |
Correct |
12 ms |
7104 KB |
Output is correct |
26 |
Correct |
10 ms |
5968 KB |
Output is correct |
27 |
Correct |
188 ms |
75860 KB |
Output is correct |
28 |
Correct |
220 ms |
170576 KB |
Output is correct |
29 |
Correct |
214 ms |
170772 KB |
Output is correct |
30 |
Correct |
209 ms |
170064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
75092 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
3 ms |
1884 KB |
Output is correct |
7 |
Correct |
4 ms |
1884 KB |
Output is correct |
8 |
Correct |
236 ms |
76004 KB |
Output is correct |
9 |
Correct |
212 ms |
76112 KB |
Output is correct |
10 |
Correct |
207 ms |
75344 KB |
Output is correct |
11 |
Correct |
225 ms |
128336 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
17 ms |
2048 KB |
Output is correct |
18 |
Correct |
19 ms |
3160 KB |
Output is correct |
19 |
Correct |
198 ms |
76096 KB |
Output is correct |
20 |
Correct |
243 ms |
170616 KB |
Output is correct |
21 |
Correct |
231 ms |
130128 KB |
Output is correct |
22 |
Correct |
227 ms |
169552 KB |
Output is correct |
23 |
Correct |
193 ms |
199252 KB |
Output is correct |
24 |
Correct |
123 ms |
102480 KB |
Output is correct |
25 |
Correct |
186 ms |
198036 KB |
Output is correct |
26 |
Correct |
174 ms |
198228 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
1880 KB |
Output is correct |
33 |
Correct |
3 ms |
2908 KB |
Output is correct |
34 |
Correct |
5 ms |
7000 KB |
Output is correct |
35 |
Correct |
5 ms |
5916 KB |
Output is correct |
36 |
Correct |
174 ms |
75860 KB |
Output is correct |
37 |
Correct |
182 ms |
129876 KB |
Output is correct |
38 |
Correct |
203 ms |
170320 KB |
Output is correct |
39 |
Correct |
208 ms |
170580 KB |
Output is correct |
40 |
Correct |
196 ms |
170140 KB |
Output is correct |
41 |
Correct |
175 ms |
197960 KB |
Output is correct |
42 |
Correct |
160 ms |
198224 KB |
Output is correct |
43 |
Correct |
195 ms |
129888 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
7 ms |
1988 KB |
Output is correct |
50 |
Correct |
8 ms |
2908 KB |
Output is correct |
51 |
Correct |
12 ms |
7104 KB |
Output is correct |
52 |
Correct |
10 ms |
5968 KB |
Output is correct |
53 |
Correct |
188 ms |
75860 KB |
Output is correct |
54 |
Correct |
220 ms |
170576 KB |
Output is correct |
55 |
Correct |
214 ms |
170772 KB |
Output is correct |
56 |
Correct |
209 ms |
170064 KB |
Output is correct |
57 |
Correct |
321 ms |
278100 KB |
Output is correct |
58 |
Correct |
229 ms |
76096 KB |
Output is correct |
59 |
Correct |
221 ms |
130036 KB |
Output is correct |