답안 #677087

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
677087 2023-01-02T08:49:50 Z Cyanmond Toll (BOI17_toll) C++17
100 / 100
233 ms 21308 KB
#include <bits/stdc++.h>

using i64 = long long;

constexpr i64 inf = 1ll << 40;

int K;

struct node {
    std::vector<std::vector<i64>> cost;

    node() : cost(K, std::vector(K, inf)) {
        for (int i = 0; i < K; ++i) {
            cost[i][i] = 0;
        }
    }

    void fill_inf() {
        std::fill(cost.begin(), cost.end(), std::vector(K, inf));
    }
};

node operate(const node &a, const node &b) {
    const int w = K;
    node res;
    res.fill_inf();

    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < w; ++j) {
            for (int k = 0; k < w; ++k) {
                res.cost[i][k] = std::min(res.cost[i][k], a.cost[i][j] + b.cost[j][k]);
            }
        }
    }

    return res;
    // O(K^3)
}

struct segment_tree {
    int n, size;
    std::vector<node> tree;

    void update(const int i) {
        tree[i] = operate(tree[2 * i], tree[2 * i + 1]);
    }

    segment_tree(const std::vector<node> &data) : n((int)data.size()) {
        size = 1;
        while (size < n) {
            size *= 2;
        }
        tree.resize(2 * size);
        for (int i = 0; i < n; ++i) {
            tree[i + size] = data[i];
        }
        for (int i = size - 1; i >= 1; --i) {
            update(i);
        }
    }

    node fold(int l, int r) {
        node pl, pr;
        for (l += size, r += size; l < r; l /= 2, r /= 2) {
            if (l & 1) {
                pl = operate(pl, tree[l++]);
            }
            if (r & 1) {
                pr = operate(tree[--r], pr);
            }
        }
        return operate(pl, pr);
    }
};

int main() {
    int N, M, O;
    std::cin >> K >> N >> M >> O;

    std::vector<int> A(M), B(M);
    std::vector<i64> T(M);
    for (int i = 0; i < M; ++i) {
        std::cin >> A[i] >> B[i] >> T[i];
    }

    std::vector<int> X(O), Y(O);
    for (int i = 0; i < O; ++i) {
        std::cin >> X[i] >> Y[i];
    }

    const int W = (N + K - 1) / K;
    std::vector<node> cost_table(W - 1);
    for (auto &e : cost_table) {
        e.fill_inf();
    }
    for (int i = 0; i < M; ++i) {
        const int p = A[i] / K;
        cost_table[p].cost[A[i] % K][B[i] % K] = T[i];
    }
    
    segment_tree seg(cost_table);
    for (int i = 0; i < O; ++i) {
        const int l = X[i] / K, r = Y[i] / K;
        const auto res = seg.fold(l, r);
        i64 ans = res.cost[X[i] % K][Y[i] % K];
        if (ans >= inf) {
            ans = -1;
        }
        std::cout << ans << std::endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 17708 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 4 ms 596 KB Output is correct
8 Correct 108 ms 17580 KB Output is correct
9 Correct 104 ms 17548 KB Output is correct
10 Correct 69 ms 16088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 133 ms 16880 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 0 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 296 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 25 ms 724 KB Output is correct
8 Correct 26 ms 740 KB Output is correct
9 Correct 98 ms 17568 KB Output is correct
10 Correct 166 ms 20900 KB Output is correct
11 Correct 136 ms 17080 KB Output is correct
12 Correct 118 ms 18876 KB Output is correct
13 Correct 131 ms 13604 KB Output is correct
14 Correct 86 ms 11468 KB Output is correct
15 Correct 79 ms 11316 KB Output is correct
16 Correct 84 ms 11432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 564 KB Output is correct
7 Correct 3 ms 484 KB Output is correct
8 Correct 5 ms 696 KB Output is correct
9 Correct 4 ms 524 KB Output is correct
10 Correct 68 ms 17452 KB Output is correct
11 Correct 112 ms 16688 KB Output is correct
12 Correct 127 ms 20560 KB Output is correct
13 Correct 134 ms 21064 KB Output is correct
14 Correct 112 ms 19828 KB Output is correct
15 Correct 66 ms 11244 KB Output is correct
16 Correct 67 ms 11176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 564 KB Output is correct
7 Correct 3 ms 484 KB Output is correct
8 Correct 5 ms 696 KB Output is correct
9 Correct 4 ms 524 KB Output is correct
10 Correct 68 ms 17452 KB Output is correct
11 Correct 112 ms 16688 KB Output is correct
12 Correct 127 ms 20560 KB Output is correct
13 Correct 134 ms 21064 KB Output is correct
14 Correct 112 ms 19828 KB Output is correct
15 Correct 66 ms 11244 KB Output is correct
16 Correct 67 ms 11176 KB Output is correct
17 Correct 104 ms 16844 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 9 ms 596 KB Output is correct
24 Correct 11 ms 596 KB Output is correct
25 Correct 18 ms 724 KB Output is correct
26 Correct 15 ms 704 KB Output is correct
27 Correct 83 ms 17452 KB Output is correct
28 Correct 143 ms 20700 KB Output is correct
29 Correct 154 ms 21188 KB Output is correct
30 Correct 126 ms 19968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 17708 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 4 ms 596 KB Output is correct
8 Correct 108 ms 17580 KB Output is correct
9 Correct 104 ms 17548 KB Output is correct
10 Correct 69 ms 16088 KB Output is correct
11 Correct 133 ms 16880 KB Output is correct
12 Correct 1 ms 300 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 296 KB Output is correct
16 Correct 1 ms 304 KB Output is correct
17 Correct 25 ms 724 KB Output is correct
18 Correct 26 ms 740 KB Output is correct
19 Correct 98 ms 17568 KB Output is correct
20 Correct 166 ms 20900 KB Output is correct
21 Correct 136 ms 17080 KB Output is correct
22 Correct 118 ms 18876 KB Output is correct
23 Correct 131 ms 13604 KB Output is correct
24 Correct 86 ms 11468 KB Output is correct
25 Correct 79 ms 11316 KB Output is correct
26 Correct 84 ms 11432 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 2 ms 564 KB Output is correct
33 Correct 3 ms 484 KB Output is correct
34 Correct 5 ms 696 KB Output is correct
35 Correct 4 ms 524 KB Output is correct
36 Correct 68 ms 17452 KB Output is correct
37 Correct 112 ms 16688 KB Output is correct
38 Correct 127 ms 20560 KB Output is correct
39 Correct 134 ms 21064 KB Output is correct
40 Correct 112 ms 19828 KB Output is correct
41 Correct 66 ms 11244 KB Output is correct
42 Correct 67 ms 11176 KB Output is correct
43 Correct 104 ms 16844 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 9 ms 596 KB Output is correct
50 Correct 11 ms 596 KB Output is correct
51 Correct 18 ms 724 KB Output is correct
52 Correct 15 ms 704 KB Output is correct
53 Correct 83 ms 17452 KB Output is correct
54 Correct 143 ms 20700 KB Output is correct
55 Correct 154 ms 21188 KB Output is correct
56 Correct 126 ms 19968 KB Output is correct
57 Correct 233 ms 21308 KB Output is correct
58 Correct 110 ms 17664 KB Output is correct
59 Correct 138 ms 17152 KB Output is correct