Submission #997999

# Submission time Handle Problem Language Result Execution time Memory
997999 2024-06-13T07:37:56 Z Nailuj_217 Toll (BOI17_toll) C++17
100 / 100
187 ms 41916 KB
#include <bits/stdc++.h>
#define l long long 
using namespace std;

struct LayerConnnection {
    l startlayer, endlayer;
    array<array<l, 5>, 5> min_weights;

};

l k, n, m, q;
const l LEN = 50005;
const l INF = 1LL<<60;

array<vector<pair<l, l>>, LEN> adj;
array<array<l, 20>, LEN> adj_matrix;
array<LayerConnnection, LEN*4+10> tree;


LayerConnnection build_tree(l i, l s, l f) {
    
    for (int a = 0; a < 5; a++) tree[i].min_weights[a].fill(INF);
    tree[i].startlayer = s;
    tree[i].endlayer = f;
    if (s == f) {
        for (int a = 0; a < k; a++) tree[i].min_weights[a][a] = 0;
        return tree[i];
    }

    LayerConnnection left = build_tree(i*2, s, (s+f)/2);
    LayerConnnection right = build_tree(i*2+1, (s+f)/2+1, f);

    for (int a = 0; a < k; a++) {
        for (int b = 0; b < k; b++) {
            for (int c = 0; c < k; c++) {
                if (!adj_matrix[((s+f)/2-1)*k+b][c]) continue;
                for (int d = 0; d < k; d++) {
                    tree[i].min_weights[a][d] = min(tree[i].min_weights[a][d], left.min_weights[a][b] + adj_matrix[((s+f)/2-1)*k+b][c] + right.min_weights[c][d]);
                }
            }
        }
    }

    return tree[i];

}


LayerConnnection query_tree(l i, l s, l f, l a, l b) {
    if (a > f || b < s) {
        LayerConnnection node = {-1, -1};
        return node;
    }
    if (a <= s && b >= f) return tree[i];

    LayerConnnection node = {s, f};
    for (int a = 0; a < k; a++) node.min_weights[a].fill(INF);
    LayerConnnection left = query_tree(i*2, s, (s+f)/2, a, b);
    LayerConnnection right = query_tree(i*2+1, (s+f)/2+1, f, a, b);

    if (left.startlayer == -1) return right;
    if (right.startlayer == -1) return left;


    for (int a = 0; a < k; a++) {
        for (int b = 0; b < k; b++) {
            for (int c = 0; c < k; c++) {
                if (!adj_matrix[((s+f)/2-1)*k+b][c]) continue;
                for (int d = 0; d < k; d++) {
                    node.min_weights[a][d] = min(node.min_weights[a][d], left.min_weights[a][b] + adj_matrix[((s+f)/2-1)*k+b][c] + right.min_weights[c][d]);
                }
            }
        }
    }

    return node;

}


