Submission #631860

# Submission time Handle Problem Language Result Execution time Memory
631860 2022-08-19T02:08:12 Z jhnah917 Shopping Plans (CCO20_day2problem3) C++14
15 / 25
309 ms 131484 KB
#include <bits/stdc++.h>
using namespace std;
mt19937 RNG(0x917917);

// https://judge.yosupo.jp/submission/71648
class K_Shortest {

private:

    template <typename T>
    struct heap_node {
        std::array<heap_node*, 2> c;
        char is_set = 0;
        T key;

        friend heap_node* insert(heap_node* a, T new_key) {
            if (!a || new_key.first < a->key.first) {
                heap_node* n = new heap_node;
                n->c = {a, nullptr};
                n->key = new_key;
                n->is_set |= 1;
                return n;
            }
            a = new heap_node(*a);
            int z = RNG() & 1;
            a->c[z] = insert(a->c[z], new_key);
            a->is_set |= 1 << z;
            return a;
        }

        ~heap_node() {
            if (is_set & 1) delete c[0];
            if (is_set & 2) delete c[1];
        }
    };

    template <typename T> using min_priority_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>;

    std::vector<int64_t> k_shortest_paths(int N, std::vector<std::pair<std::array<int, 2>, int64_t>> edges, int st, int en, int K) {
        int M = int(edges.size());

        std::vector<std::vector<std::tuple<int, int, int64_t>>> radj(N);
        for (int e = 0; e < M; e++) {
            auto [x, l] = edges[e];
            auto [u, v] = x;
            radj[v].push_back({e, u, l});
        }

        std::vector<int64_t> dist(N, -1);
        std::vector<int> prvE(N, -1);
        std::vector<int> toposort; toposort.reserve(N);

        {
            min_priority_queue<std::pair<int64_t, int>> q;
            q.push({dist[en] = 0, en});

            while (!q.empty()) {
                auto [d, cur] = q.top(); q.pop();
                if (d > dist[cur]) continue;
                toposort.push_back(cur);
                for (auto [e, nxt, l] : radj[cur]) {
                    if (dist[nxt] == -1 || d + l < dist[nxt]) {
                        prvE[nxt] = e;
                        q.push({dist[nxt] = d + l, nxt});
                    }
                }
            }
        }

        std::vector<std::vector<std::pair<int64_t, int>>> adj(N);
        for (int e = 0; e < M; e++) {
            auto& [x, l] = edges[e];
            const auto& [u, v] = x;
            if (dist[v] == -1) continue;
            assert(dist[u] != -1);

            l += dist[v] - dist[u];
            assert(l >= 0);

            if (e == prvE[u]) continue;

            adj[u].push_back({l, v});
        }
        for (int i = 0; i < N; i++) {
            sort(adj[i].begin(), adj[i].end());
            adj[i].push_back({-1, -1}); // Sentinel
        }

        using iter_t = decltype(adj[0].begin());
        using hnode = heap_node<std::pair<int64_t, iter_t>>;
        std::vector<hnode*> node_roots(N, nullptr);
        for (int cur : toposort) {
            if (cur != en) {
                assert(edges[prvE[cur]].first[0] == cur);
                int prv = edges[prvE[cur]].first[1];
                node_roots[cur] = node_roots[prv];
            } else {
                node_roots[cur] = nullptr;
            }

            const auto& [l, nxt] = adj[cur][0];
            if (nxt != -1) {
                node_roots[cur] = insert(node_roots[cur], {l, adj[cur].begin()});
            }
        }

        std::vector<std::pair<int64_t, int>> dummy_adj({{0, st}, {-1, -1}});

        std::vector<int64_t> res; res.reserve(K);
        min_priority_queue<std::tuple<int64_t, hnode*, iter_t>> q;
        q.push({dist[st], nullptr, dummy_adj.begin()});
        while (int(res.size()) < K && !q.empty()) {
            auto [l, start_heap, val_iter] = q.top(); q.pop();
            res.push_back(l);
            int64_t elen = val_iter->first;
            if (next(val_iter)->second != -1) {
                q.push({l - elen + next(val_iter)->first, nullptr, next(val_iter)});
            }
            if (start_heap) {
                for (int z = 0; z < 2; z++) {
                    auto nxt_start = start_heap->c[z];
                    if (!nxt_start) continue;
                    q.push({l - elen + nxt_start->key.first, nxt_start, nxt_start->key.second});
                }
            }
            {
                int nxt = val_iter->second;
                auto nxt_start = node_roots[nxt];
                if (nxt_start) {
                    q.push({l + nxt_start->key.first, nxt_start, nxt_start->key.second});
                }
            }
        }
        return res;
    }

