#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
int k, n, m, o;
vector<vector<vii>> dp(50000, vector<vii>(17, vii(5, vi(5, inf))));
void combine(vii &ans, vii &curr, vii &dp) {
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
for (int z = 0; z < k; z++) {
ans[x][y] = min(ans[x][y], curr[x][z] + dp[z][y]);
}
}
}
}
void sol() {
cin >> k >> n >> m >> o;
for (int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c;
dp[a/k][0][a%k][b%k] = c;
}
for (int j = 1; j <= 16; j++) {
for (int i = 0; i < n; i++) {
if (i + (1<<j) >= n) break;
combine(dp[i][j], dp[i][j-1], dp[i+(1<<j-1)][j-1]);
}
}
while (o--) {
int a, b; cin >> a >> b;
if (a > b) {cout << -1 << endl; continue;}
vii ans(5, vi(5, inf));
for (int i = 0; i < k; i++) ans[i][i] = 0;
int dis = b/k - a/k, aa = a/k;
for (int i = 0; i <= 16; i++) {
if (dis&(1<<i)) {
vii curr(5, vi(5, inf));
combine(curr, ans, dp[aa][i]);
ans = curr;
aa += (1<<i);
}
}
cout << (ans[a%k][b%k] >= inf ? -1 : ans[a%k][b%k]) << endl;
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
Compilation message
toll.cpp: In function 'void sol()':
toll.cpp:59:53: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
59 | combine(dp[i][j], dp[i][j-1], dp[i+(1<<j-1)][j-1]);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
405 ms |
262296 KB |
Output is correct |
2 |
Correct |
245 ms |
261328 KB |
Output is correct |
3 |
Correct |
252 ms |
261372 KB |
Output is correct |
4 |
Correct |
253 ms |
261480 KB |
Output is correct |
5 |
Correct |
239 ms |
261284 KB |
Output is correct |
6 |
Correct |
242 ms |
261368 KB |
Output is correct |
7 |
Correct |
265 ms |
261392 KB |
Output is correct |
8 |
Correct |
342 ms |
262260 KB |
Output is correct |
9 |
Correct |
384 ms |
262280 KB |
Output is correct |
10 |
Correct |
328 ms |
261432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
403 ms |
262964 KB |
Output is correct |
2 |
Correct |
243 ms |
261292 KB |
Output is correct |
3 |
Correct |
240 ms |
261268 KB |
Output is correct |
4 |
Correct |
246 ms |
261396 KB |
Output is correct |
5 |
Correct |
240 ms |
261256 KB |
Output is correct |
6 |
Correct |
239 ms |
261248 KB |
Output is correct |
7 |
Correct |
255 ms |
261548 KB |
Output is correct |
8 |
Correct |
266 ms |
261516 KB |
Output is correct |
9 |
Correct |
329 ms |
262476 KB |
Output is correct |
10 |
Correct |
429 ms |
263656 KB |
Output is correct |
11 |
Correct |
402 ms |
263068 KB |
Output is correct |
12 |
Correct |
418 ms |
262664 KB |
Output is correct |
13 |
Correct |
433 ms |
263760 KB |
Output is correct |
14 |
Correct |
361 ms |
262820 KB |
Output is correct |
15 |
Correct |
398 ms |
262584 KB |
Output is correct |
16 |
Correct |
406 ms |
262592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
261352 KB |
Output is correct |
2 |
Correct |
240 ms |
261356 KB |
Output is correct |
3 |
Correct |
241 ms |
261300 KB |
Output is correct |
4 |
Correct |
250 ms |
261268 KB |
Output is correct |
5 |
Correct |
258 ms |
261268 KB |
Output is correct |
6 |
Correct |
242 ms |
261484 KB |
Output is correct |
7 |
Correct |
241 ms |
261292 KB |
Output is correct |
8 |
Correct |
243 ms |
261316 KB |
Output is correct |
9 |
Correct |
247 ms |
261384 KB |
Output is correct |
10 |
Correct |
313 ms |
262176 KB |
Output is correct |
11 |
Correct |
372 ms |
262900 KB |
Output is correct |
12 |
Correct |
409 ms |
263500 KB |
Output is correct |
13 |
Correct |
428 ms |
263792 KB |
Output is correct |
14 |
Correct |
406 ms |
263068 KB |
Output is correct |
15 |
Correct |
392 ms |
262496 KB |
Output is correct |
16 |
Correct |
382 ms |
262564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
261352 KB |
Output is correct |
2 |
Correct |
240 ms |
261356 KB |
Output is correct |
3 |
Correct |
241 ms |
261300 KB |
Output is correct |
4 |
Correct |
250 ms |
261268 KB |
Output is correct |
5 |
Correct |
258 ms |
261268 KB |
Output is correct |
6 |
Correct |
242 ms |
261484 KB |
Output is correct |
7 |
Correct |
241 ms |
261292 KB |
Output is correct |
8 |
Correct |
243 ms |
261316 KB |
Output is correct |
9 |
Correct |
247 ms |
261384 KB |
Output is correct |
10 |
Correct |
313 ms |
262176 KB |
Output is correct |
11 |
Correct |
372 ms |
262900 KB |
Output is correct |
12 |
Correct |
409 ms |
263500 KB |
Output is correct |
13 |
Correct |
428 ms |
263792 KB |
Output is correct |
14 |
Correct |
406 ms |
263068 KB |
Output is correct |
15 |
Correct |
392 ms |
262496 KB |
Output is correct |
16 |
Correct |
382 ms |
262564 KB |
Output is correct |
17 |
Correct |
395 ms |
262896 KB |
Output is correct |
18 |
Correct |
238 ms |
261324 KB |
Output is correct |
19 |
Correct |
260 ms |
261308 KB |
Output is correct |
20 |
Correct |
244 ms |
261308 KB |
Output is correct |
21 |
Correct |
238 ms |
261340 KB |
Output is correct |
22 |
Correct |
259 ms |
261400 KB |
Output is correct |
23 |
Correct |
242 ms |
261372 KB |
Output is correct |
24 |
Correct |
244 ms |
261412 KB |
Output is correct |
25 |
Correct |
263 ms |
261432 KB |
Output is correct |
26 |
Correct |
257 ms |
261404 KB |
Output is correct |
27 |
Correct |
318 ms |
262180 KB |
Output is correct |
28 |
Correct |
418 ms |
263500 KB |
Output is correct |
29 |
Correct |
441 ms |
263828 KB |
Output is correct |
30 |
Correct |
413 ms |
263224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
405 ms |
262296 KB |
Output is correct |
2 |
Correct |
245 ms |
261328 KB |
Output is correct |
3 |
Correct |
252 ms |
261372 KB |
Output is correct |
4 |
Correct |
253 ms |
261480 KB |
Output is correct |
5 |
Correct |
239 ms |
261284 KB |
Output is correct |
6 |
Correct |
242 ms |
261368 KB |
Output is correct |
7 |
Correct |
265 ms |
261392 KB |
Output is correct |
8 |
Correct |
342 ms |
262260 KB |
Output is correct |
9 |
Correct |
384 ms |
262280 KB |
Output is correct |
10 |
Correct |
328 ms |
261432 KB |
Output is correct |
11 |
Correct |
403 ms |
262964 KB |
Output is correct |
12 |
Correct |
243 ms |
261292 KB |
Output is correct |
13 |
Correct |
240 ms |
261268 KB |
Output is correct |
14 |
Correct |
246 ms |
261396 KB |
Output is correct |
15 |
Correct |
240 ms |
261256 KB |
Output is correct |
16 |
Correct |
239 ms |
261248 KB |
Output is correct |
17 |
Correct |
255 ms |
261548 KB |
Output is correct |
18 |
Correct |
266 ms |
261516 KB |
Output is correct |
19 |
Correct |
329 ms |
262476 KB |
Output is correct |
20 |
Correct |
429 ms |
263656 KB |
Output is correct |
21 |
Correct |
402 ms |
263068 KB |
Output is correct |
22 |
Correct |
418 ms |
262664 KB |
Output is correct |
23 |
Correct |
433 ms |
263760 KB |
Output is correct |
24 |
Correct |
361 ms |
262820 KB |
Output is correct |
25 |
Correct |
398 ms |
262584 KB |
Output is correct |
26 |
Correct |
406 ms |
262592 KB |
Output is correct |
27 |
Correct |
268 ms |
261352 KB |
Output is correct |
28 |
Correct |
240 ms |
261356 KB |
Output is correct |
29 |
Correct |
241 ms |
261300 KB |
Output is correct |
30 |
Correct |
250 ms |
261268 KB |
Output is correct |
31 |
Correct |
258 ms |
261268 KB |
Output is correct |
32 |
Correct |
242 ms |
261484 KB |
Output is correct |
33 |
Correct |
241 ms |
261292 KB |
Output is correct |
34 |
Correct |
243 ms |
261316 KB |
Output is correct |
35 |
Correct |
247 ms |
261384 KB |
Output is correct |
36 |
Correct |
313 ms |
262176 KB |
Output is correct |
37 |
Correct |
372 ms |
262900 KB |
Output is correct |
38 |
Correct |
409 ms |
263500 KB |
Output is correct |
39 |
Correct |
428 ms |
263792 KB |
Output is correct |
40 |
Correct |
406 ms |
263068 KB |
Output is correct |
41 |
Correct |
392 ms |
262496 KB |
Output is correct |
42 |
Correct |
382 ms |
262564 KB |
Output is correct |
43 |
Correct |
395 ms |
262896 KB |
Output is correct |
44 |
Correct |
238 ms |
261324 KB |
Output is correct |
45 |
Correct |
260 ms |
261308 KB |
Output is correct |
46 |
Correct |
244 ms |
261308 KB |
Output is correct |
47 |
Correct |
238 ms |
261340 KB |
Output is correct |
48 |
Correct |
259 ms |
261400 KB |
Output is correct |
49 |
Correct |
242 ms |
261372 KB |
Output is correct |
50 |
Correct |
244 ms |
261412 KB |
Output is correct |
51 |
Correct |
263 ms |
261432 KB |
Output is correct |
52 |
Correct |
257 ms |
261404 KB |
Output is correct |
53 |
Correct |
318 ms |
262180 KB |
Output is correct |
54 |
Correct |
418 ms |
263500 KB |
Output is correct |
55 |
Correct |
441 ms |
263828 KB |
Output is correct |
56 |
Correct |
413 ms |
263224 KB |
Output is correct |
57 |
Correct |
513 ms |
264796 KB |
Output is correct |
58 |
Correct |
344 ms |
262224 KB |
Output is correct |
59 |
Correct |
419 ms |
263116 KB |
Output is correct |