int main() {

    cin >> k >> n >> m >> q;
    l a, b, w;

    for (int i = m; i--;) {
        cin >> a >> b >> w;
        adj[a].push_back({b, w});
        adj_matrix[a][b%k] = w;
    }

    build_tree(1, 1, n/k + 1);

    LayerConnnection node;

    while (q--) {
        cin >> a >> b;
        node = query_tree(1, 1, n/k+1, a/k+1, b/k+1);
        if (node.min_weights[a%k][b%k] < (1LL<<58)) cout << node.min_weights[a%k][b%k] << endl;
        else cout << -1 << endl;
    }


    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 94 ms 41916 KB Output is correct
2 Correct 1 ms 3672 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 5 ms 5724 KB Output is correct
6 Correct 4 ms 5724 KB Output is correct
7 Correct 5 ms 5724 KB Output is correct
8 Correct 98 ms 41672 KB Output is correct
9 Correct 71 ms 41556 KB Output is correct
10 Correct 41 ms 30648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 29168 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3660 KB Output is correct
4 Correct 1 ms 3416 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 18 ms 6008 KB Output is correct
8 Correct 30 ms 6068 KB Output is correct
9 Correct 91 ms 41760 KB Output is correct
10 Correct 160 ms 31180 KB Output is correct
11 Correct 92 ms 29264 KB Output is correct
12 Correct 79 ms 28300 KB Output is correct
13 Correct 128 ms 19028 KB Output is correct
14 Correct 69 ms 20048 KB Output is correct
15 Correct 62 ms 15136 KB Output is correct
16 Correct 78 ms 15184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3672 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3668 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 2 ms 5724 KB Output is correct
7 Correct 2 ms 5724 KB Output is correct
8 Correct 4 ms 3788 KB Output is correct
9 Correct 3 ms 3932 KB Output is correct
10 Correct 37 ms 41752 KB Output is correct
11 Correct 70 ms 29012 KB Output is correct
12 Correct 105 ms 31028 KB Output is correct
13 Correct 93 ms 31716 KB Output is correct
14 Correct 75 ms 29780 KB Output is correct
15 Correct 79 ms 15192 KB Output is correct
16 Correct 83 ms 15020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3672 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3668 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 2 ms 5724 KB Output is correct
7 Correct 2 ms 5724 KB Output is correct
8 Correct 4 ms 3788 KB Output is correct
9 Correct 3 ms 3932 KB Output is correct
10 Correct 37 ms 41752 KB Output is correct
11 Correct 70 ms 29012 KB Output is correct
12 Correct 105 ms 31028 KB Output is correct
13 Correct 93 ms 31716 KB Output is correct
14 Correct 75 ms 29780 KB Output is correct
15 Correct 79 ms 15192 KB Output is correct
16 Correct 83 ms 15020 KB Output is correct
17 Correct 75 ms 29088 KB Output is correct
18 Correct 1 ms 3676 KB Output is correct
19 Correct 1 ms 3676 KB Output is correct
20 Correct 1 ms 3676 KB Output is correct
21 Correct 1 ms 3624 KB Output is correct
22 Correct 1 ms 3676 KB Output is correct
23 Correct 8 ms 5968 KB Output is correct
24 Correct 8 ms 5976 KB Output is correct
25 Correct 26 ms 3932 KB Output is correct
26 Correct 15 ms 3932 KB Output is correct
27 Correct 61 ms 41804 KB Output is correct
28 Correct 105 ms 31056 KB Output is correct
29 Correct 98 ms 31828 KB Output is correct
30 Correct 79 ms 30036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 41916 KB Output is correct
2 Correct 1 ms 3672 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 5 ms 5724 KB Output is correct
6 Correct 4 ms 5724 KB Output is correct
7 Correct 5 ms 5724 KB Output is correct
8 Correct 98 ms 41672 KB Output is correct
9 Correct 71 ms 41556 KB Output is correct
10 Correct 41 ms 30648 KB Output is correct
11 Correct 101 ms 29168 KB Output is correct
12 Correct 1 ms 3420 KB Output is correct
13 Correct 1 ms 3660 KB Output is correct
14 Correct 1 ms 3416 KB Output is correct
15 Correct 1 ms 3420 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 18 ms 6008 KB Output is correct
18 Correct 30 ms 6068 KB Output is correct
19 Correct 91 ms 41760 KB Output is correct
20 Correct 160 ms 31180 KB Output is correct
21 Correct 92 ms 29264 KB Output is correct
22 Correct 79 ms 28300 KB Output is correct
23 Correct 128 ms 19028 KB Output is correct
24 Correct 69 ms 20048 KB Output is correct
25 Correct 62 ms 15136 KB Output is correct
26 Correct 78 ms 15184 KB Output is correct
27 Correct 1 ms 3672 KB Output is correct
28 Correct 1 ms 3420 KB Output is correct
29 Correct 1 ms 3420 KB Output is correct
30 Correct 1 ms 3668 KB Output is correct
31 Correct 1 ms 3420 KB Output is correct
32 Correct 2 ms 5724 KB Output is correct
33 Correct 2 ms 5724 KB Output is correct
34 Correct 4 ms 3788 KB Output is correct
35 Correct 3 ms 3932 KB Output is correct
36 Correct 37 ms 41752 KB Output is correct
37 Correct 70 ms 29012 KB Output is correct
38 Correct 105 ms 31028 KB Output is correct
39 Correct 93 ms 31716 KB Output is correct
40 Correct 75 ms 29780 KB Output is correct
41 Correct 79 ms 15192 KB Output is correct
42 Correct 83 ms 15020 KB Output is correct
43 Correct 75 ms 29088 KB Output is correct
44 Correct 1 ms 3676 KB Output is correct
45 Correct 1 ms 3676 KB Output is correct
46 Correct 1 ms 3676 KB Output is correct
47 Correct 1 ms 3624 KB Output is correct
48 Correct 1 ms 3676 KB Output is correct
49 Correct 8 ms 5968 KB Output is correct
50 Correct 8 ms 5976 KB Output is correct
51 Correct 26 ms 3932 KB Output is correct
52 Correct 15 ms 3932 KB Output is correct
53 Correct 61 ms 41804 KB Output is correct
54 Correct 105 ms 31056 KB Output is correct
55 Correct 98 ms 31828 KB Output is correct
56 Correct 79 ms 30036 KB Output is correct
57 Correct 187 ms 26448 KB Output is correct
58 Correct 77 ms 41872 KB Output is correct
59 Correct 95 ms 29272 KB Output is correct