    std::vector<int64_t> data;

    void build(
            const std::vector<std::pair<std::array<int, 2>, int64_t>> &e,
            int n,
            int start,
            int finish,
            int k
    ) {
        data = k_shortest_paths(n + 2, e, start, finish, k);
        data.resize(k, -1);
    }

public:

    K_Shortest() {}

    template<typename Edge> K_Shortest(
            const std::vector<Edge> &edges,
            int n,
            int start,
            int finish,
            int k
    ) {
        int m = edges.size();
        std::vector<std::pair<std::array<int, 2>, int64_t>> e;
        e.reserve(m);
        for (const Edge &edge : edges) {
            e.push_back({{edge.first, edge.second}, edge.third});
        }
        build(e, n, start, finish, k);
    }

    K_Shortest(
            const std::vector<std::vector<pair<int, int64_t>>> &adj,
            int start,
            int finish,
            int k
    ) {
        int n = adj.size();
        std::vector<std::pair<std::array<int, 2>, int64_t>> e;
        for (int i = 0; i < n; i++) {
            for (const auto &p : adj[i]) {
                e.push_back({{i, p.first}, p.second});
            }
        }
        build(e, n, start, finish, k);
    }

    std::vector<int64_t> get() {return data;}
};

struct Trio {
    int first, second, third;
    Trio(int a, int b, int c) {
        first = a;
        second = b;
        third = c;
    }
    Trio() {first = 0, second = 0, third = 0;}
    template<typename T> friend T& operator << (T &out, const Trio &trio) {
        out << "(" << trio.first << ", " << trio.second << ", " << trio.third << ")";
        return out;
    }
    template<typename T> friend T& operator >> (T &in, Trio &trio) {return in >> trio.first >> trio.second >> trio.third;}
    friend bool operator == (const Trio &a, const Trio &b) {
        return a.first == b.first && a.second == b.second && a.third == b.third;
    }
    friend bool operator != (const Trio &a, const Trio &b) {
        return !(a == b);
    }
    friend bool operator < (const Trio &a, const Trio &b) {
        if (a.third == b.third && a.second == b.second) return a.first < b.first;
        else if (a.third == b.third) return a.second < b.second;
        else return a.third < b.third;
    }
    friend bool operator <= (const Trio &a, const Trio &b) {
        return a == b || a < b;
    }
    friend bool operator > (const Trio &a, const Trio &b) {
        return !(a <= b);
    }
    friend bool operator >= (const Trio &a, const Trio &b) {
        return !(a < b);
    }
};

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    int N, M, K; cin >> N >> M >> K;
    vector<Trio> E;
    vector<int> Group(N), Cost(N), X(M), Y(M);
    vector<vector<int>> G(M);
    for(int i=0; i<N; i++) cin >> Group[i] >> Cost[i], G[--Group[i]].push_back(i);
    for(int i=0; i<M; i++) cin >> X[i] >> Y[i], assert(X[i] == 1 && Y[i] == 1);
    int V = 0;
    for(int i=0; i<M; i++){
        for(int j=0; j<G[i].size(); j++){
            E.emplace_back(V, V+j+1, Cost[G[i][j]]);
            E.emplace_back(V+j+1, V+G[i].size()+1, 0);
        }
        V += G[i].size() + 1;
    }
    K_Shortest Solver(E, V+1, 0, V, K);
    auto R = Solver.get();
    for(int i=0; i<K; i++) cout << R[i] << "\n";
}

