cp-library

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

View the Project on GitHub rniya/cp-library

:heavy_check_mark: Directed Shortest Cycle
(src/graph/DirectedShortestCycle.hpp)

概要

メンバ関数 効果 時間計算量
DirectedShortestCycle(n) $n$ 頂点 $0$ 辺のグラフとして初期化する. $\mathrm{O}(n)$
add_edge(u, v, w) 頂点 $u$ から $v$ への重み $w$ の有向辺を追加する. $\mathrm{O}(1)$
solve(r) 頂点 $r$ を含む最小重みサイクルの周長,頂点列,辺列を返す. $\mathrm{O}(m \log n)$

問題例

Verified with

Code

#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#include <tuple>
#include <vector>

template <typename T> struct DirectedShortestCycle {
    struct edge {
        int from, to;
        T cost;
        edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    };

    std::vector<std::vector<int>> G;
    std::vector<edge> edges;

    DirectedShortestCycle(int n) : n(n), G(n) {}

    void add_edge(int u, int v, T w) {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        assert(w >= 0);
        G[u].emplace_back(edges.size());
        edges.emplace_back(u, v, w);
    }

    std::tuple<T, std::vector<int>, std::vector<int>> solve(int r) {
        std::vector<int> prve(n, -1);
        std::vector<T> dist(n, inf);
        std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<>> pq;
        for (const int& idx : G[r]) {
            int u = edges[idx].to, w = edges[idx].cost;
            if (w < dist[u]) {
                dist[u] = w;
                prve[u] = idx;
                pq.emplace(dist[u], u);
            }
        }
        while (not pq.empty()) {
            auto [d, v] = pq.top();
            pq.pop();
            if (dist[v] < d) continue;
            for (const int& idx : G[v]) {
                int u = edges[idx].to, w = edges[idx].cost;
                if (dist[u] <= dist[v] + w) continue;
                dist[u] = dist[v] + w;
                prve[u] = idx;
                pq.emplace(dist[u], u);
            }
        }

        if (dist[r] == inf) return {inf, {}, {}};
        std::vector<int> vs, es;
        vs.emplace_back(r);
        while (true) {
            int idx = prve[vs.back()];
            es.emplace_back(idx);
            vs.emplace_back(edges[idx].from);
            if (vs.back() == r) break;
        }
        std::reverse(vs.begin(), vs.end());
        std::reverse(es.begin(), es.end());
        vs.pop_back();
        return {dist[r], vs, es};
    }

  private:
    constexpr static T inf = std::numeric_limits<T>::max() / 2;
    int n;
};
#line 1 "src/graph/DirectedShortestCycle.hpp"
#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#include <tuple>
#include <vector>

template <typename T> struct DirectedShortestCycle {
    struct edge {
        int from, to;
        T cost;
        edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    };

    std::vector<std::vector<int>> G;
    std::vector<edge> edges;

    DirectedShortestCycle(int n) : n(n), G(n) {}

    void add_edge(int u, int v, T w) {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        assert(w >= 0);
        G[u].emplace_back(edges.size());
        edges.emplace_back(u, v, w);
    }

    std::tuple<T, std::vector<int>, std::vector<int>> solve(int r) {
        std::vector<int> prve(n, -1);
        std::vector<T> dist(n, inf);
        std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<>> pq;
        for (const int& idx : G[r]) {
            int u = edges[idx].to, w = edges[idx].cost;
            if (w < dist[u]) {
                dist[u] = w;
                prve[u] = idx;
                pq.emplace(dist[u], u);
            }
        }
        while (not pq.empty()) {
            auto [d, v] = pq.top();
            pq.pop();
            if (dist[v] < d) continue;
            for (const int& idx : G[v]) {
                int u = edges[idx].to, w = edges[idx].cost;
                if (dist[u] <= dist[v] + w) continue;
                dist[u] = dist[v] + w;
                prve[u] = idx;
                pq.emplace(dist[u], u);
            }
        }

        if (dist[r] == inf) return {inf, {}, {}};
        std::vector<int> vs, es;
        vs.emplace_back(r);
        while (true) {
            int idx = prve[vs.back()];
            es.emplace_back(idx);
            vs.emplace_back(edges[idx].from);
            if (vs.back() == r) break;
        }
        std::reverse(vs.begin(), vs.end());
        std::reverse(es.begin(), es.end());
        vs.pop_back();
        return {dist[r], vs, es};
    }

  private:
    constexpr static T inf = std::numeric_limits<T>::max() / 2;
    int n;
};
Back to top page