zawatins-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub zawa-tin/zawatins-library

:heavy_check_mark: test/range-edge-graph1.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

#include "../src/graph/range-edge-graph.hpp"

#include <iostream>

int main() {
    // int n, m; std::cin >> n >> m;
    // zawa::range_edge_graph<long long> reg(n);
    // for (int _ = 0 ; _ < m ; _++) {
    //     int l, r, c; std::cin >> l >> r >> c;
    //     reg.add_edge(l - 1, r, l - 1, r, c);
    // }
    // long long sup = (1LL << 60);
    // auto ds = reg.dijkstra(0, sup);
    // std::cout << (ds.back() == sup ? -1LL : ds.back()) << std::endl;

    std::cout << "Hello World" << std::endl;
}

/*
 * 第二回全国統一プログラミング王決定戦予選 D Shortest Path on a Line
 * https://atcoder.jp/contests/nikkei2019-2-qual/submissions/37258810 
 */
#line 1 "test/range-edge-graph1.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

#line 2 "src/graph/range-edge-graph.hpp"

#include <vector>
#include <queue>
#include <utility>
#include <limits>

namespace zawa {

template <class cost_type>
class range_edge_graph {
private:
    int n;
    std::vector<std::vector<std::pair<int, cost_type>>> G;

    void add_edge(int u, int v, cost_type cost) {
        if (u >= 3 * n) {
            u -= 2 * n;
        }
        G[u].emplace_back(v, cost);
    }

public:
    range_edge_graph(int n) : n(n), G(3 * n) {
        for (int i = 1 ; i < n ; i++) {
            int child_l = ((i << 1) | 0), child_r = ((i << 1 | 1));
            add_edge(i, child_l, 0);
            add_edge(i, child_r, 0);
            add_edge(child_l + 2 * n, i + 2 * n, 0);
            add_edge(child_r + 2 * n, i + 2 * n, 0);
        }
    }

    void add_edge(int ul, int ur, int vl, int vr, cost_type cost) {
        int id = G.size();
        for (int l = ul + n, r = ur + n ; l < r ; l >>= 1, r >>= 1) {
            if (l & 1) {
                add_edge(l + 2 * n, id, 0);
                l++;
            }
            if (r & 1) {
                r--;
                add_edge(r + 2 * n, id, 0);
            }
        }
        std::vector<std::pair<int, cost_type>> node;
        for (int l = vl + n, r = vr + n ; l < r ; l >>= 1, r >>= 1) {
            if (l & 1) {
                node.emplace_back(l, cost);
                l++;
            }
            if (r & 1) {
                r--;
                node.emplace_back(r, cost);
            }
        }
        G.push_back(node);
    }

    std::vector<cost_type> dijkstra(int s, cost_type sup = std::numeric_limits<cost_type>::max()) {
        std::priority_queue<
            std::pair<cost_type, int>, 
            std::vector<std::pair<cost_type, int>>, 
            std::greater<std::pair<cost_type, int>>
                > que;
        que.emplace((cost_type)0, s + n);
        std::vector<cost_type> dist(G.size(), sup);
        dist[s + n] = (cost_type)0;
        while (que.size()) {
            auto [d, v] = que.top();
            que.pop();
            if (d > dist[v]) {
                continue;
            }
            for (auto [x, cost] : G[v]) {
                if (dist[x] > d + cost) {
                    dist[x] = d + cost;
                    que.emplace(d + cost, x);
                }
            }
        }
        return std::vector<cost_type>(dist.begin() + n, dist.begin() + 2 * n);
    }

};

} // namespace zawa
#line 4 "test/range-edge-graph1.test.cpp"

#include <iostream>

int main() {
    // int n, m; std::cin >> n >> m;
    // zawa::range_edge_graph<long long> reg(n);
    // for (int _ = 0 ; _ < m ; _++) {
    //     int l, r, c; std::cin >> l >> r >> c;
    //     reg.add_edge(l - 1, r, l - 1, r, c);
    // }
    // long long sup = (1LL << 60);
    // auto ds = reg.dijkstra(0, sup);
    // std::cout << (ds.back() == sup ? -1LL : ds.back()) << std::endl;

    std::cout << "Hello World" << std::endl;
}

/*
 * 第二回全国統一プログラミング王決定戦予選 D Shortest Path on a Line
 * https://atcoder.jp/contests/nikkei2019-2-qual/submissions/37258810 
 */
Back to top page