Compilation message

Main.cpp: In member function 'std::vector<long int> K_Shortest::k_shortest_paths(int, std::vector<std::pair<std::array<int, 2>, long int> >, int, int, int)':
Main.cpp:44:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   44 |             auto [x, l] = edges[e];
      |                  ^
Main.cpp:45:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   45 |             auto [u, v] = x;
      |                  ^
Main.cpp:58:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   58 |                 auto [d, cur] = q.top(); q.pop();
      |                      ^
Main.cpp:61:27: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   61 |                 for (auto [e, nxt, l] : radj[cur]) {
      |                           ^
Main.cpp:72:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   72 |             auto& [x, l] = edges[e];
      |                   ^
Main.cpp:73:25: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   73 |             const auto& [u, v] = x;
      |                         ^
Main.cpp:101:25: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  101 |             const auto& [l, nxt] = adj[cur][0];
      |                         ^
Main.cpp:113:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  113 |             auto [l, start_heap, val_iter] = q.top(); q.pop();
      |                  ^
Main.cpp: In function 'int main()':
Main.cpp:234:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  234 |         for(int j=0; j<G[i].size(); j++){
      |                      ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2708 KB Output is correct
2 Correct 5 ms 2452 KB Output is correct
3 Correct 7 ms 2580 KB Output is correct
4 Correct 5 ms 2196 KB Output is correct
5 Correct 5 ms 2704 KB Output is correct
6 Correct 5 ms 2836 KB Output is correct
7 Correct 4 ms 2072 KB Output is correct
8 Correct 3 ms 1740 KB Output is correct
9 Correct 3 ms 1492 KB Output is correct
10 Correct 6 ms 2452 KB Output is correct
11 Correct 3 ms 1612 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 660 KB Output is correct
14 Correct 6 ms 2580 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 660 KB Output is correct
17 Correct 4 ms 2196 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 916 KB Output is correct
20 Correct 5 ms 2708 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 4 ms 1748 KB Output is correct
24 Correct 3 ms 1568 KB Output is correct
25 Correct 3 ms 1620 KB Output is correct
26 Correct 5 ms 2584 KB Output is correct
27 Correct 5 ms 2068 KB Output is correct
28 Correct 4 ms 1748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 28400 KB Output is correct
2 Correct 82 ms 15604 KB Output is correct
3 Correct 75 ms 15736 KB Output is correct
4 Correct 71 ms 15020 KB Output is correct
5 Correct 77 ms 15568 KB Output is correct
6 Correct 87 ms 15512 KB Output is correct
7 Correct 84 ms 14892 KB Output is correct
8 Correct 61 ms 15392 KB Output is correct
9 Correct 17 ms 5068 KB Output is correct
10 Correct 99 ms 28116 KB Output is correct
11 Correct 16 ms 5060 KB Output is correct
12 Correct 32 ms 4432 KB Output is correct
13 Correct 75 ms 14012 KB Output is correct
14 Correct 73 ms 15640 KB Output is correct
15 Correct 16 ms 4112 KB Output is correct
16 Correct 65 ms 14020 KB Output is correct
17 Correct 82 ms 14956 KB Output is correct
18 Correct 25 ms 4252 KB Output is correct
19 Correct 96 ms 13864 KB Output is correct
20 Correct 97 ms 15872 KB Output is correct
21 Correct 16 ms 4044 KB Output is correct
22 Correct 58 ms 4544 KB Output is correct
23 Correct 69 ms 14824 KB Output is correct
24 Correct 24 ms 5276 KB Output is correct
25 Correct 21 ms 5232 KB Output is correct
26 Correct 69 ms 15784 KB Output is correct
27 Correct 85 ms 15916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2708 KB Output is correct
2 Correct 5 ms 2452 KB Output is correct
3 Correct 7 ms 2580 KB Output is correct
4 Correct 5 ms 2196 KB Output is correct
5 Correct 5 ms 2704 KB Output is correct
6 Correct 5 ms 2836 KB Output is correct
7 Correct 4 ms 2072 KB Output is correct
8 Correct 3 ms 1740 KB Output is correct
9 Correct 3 ms 1492 KB Output is correct
10 Correct 6 ms 2452 KB Output is correct
11 Correct 3 ms 1612 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 660 KB Output is correct
14 Correct 6 ms 2580 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 660 KB Output is correct
17 Correct 4 ms 2196 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 916 KB Output is correct
20 Correct 5 ms 2708 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 4 ms 1748 KB Output is correct
24 Correct 3 ms 1568 KB Output is correct
25 Correct 3 ms 1620 KB Output is correct
26 Correct 5 ms 2584 KB Output is correct
27 Correct 5 ms 2068 KB Output is correct
28 Correct 4 ms 1748 KB Output is correct
29 Correct 100 ms 28400 KB Output is correct
30 Correct 82 ms 15604 KB Output is correct
31 Correct 75 ms 15736 KB Output is correct
32 Correct 71 ms 15020 KB Output is correct
33 Correct 77 ms 15568 KB Output is correct
34 Correct 87 ms 15512 KB Output is correct
35 Correct 84 ms 14892 KB Output is correct
36 Correct 61 ms 15392 KB Output is correct
37 Correct 17 ms 5068 KB Output is correct
38 Correct 99 ms 28116 KB Output is correct
39 Correct 16 ms 5060 KB Output is correct
40 Correct 32 ms 4432 KB Output is correct
41 Correct 75 ms 14012 KB Output is correct
42 Correct 73 ms 15640 KB Output is correct
43 Correct 16 ms 4112 KB Output is correct
44 Correct 65 ms 14020 KB Output is correct
45 Correct 82 ms 14956 KB Output is correct
46 Correct 25 ms 4252 KB Output is correct
47 Correct 96 ms 13864 KB Output is correct
48 Correct 97 ms 15872 KB Output is correct
49 Correct 16 ms 4044 KB Output is correct
50 Correct 58 ms 4544 KB Output is correct
51 Correct 69 ms 14824 KB Output is correct
52 Correct 24 ms 5276 KB Output is correct
53 Correct 21 ms 5232 KB Output is correct
54 Correct 69 ms 15784 KB Output is correct
55 Correct 85 ms 15916 KB Output is correct
56 Correct 298 ms 116184 KB Output is correct
57 Correct 301 ms 113740 KB Output is correct
58 Correct 288 ms 111104 KB Output is correct
59 Correct 249 ms 94240 KB Output is correct
60 Correct 273 ms 116908 KB Output is correct
61 Correct 276 ms 131484 KB Output is correct
62 Correct 241 ms 79584 KB Output is correct
63 Correct 170 ms 65320 KB Output is correct
64 Correct 138 ms 56672 KB Output is correct
65 Correct 309 ms 114128 KB Output is correct
66 Correct 150 ms 66356 KB Output is correct
67 Correct 58 ms 5760 KB Output is correct
68 Correct 73 ms 13752 KB Output is correct
69 Correct 299 ms 122024 KB Output is correct
70 Correct 21 ms 4180 KB Output is correct
71 Correct 62 ms 7584 KB Output is correct
72 Correct 228 ms 94068 KB Output is correct
73 Correct 19 ms 4124 KB Output is correct
74 Correct 80 ms 13808 KB Output is correct
75 Correct 296 ms 129464 KB Output is correct
76 Correct 15 ms 4032 KB Output is correct
77 Correct 53 ms 5544 KB Output is correct
78 Correct 167 ms 59124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2708 KB Output is correct
2 Correct 5 ms 2452 KB Output is correct
3 Correct 7 ms 2580 KB Output is correct
4 Correct 5 ms 2196 KB Output is correct
5 Correct 5 ms 2704 KB Output is correct
6 Correct 5 ms 2836 KB Output is correct
7 Correct 4 ms 2072 KB Output is correct
8 Correct 3 ms 1740 KB Output is correct
9 Correct 3 ms 1492 KB Output is correct
10 Correct 6 ms 2452 KB Output is correct
11 Correct 3 ms 1612 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 660 KB Output is correct
14 Correct 6 ms 2580 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 660 KB Output is correct
17 Correct 4 ms 2196 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 916 KB Output is correct
20 Correct 5 ms 2708 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 4 ms 1748 KB Output is correct
24 Correct 3 ms 1568 KB Output is correct
25 Correct 3 ms 1620 KB Output is correct
26 Correct 5 ms 2584 KB Output is correct
27 Correct 5 ms 2068 KB Output is correct
28 Correct 4 ms 1748 KB Output is correct
29 Correct 100 ms 28400 KB Output is correct
30 Correct 82 ms 15604 KB Output is correct
31 Correct 75 ms 15736 KB Output is correct
32 Correct 71 ms 15020 KB Output is correct
33 Correct 77 ms 15568 KB Output is correct
34 Correct 87 ms 15512 KB Output is correct
35 Correct 84 ms 14892 KB Output is correct
36 Correct 61 ms 15392 KB Output is correct
37 Correct 17 ms 5068 KB Output is correct
38 Correct 99 ms 28116 KB Output is correct
39 Correct 16 ms 5060 KB Output is correct
40 Correct 32 ms 4432 KB Output is correct
41 Correct 75 ms 14012 KB Output is correct
42 Correct 73 ms 15640 KB Output is correct
43 Correct 16 ms 4112 KB Output is correct
44 Correct 65 ms 14020 KB Output is correct
45 Correct 82 ms 14956 KB Output is correct
46 Correct 25 ms 4252 KB Output is correct
47 Correct 96 ms 13864 KB Output is correct
48 Correct 97 ms 15872 KB Output is correct
49 Correct 16 ms 4044 KB Output is correct
50 Correct 58 ms 4544 KB Output is correct
51 Correct 69 ms 14824 KB Output is correct
52 Correct 24 ms 5276 KB Output is correct
53 Correct 21 ms 5232 KB Output is correct
54 Correct 69 ms 15784 KB Output is correct
55 Correct 85 ms 15916 KB Output is correct
56 Correct 298 ms 116184 KB Output is correct
57 Correct 301 ms 113740 KB Output is correct
58 Correct 288 ms 111104 KB Output is correct
59 Correct 249 ms 94240 KB Output is correct
60 Correct 273 ms 116908 KB Output is correct
61 Correct 276 ms 131484 KB Output is correct
62 Correct 241 ms 79584 KB Output is correct
63 Correct 170 ms 65320 KB Output is correct
64 Correct 138 ms 56672 KB Output is correct
65 Correct 309 ms 114128 KB Output is correct
66 Correct 150 ms 66356 KB Output is correct
67 Correct 58 ms 5760 KB Output is correct
68 Correct 73 ms 13752 KB Output is correct
69 Correct 299 ms 122024 KB Output is correct
70 Correct 21 ms 4180 KB Output is correct
71 Correct 62 ms 7584 KB Output is correct
72 Correct 228 ms 94068 KB Output is correct
73 Correct 19 ms 4124 KB Output is correct
74 Correct 80 ms 13808 KB Output is correct
75 Correct 296 ms 129464 KB Output is correct
76 Correct 15 ms 4032 KB Output is correct
77 Correct 53 ms 5544 KB Output is correct
78 Correct 167 ms 59124 KB Output is correct
79 Runtime error 1 ms 468 KB Execution killed with signal 6
80 Halted 0 ms 0 KB -