#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
vector<vector<int>> combine(vector<vector<int>> &a, vector<vector<int>> &b){
int k = a.size();
vector<vector<int>> res(k, vector<int>(k, INF));
for (int i = 0; i < k; i++){
for (int j = 0; j < k; j++){
for (int z = 0; z < k; z++){
res[i][j] = min(res[i][j], a[i][z] + b[z][j]);
}
}
}
return res;
}
void Solve()
{
int k, n, m, q; cin >> k >> n >> m >> q;
vector<vector<vector<vector<int>>>> dp((n/k) + 1, vector<vector<vector<int>>>(18, vector<vector<int>>(k, vector<int>(k, INF))));
for (int i = 1; i <= m; i++){
int a, b, t; cin >> a >> b >> t;
dp[a/k][0][a % k][b % k] = t;
}
for (int j = 1; j < 18; j++){
for (int i = 0; i <= n/k; i++){
//combine dp[i][j - 1] and dp[i + (1 << (j - 1))][j - 1]
int x = i + (1 << (j - 1));
if (x <= (n/k))
dp[i][j] = combine(dp[i][j - 1], dp[x][j - 1]);
}
}
while (q--){
int a, b; cin >> a >> b;
int x = a / k;
int y = b / k;
int diff = y - x;
//lift diff times
vector<vector<int>> res(k, vector<int>(k, INF));
res[a % k][a % k] = 0;
for (int i = 17; i >= 0; i--){
if (diff >> i & 1){
res = combine(res, dp[x][i]);
x += 1 << i;
}
}
int ans = INF;
for (int i = 0; i < k; i++){
ans = min(ans, res[i][b % k]);
}
if (ans == INF) ans = -1;
cout << ans << "\n";
}
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
79976 KB |
Output is correct |
2 |
Correct |
1 ms |
320 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 |
1876 KB |
Output is correct |
6 |
Correct |
3 ms |
1868 KB |
Output is correct |
7 |
Correct |
3 ms |
1876 KB |
Output is correct |
8 |
Correct |
214 ms |
80716 KB |
Output is correct |
9 |
Correct |
195 ms |
80632 KB |
Output is correct |
10 |
Correct |
174 ms |
79900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
68360 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
8 ms |
2004 KB |
Output is correct |
8 |
Correct |
9 ms |
1748 KB |
Output is correct |
9 |
Correct |
177 ms |
80672 KB |
Output is correct |
10 |
Correct |
185 ms |
62020 KB |
Output is correct |
11 |
Correct |
156 ms |
69988 KB |
Output is correct |
12 |
Correct |
158 ms |
60948 KB |
Output is correct |
13 |
Correct |
105 ms |
44280 KB |
Output is correct |
14 |
Correct |
83 ms |
37324 KB |
Output is correct |
15 |
Correct |
83 ms |
43184 KB |
Output is correct |
16 |
Correct |
83 ms |
43172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1620 KB |
Output is correct |
8 |
Correct |
3 ms |
1748 KB |
Output is correct |
9 |
Correct |
2 ms |
1744 KB |
Output is correct |
10 |
Correct |
181 ms |
80492 KB |
Output is correct |
11 |
Correct |
151 ms |
69700 KB |
Output is correct |
12 |
Correct |
149 ms |
61736 KB |
Output is correct |
13 |
Correct |
149 ms |
61980 KB |
Output is correct |
14 |
Correct |
146 ms |
61464 KB |
Output is correct |
15 |
Correct |
78 ms |
43144 KB |
Output is correct |
16 |
Correct |
87 ms |
43144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1620 KB |
Output is correct |
8 |
Correct |
3 ms |
1748 KB |
Output is correct |
9 |
Correct |
2 ms |
1744 KB |
Output is correct |
10 |
Correct |
181 ms |
80492 KB |
Output is correct |
11 |
Correct |
151 ms |
69700 KB |
Output is correct |
12 |
Correct |
149 ms |
61736 KB |
Output is correct |
13 |
Correct |
149 ms |
61980 KB |
Output is correct |
14 |
Correct |
146 ms |
61464 KB |
Output is correct |
15 |
Correct |
78 ms |
43144 KB |
Output is correct |
16 |
Correct |
87 ms |
43144 KB |
Output is correct |
17 |
Correct |
148 ms |
69748 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
4 ms |
1864 KB |
Output is correct |
24 |
Correct |
5 ms |
1732 KB |
Output is correct |
25 |
Correct |
6 ms |
1748 KB |
Output is correct |
26 |
Correct |
5 ms |
1880 KB |
Output is correct |
27 |
Correct |
216 ms |
80608 KB |
Output is correct |
28 |
Correct |
158 ms |
61852 KB |
Output is correct |
29 |
Correct |
173 ms |
62248 KB |
Output is correct |
30 |
Correct |
149 ms |
61516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
79976 KB |
Output is correct |
2 |
Correct |
1 ms |
320 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 |
1876 KB |
Output is correct |
6 |
Correct |
3 ms |
1868 KB |
Output is correct |
7 |
Correct |
3 ms |
1876 KB |
Output is correct |
8 |
Correct |
214 ms |
80716 KB |
Output is correct |
9 |
Correct |
195 ms |
80632 KB |
Output is correct |
10 |
Correct |
174 ms |
79900 KB |
Output is correct |
11 |
Correct |
166 ms |
68360 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
8 ms |
2004 KB |
Output is correct |
18 |
Correct |
9 ms |
1748 KB |
Output is correct |
19 |
Correct |
177 ms |
80672 KB |
Output is correct |
20 |
Correct |
185 ms |
62020 KB |
Output is correct |
21 |
Correct |
156 ms |
69988 KB |
Output is correct |
22 |
Correct |
158 ms |
60948 KB |
Output is correct |
23 |
Correct |
105 ms |
44280 KB |
Output is correct |
24 |
Correct |
83 ms |
37324 KB |
Output is correct |
25 |
Correct |
83 ms |
43184 KB |
Output is correct |
26 |
Correct |
83 ms |
43172 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
1876 KB |
Output is correct |
33 |
Correct |
2 ms |
1620 KB |
Output is correct |
34 |
Correct |
3 ms |
1748 KB |
Output is correct |
35 |
Correct |
2 ms |
1744 KB |
Output is correct |
36 |
Correct |
181 ms |
80492 KB |
Output is correct |
37 |
Correct |
151 ms |
69700 KB |
Output is correct |
38 |
Correct |
149 ms |
61736 KB |
Output is correct |
39 |
Correct |
149 ms |
61980 KB |
Output is correct |
40 |
Correct |
146 ms |
61464 KB |
Output is correct |
41 |
Correct |
78 ms |
43144 KB |
Output is correct |
42 |
Correct |
87 ms |
43144 KB |
Output is correct |
43 |
Correct |
148 ms |
69748 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
324 KB |
Output is correct |
49 |
Correct |
4 ms |
1864 KB |
Output is correct |
50 |
Correct |
5 ms |
1732 KB |
Output is correct |
51 |
Correct |
6 ms |
1748 KB |
Output is correct |
52 |
Correct |
5 ms |
1880 KB |
Output is correct |
53 |
Correct |
216 ms |
80608 KB |
Output is correct |
54 |
Correct |
158 ms |
61852 KB |
Output is correct |
55 |
Correct |
173 ms |
62248 KB |
Output is correct |
56 |
Correct |
149 ms |
61516 KB |
Output is correct |
57 |
Correct |
192 ms |
76332 KB |
Output is correct |
58 |
Correct |
195 ms |
80700 KB |
Output is correct |
59 |
Correct |
170 ms |
69928 KB |
Output is correct |