// spent some time confused b/c i thought that queries had to be between
// different blocks. but this is only edges, queries in the same block are
// valid. and of course the distance between u and u is 0
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
#ifdef LOCAL
#include "algo/debug.h"
#endif
#define f first
#define s second
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
// dist_to(src class in m, dest layer, dest layer class)
// dist_from(src layer, src class, dest class in m)
const int max_k = 5, max_n = 5e4;
int k, n, m, q, dist_to[max_k][max_n][max_k], dist_from[max_n][max_k][max_k];
const int max_q = 10000;
int ans[max_q];
array<int, 2> queries[max_q];
V<array<int, 2>> g[max_n][max_k];
void dnc(int l, int r, vi query_inds) {
if (l == r-1) {
for (int ind : query_inds) {
auto [l, r] = queries[ind];
if (l/k != r/k) while (true);
ans[ind] = l == r ? 0 : -1;
}
return;
}
int m = (l+r)/2;
{
for (int from = 0; from < k; from++) {
for (int i = m; i < r; i++) {
fill(all(dist_to[from][i]), inf);
}
}
for (int from = 0; from < k; from++) {
dist_to[from][m][from] = 0;
for (int i = m; i < r-1; i++) {
for (int now = 0; now < k; now++) {
for (auto [w, nxt] : g[i][now]) {
dist_to[from][i+1][nxt] = min(dist_to[from][i+1][nxt], dist_to[from][i][now]+w);
}
}
}
}
}
{
for (int src_layer = l; src_layer <= m; src_layer++) {
for (int src_class = 0; src_class < k; src_class++) {
fill(all(dist_from[src_layer][src_class]), inf);
}
}
for (int dest_class = 0; dest_class < k; dest_class++) {
dist_from[m][dest_class][dest_class] = 0;
for (int src_layer = m-1; src_layer >= l; src_layer--) {
for (int src_class = 0; src_class < k; src_class++) {
for (auto [w, nxt_class] : g[src_layer][src_class]) {
dist_from[src_layer][src_class][dest_class] = min(
dist_from[src_layer][src_class][dest_class],
w+dist_from[src_layer+1][nxt_class][dest_class]
);
}
}
}
}
}
vi todo[2];
for (int ind : query_inds) {
auto [l, r] = queries[ind];
if (l/k <= m && m <= r/k) {
int res = inf;
for (int m_class = 0; m_class < k; m_class++) {
res = min(res, dist_from[l/k][l%k][m_class]+dist_to[m_class][r/k][r%k]);
}
if (res == inf) res = -1;
ans[ind] = res;
} else {
todo[l/k >= m].pb(ind);
}
}
dnc(l, m, todo[0]);
dnc(m, r, todo[1]);
}
void solve() {
cin >> k >> n >> m >> q;
for (int i = 0; i < m; i++) {
int u, v, w; cin >> u >> v >> w;
g[u/k][u%k].pb({w, v%k});
}
for (int i = 0; i < q; i++) {
cin >> queries[i][0] >> queries[i][1];
}
vi query_inds(q); iota(all(query_inds), 0);
dnc(0, (n-1)/k+1, query_inds);
for (int i = 0; i < q; i++) cout << ans[i] << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
22 ms |
14684 KB |
Output is correct |
9 |
Correct |
23 ms |
14580 KB |
Output is correct |
10 |
Correct |
11 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
14612 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
4 ms |
9052 KB |
Output is correct |
8 |
Correct |
6 ms |
9048 KB |
Output is correct |
9 |
Correct |
22 ms |
14684 KB |
Output is correct |
10 |
Correct |
53 ms |
15452 KB |
Output is correct |
11 |
Correct |
43 ms |
15012 KB |
Output is correct |
12 |
Correct |
35 ms |
14580 KB |
Output is correct |
13 |
Correct |
59 ms |
15696 KB |
Output is correct |
14 |
Correct |
31 ms |
14676 KB |
Output is correct |
15 |
Correct |
33 ms |
14428 KB |
Output is correct |
16 |
Correct |
33 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10996 KB |
Output is correct |
6 |
Correct |
2 ms |
8856 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
10844 KB |
Output is correct |
9 |
Correct |
3 ms |
10844 KB |
Output is correct |
10 |
Correct |
20 ms |
15316 KB |
Output is correct |
11 |
Correct |
34 ms |
15964 KB |
Output is correct |
12 |
Correct |
49 ms |
17532 KB |
Output is correct |
13 |
Correct |
55 ms |
18004 KB |
Output is correct |
14 |
Correct |
44 ms |
16724 KB |
Output is correct |
15 |
Correct |
35 ms |
15536 KB |
Output is correct |
16 |
Correct |
33 ms |
15444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10996 KB |
Output is correct |
6 |
Correct |
2 ms |
8856 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
3 ms |
10844 KB |
Output is correct |
9 |
Correct |
3 ms |
10844 KB |
Output is correct |
10 |
Correct |
20 ms |
15316 KB |
Output is correct |
11 |
Correct |
34 ms |
15964 KB |
Output is correct |
12 |
Correct |
49 ms |
17532 KB |
Output is correct |
13 |
Correct |
55 ms |
18004 KB |
Output is correct |
14 |
Correct |
44 ms |
16724 KB |
Output is correct |
15 |
Correct |
35 ms |
15536 KB |
Output is correct |
16 |
Correct |
33 ms |
15444 KB |
Output is correct |
17 |
Correct |
34 ms |
15952 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
8796 KB |
Output is correct |
24 |
Correct |
3 ms |
8800 KB |
Output is correct |
25 |
Correct |
4 ms |
11120 KB |
Output is correct |
26 |
Correct |
5 ms |
11100 KB |
Output is correct |
27 |
Correct |
29 ms |
15196 KB |
Output is correct |
28 |
Correct |
50 ms |
17488 KB |
Output is correct |
29 |
Correct |
55 ms |
18176 KB |
Output is correct |
30 |
Correct |
45 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
22 ms |
14684 KB |
Output is correct |
9 |
Correct |
23 ms |
14580 KB |
Output is correct |
10 |
Correct |
11 ms |
12892 KB |
Output is correct |
11 |
Correct |
35 ms |
14612 KB |
Output is correct |
12 |
Correct |
2 ms |
8792 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
4 ms |
9052 KB |
Output is correct |
18 |
Correct |
6 ms |
9048 KB |
Output is correct |
19 |
Correct |
22 ms |
14684 KB |
Output is correct |
20 |
Correct |
53 ms |
15452 KB |
Output is correct |
21 |
Correct |
43 ms |
15012 KB |
Output is correct |
22 |
Correct |
35 ms |
14580 KB |
Output is correct |
23 |
Correct |
59 ms |
15696 KB |
Output is correct |
24 |
Correct |
31 ms |
14676 KB |
Output is correct |
25 |
Correct |
33 ms |
14428 KB |
Output is correct |
26 |
Correct |
33 ms |
14428 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
30 |
Correct |
2 ms |
10844 KB |
Output is correct |
31 |
Correct |
2 ms |
10996 KB |
Output is correct |
32 |
Correct |
2 ms |
8856 KB |
Output is correct |
33 |
Correct |
2 ms |
8796 KB |
Output is correct |
34 |
Correct |
3 ms |
10844 KB |
Output is correct |
35 |
Correct |
3 ms |
10844 KB |
Output is correct |
36 |
Correct |
20 ms |
15316 KB |
Output is correct |
37 |
Correct |
34 ms |
15964 KB |
Output is correct |
38 |
Correct |
49 ms |
17532 KB |
Output is correct |
39 |
Correct |
55 ms |
18004 KB |
Output is correct |
40 |
Correct |
44 ms |
16724 KB |
Output is correct |
41 |
Correct |
35 ms |
15536 KB |
Output is correct |
42 |
Correct |
33 ms |
15444 KB |
Output is correct |
43 |
Correct |
34 ms |
15952 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
2 ms |
8796 KB |
Output is correct |
46 |
Correct |
2 ms |
10844 KB |
Output is correct |
47 |
Correct |
2 ms |
10844 KB |
Output is correct |
48 |
Correct |
2 ms |
10844 KB |
Output is correct |
49 |
Correct |
3 ms |
8796 KB |
Output is correct |
50 |
Correct |
3 ms |
8800 KB |
Output is correct |
51 |
Correct |
4 ms |
11120 KB |
Output is correct |
52 |
Correct |
5 ms |
11100 KB |
Output is correct |
53 |
Correct |
29 ms |
15196 KB |
Output is correct |
54 |
Correct |
50 ms |
17488 KB |
Output is correct |
55 |
Correct |
55 ms |
18176 KB |
Output is correct |
56 |
Correct |
45 ms |
16724 KB |
Output is correct |
57 |
Correct |
84 ms |
19024 KB |
Output is correct |
58 |
Correct |
23 ms |
15448 KB |
Output is correct |
59 |
Correct |
36 ms |
16212 KB |
Output is correct |