#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T>
bool chmax(T &val, T nv) { return val < nv ? (val = nv, true) : false; }
template<class T>
bool chmin(T &val, T nv) { return nv < val ? (val = nv, true) : false; }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T1, class ...T2>
void kout (T1 v, T2 ...e) { cerr << v << ' ', kout(e...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 50010, inf = 1e9, MAX_K = 5, MAX_L = 16;
int k, n, m, q;
int dp[MAX_L][MAX_N][MAX_K][MAX_K];
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> k >> n >> m >> q;
for (int i = 0;i < MAX_L;++i) for (int j = 0;j < n;++j) for (int l = 0;l < k;++l) for (int m = 0;m < k;++m)
dp[i][j][l][m] = inf;
for (int a, b, t, i = 0;i < m;++i) {
cin >> a >> b >> t;
dp[0][a / k][a % k][b % k] = t;
}
for (int d = 1;1 << d <= n;++d) {
for (int i = 0;i < n;++i)
for (int j = 0;j < k;++j) for (int l = 0;l < k;++l)
for (int m = 0;m < k;++m)
chmin(dp[d][i][j][m], dp[d-1][i][j][l] + dp[d-1][i + (1<<(d-1))][l][m]);
}
auto qry = [&](int a, int b) {
if (a / k == b / k) return -1;
int dis = b / k - a / k;
int *mat = new int[k], *tmp = new int[k];
for (int i = 0;i < k;++i) mat[i] = inf;
mat[a % k] = 0;
for (int d = 0;1 << d <= dis;++d) if (dis >> d & 1) {
DE(d);
fill(tmp, tmp + k, inf);
for (int i = 0;i < k;++i) for (int j = 0;j < k;++j) {
chmin(tmp[j], mat[i] + dp[d][a / k][i][j]);
}
a += (1 << d) * k;
swap(tmp, mat);
}
return mat[b % k] == inf ? -1 : mat[b % k];
};
while (q--) {
int a, b;
cin >> a >> b;
cout << qry(a, b) << '\n';
}
}
Compilation message
toll.cpp: In lambda function:
toll.cpp:18:17: warning: statement has no effect [-Wunused-value]
18 | #define DE(...) 0
| ^
toll.cpp:54:4: note: in expansion of macro 'DE'
54 | DE(d);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
79724 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
2156 KB |
Output is correct |
6 |
Correct |
2 ms |
2156 KB |
Output is correct |
7 |
Correct |
2 ms |
2156 KB |
Output is correct |
8 |
Correct |
79 ms |
80236 KB |
Output is correct |
9 |
Correct |
73 ms |
80236 KB |
Output is correct |
10 |
Correct |
57 ms |
79468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
79724 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
6 ms |
2796 KB |
Output is correct |
8 |
Correct |
7 ms |
2796 KB |
Output is correct |
9 |
Correct |
73 ms |
80236 KB |
Output is correct |
10 |
Correct |
134 ms |
81700 KB |
Output is correct |
11 |
Correct |
94 ms |
81004 KB |
Output is correct |
12 |
Correct |
115 ms |
80620 KB |
Output is correct |
13 |
Correct |
199 ms |
50028 KB |
Output is correct |
14 |
Correct |
81 ms |
49152 KB |
Output is correct |
15 |
Correct |
181 ms |
48876 KB |
Output is correct |
16 |
Correct |
184 ms |
48876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
2028 KB |
Output is correct |
7 |
Correct |
2 ms |
2028 KB |
Output is correct |
8 |
Correct |
6 ms |
2156 KB |
Output is correct |
9 |
Correct |
4 ms |
2028 KB |
Output is correct |
10 |
Correct |
65 ms |
79468 KB |
Output is correct |
11 |
Correct |
87 ms |
80236 KB |
Output is correct |
12 |
Correct |
129 ms |
80876 KB |
Output is correct |
13 |
Correct |
130 ms |
81004 KB |
Output is correct |
14 |
Correct |
119 ms |
80492 KB |
Output is correct |
15 |
Correct |
178 ms |
48620 KB |
Output is correct |
16 |
Correct |
185 ms |
48748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
2028 KB |
Output is correct |
7 |
Correct |
2 ms |
2028 KB |
Output is correct |
8 |
Correct |
6 ms |
2156 KB |
Output is correct |
9 |
Correct |
4 ms |
2028 KB |
Output is correct |
10 |
Correct |
65 ms |
79468 KB |
Output is correct |
11 |
Correct |
87 ms |
80236 KB |
Output is correct |
12 |
Correct |
129 ms |
80876 KB |
Output is correct |
13 |
Correct |
130 ms |
81004 KB |
Output is correct |
14 |
Correct |
119 ms |
80492 KB |
Output is correct |
15 |
Correct |
178 ms |
48620 KB |
Output is correct |
16 |
Correct |
185 ms |
48748 KB |
Output is correct |
17 |
Correct |
90 ms |
80364 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
3 ms |
2284 KB |
Output is correct |
24 |
Correct |
4 ms |
2284 KB |
Output is correct |
25 |
Correct |
8 ms |
2284 KB |
Output is correct |
26 |
Correct |
6 ms |
2284 KB |
Output is correct |
27 |
Correct |
69 ms |
79724 KB |
Output is correct |
28 |
Correct |
129 ms |
81036 KB |
Output is correct |
29 |
Correct |
138 ms |
81260 KB |
Output is correct |
30 |
Correct |
126 ms |
80748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
79724 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
2156 KB |
Output is correct |
6 |
Correct |
2 ms |
2156 KB |
Output is correct |
7 |
Correct |
2 ms |
2156 KB |
Output is correct |
8 |
Correct |
79 ms |
80236 KB |
Output is correct |
9 |
Correct |
73 ms |
80236 KB |
Output is correct |
10 |
Correct |
57 ms |
79468 KB |
Output is correct |
11 |
Correct |
97 ms |
79724 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
6 ms |
2796 KB |
Output is correct |
18 |
Correct |
7 ms |
2796 KB |
Output is correct |
19 |
Correct |
73 ms |
80236 KB |
Output is correct |
20 |
Correct |
134 ms |
81700 KB |
Output is correct |
21 |
Correct |
94 ms |
81004 KB |
Output is correct |
22 |
Correct |
115 ms |
80620 KB |
Output is correct |
23 |
Correct |
199 ms |
50028 KB |
Output is correct |
24 |
Correct |
81 ms |
49152 KB |
Output is correct |
25 |
Correct |
181 ms |
48876 KB |
Output is correct |
26 |
Correct |
184 ms |
48876 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
2 ms |
2028 KB |
Output is correct |
33 |
Correct |
2 ms |
2028 KB |
Output is correct |
34 |
Correct |
6 ms |
2156 KB |
Output is correct |
35 |
Correct |
4 ms |
2028 KB |
Output is correct |
36 |
Correct |
65 ms |
79468 KB |
Output is correct |
37 |
Correct |
87 ms |
80236 KB |
Output is correct |
38 |
Correct |
129 ms |
80876 KB |
Output is correct |
39 |
Correct |
130 ms |
81004 KB |
Output is correct |
40 |
Correct |
119 ms |
80492 KB |
Output is correct |
41 |
Correct |
178 ms |
48620 KB |
Output is correct |
42 |
Correct |
185 ms |
48748 KB |
Output is correct |
43 |
Correct |
90 ms |
80364 KB |
Output is correct |
44 |
Correct |
1 ms |
492 KB |
Output is correct |
45 |
Correct |
1 ms |
492 KB |
Output is correct |
46 |
Correct |
1 ms |
492 KB |
Output is correct |
47 |
Correct |
1 ms |
492 KB |
Output is correct |
48 |
Correct |
1 ms |
492 KB |
Output is correct |
49 |
Correct |
3 ms |
2284 KB |
Output is correct |
50 |
Correct |
4 ms |
2284 KB |
Output is correct |
51 |
Correct |
8 ms |
2284 KB |
Output is correct |
52 |
Correct |
6 ms |
2284 KB |
Output is correct |
53 |
Correct |
69 ms |
79724 KB |
Output is correct |
54 |
Correct |
129 ms |
81036 KB |
Output is correct |
55 |
Correct |
138 ms |
81260 KB |
Output is correct |
56 |
Correct |
126 ms |
80748 KB |
Output is correct |
57 |
Correct |
232 ms |
82668 KB |
Output is correct |
58 |
Correct |
77 ms |
80256 KB |
Output is correct |
59 |
Correct |
99 ms |
81132 KB |
Output is correct |