#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<
// a doua zi dupa 11 ore de somn merge mai bine garantez eu
// ah.. sau poate ca nu.. sau?
// omfg trebe sa invat sa codez ca nu i ok asa sincer
using namespace std;
const int INF = (int)1e9 + 5;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int k, n, m, q; cin >> k >> n >> m >> q;
int groups = (n + k - 1) / k;
int log_groups = 32 - __builtin_clz(groups);
vector<vector<vector<int>>> dist(log_groups,
vector<vector<int>>(n,
vector<int>(k, INF)));
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < m; ++i) {
int a, b, c; cin >> a >> b >> c;
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
dist[0][a][b % k] = c;
}
for (int p = 1; p < log_groups; ++p) {
for (int from = 0; from / k + (1 << p) <= (n - 1) / k; ++from) {
for (int to = 0; to < k; ++to) {
int gr = from / k + (1 << (p - 1));
int lst = min((gr + 1) * k, n);
for (int x = gr * k, r = 0; x < lst; ++x, ++r) {
dist[p][from][to] = min(dist[p][from][to],
dist[p - 1][from][r] + dist[p - 1][x][to]);
}
}
}
}
while (q--) {
int a, b; cin >> a >> b;
vector<int> d(k, INF);
d[a % k] = 0;
a = a / k;
int len = b / k - a;
for (int bit = 0; (1 << bit) <= len; ++bit) {
if (((1 << bit) & len) == 0) continue;
vector<int> nd(k, INF);
for (int from = a * k; from < (a + 1) * k; ++from) {
for (int x = 0; x < k; ++x) {
nd[x] = min(nd[x], d[from - a * k] + dist[bit][from][x]);
}
}
a += 1 << bit;
d = move(nd);
}
int ans = d[b % k];
if (ans == INF) ans = -1;
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
47980 KB |
Output is correct |
2 |
Correct |
2 ms |
368 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
4 ms |
1012 KB |
Output is correct |
6 |
Correct |
5 ms |
1032 KB |
Output is correct |
7 |
Correct |
3 ms |
988 KB |
Output is correct |
8 |
Correct |
153 ms |
47916 KB |
Output is correct |
9 |
Correct |
161 ms |
47780 KB |
Output is correct |
10 |
Correct |
118 ms |
46980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
46364 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
9 ms |
1016 KB |
Output is correct |
8 |
Correct |
10 ms |
1056 KB |
Output is correct |
9 |
Correct |
143 ms |
47828 KB |
Output is correct |
10 |
Correct |
240 ms |
48072 KB |
Output is correct |
11 |
Correct |
197 ms |
46440 KB |
Output is correct |
12 |
Correct |
190 ms |
46244 KB |
Output is correct |
13 |
Correct |
195 ms |
28216 KB |
Output is correct |
14 |
Correct |
149 ms |
27892 KB |
Output is correct |
15 |
Correct |
140 ms |
25796 KB |
Output is correct |
16 |
Correct |
144 ms |
25828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
4 ms |
1016 KB |
Output is correct |
7 |
Correct |
4 ms |
1032 KB |
Output is correct |
8 |
Correct |
5 ms |
1020 KB |
Output is correct |
9 |
Correct |
5 ms |
912 KB |
Output is correct |
10 |
Correct |
143 ms |
47780 KB |
Output is correct |
11 |
Correct |
177 ms |
46376 KB |
Output is correct |
12 |
Correct |
234 ms |
47900 KB |
Output is correct |
13 |
Correct |
242 ms |
47568 KB |
Output is correct |
14 |
Correct |
208 ms |
46736 KB |
Output is correct |
15 |
Correct |
133 ms |
25164 KB |
Output is correct |
16 |
Correct |
137 ms |
25132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
4 ms |
1016 KB |
Output is correct |
7 |
Correct |
4 ms |
1032 KB |
Output is correct |
8 |
Correct |
5 ms |
1020 KB |
Output is correct |
9 |
Correct |
5 ms |
912 KB |
Output is correct |
10 |
Correct |
143 ms |
47780 KB |
Output is correct |
11 |
Correct |
177 ms |
46376 KB |
Output is correct |
12 |
Correct |
234 ms |
47900 KB |
Output is correct |
13 |
Correct |
242 ms |
47568 KB |
Output is correct |
14 |
Correct |
208 ms |
46736 KB |
Output is correct |
15 |
Correct |
133 ms |
25164 KB |
Output is correct |
16 |
Correct |
137 ms |
25132 KB |
Output is correct |
17 |
Correct |
186 ms |
45800 KB |
Output is correct |
18 |
Correct |
2 ms |
276 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
0 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
6 ms |
988 KB |
Output is correct |
24 |
Correct |
7 ms |
988 KB |
Output is correct |
25 |
Correct |
7 ms |
1016 KB |
Output is correct |
26 |
Correct |
9 ms |
1064 KB |
Output is correct |
27 |
Correct |
142 ms |
47276 KB |
Output is correct |
28 |
Correct |
239 ms |
47384 KB |
Output is correct |
29 |
Correct |
244 ms |
47556 KB |
Output is correct |
30 |
Correct |
213 ms |
46412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
47980 KB |
Output is correct |
2 |
Correct |
2 ms |
368 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
4 ms |
1012 KB |
Output is correct |
6 |
Correct |
5 ms |
1032 KB |
Output is correct |
7 |
Correct |
3 ms |
988 KB |
Output is correct |
8 |
Correct |
153 ms |
47916 KB |
Output is correct |
9 |
Correct |
161 ms |
47780 KB |
Output is correct |
10 |
Correct |
118 ms |
46980 KB |
Output is correct |
11 |
Correct |
195 ms |
46364 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
1 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
9 ms |
1016 KB |
Output is correct |
18 |
Correct |
10 ms |
1056 KB |
Output is correct |
19 |
Correct |
143 ms |
47828 KB |
Output is correct |
20 |
Correct |
240 ms |
48072 KB |
Output is correct |
21 |
Correct |
197 ms |
46440 KB |
Output is correct |
22 |
Correct |
190 ms |
46244 KB |
Output is correct |
23 |
Correct |
195 ms |
28216 KB |
Output is correct |
24 |
Correct |
149 ms |
27892 KB |
Output is correct |
25 |
Correct |
140 ms |
25796 KB |
Output is correct |
26 |
Correct |
144 ms |
25828 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
412 KB |
Output is correct |
30 |
Correct |
2 ms |
380 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
4 ms |
1016 KB |
Output is correct |
33 |
Correct |
4 ms |
1032 KB |
Output is correct |
34 |
Correct |
5 ms |
1020 KB |
Output is correct |
35 |
Correct |
5 ms |
912 KB |
Output is correct |
36 |
Correct |
143 ms |
47780 KB |
Output is correct |
37 |
Correct |
177 ms |
46376 KB |
Output is correct |
38 |
Correct |
234 ms |
47900 KB |
Output is correct |
39 |
Correct |
242 ms |
47568 KB |
Output is correct |
40 |
Correct |
208 ms |
46736 KB |
Output is correct |
41 |
Correct |
133 ms |
25164 KB |
Output is correct |
42 |
Correct |
137 ms |
25132 KB |
Output is correct |
43 |
Correct |
186 ms |
45800 KB |
Output is correct |
44 |
Correct |
2 ms |
276 KB |
Output is correct |
45 |
Correct |
2 ms |
376 KB |
Output is correct |
46 |
Correct |
0 ms |
376 KB |
Output is correct |
47 |
Correct |
2 ms |
460 KB |
Output is correct |
48 |
Correct |
2 ms |
376 KB |
Output is correct |
49 |
Correct |
6 ms |
988 KB |
Output is correct |
50 |
Correct |
7 ms |
988 KB |
Output is correct |
51 |
Correct |
7 ms |
1016 KB |
Output is correct |
52 |
Correct |
9 ms |
1064 KB |
Output is correct |
53 |
Correct |
142 ms |
47276 KB |
Output is correct |
54 |
Correct |
239 ms |
47384 KB |
Output is correct |
55 |
Correct |
244 ms |
47556 KB |
Output is correct |
56 |
Correct |
213 ms |
46412 KB |
Output is correct |
57 |
Correct |
305 ms |
44416 KB |
Output is correct |
58 |
Correct |
165 ms |
47016 KB |
Output is correct |
59 |
Correct |
204 ms |
45416 KB |
Output is correct |