cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:question: Dijkstra 法
(include/emthrm/graph/shortest_path/dijkstra.hpp)

単一始点最短路問題 (single-source shortest path problem)

始点から他の任意の頂点までの最短路を求める問題である。

時間計算量

アルゴリズム 時間計算量
Bellman–Ford 法 $O(\lvert V \rvert \lvert E \rvert)$
Dijkstra 法 $O(\lvert E \rvert \log{\lvert V \rvert})$

仕様

Bellman–Ford 法

template <typename CostType>
struct BellmanFord;

メンバ変数

名前 説明
const CostType inf $\infty$
std::vector<CostType> dist dist[ver] は始点から頂点 $\mathrm{ver}$ までの最短距離を表す。ただし到達できないときは $\infty$ となる。

メンバ関数

名前 効果・戻り値
BellmanFord(const std::vector<std::vector<Edge<CostType>>>& graph, const CostType inf = std::numeric_limits<CostType>::max()); グラフ $\mathrm{graph}$ に対してオブジェクトを構築する。
bool has_negative_cycle(const int s); 始点 $s$ の単一始点最短路を構築し、グラフが負の閉路をもつかを返す。
std::vector<int> build_path(int t) const; 終点 $t$ の最短路。ただし到達できないときは空配列を返す。

Dijkstra 法

template <typename CostType>
struct Dijkstra

メンバ変数

名前 説明
const CostType inf $\infty$

メンバ関数

名前 効果・戻り値
Dijkstra(const std::vector<std::vector<Edge<CostType>>>& graph, const CostType inf = std::numeric_limits<CostType>::max()); グラフ $\mathrm{graph}$ に対してオブジェクトを構築する。
std::vector<CostType> build(const int s); 始点 $s$ の単一始点最短路。ただし到達できない頂点には $\infty$ を格納する。
std::vector<int> build_path(int t) const; 終点 $t$ の最短路。ただし到達できないときは空配列を返す。

参考文献

Bellman–Ford 法

Dijkstra 法

TODO

Submissons

Depends on

Verified with

Code

#ifndef EMTHRM_GRAPH_SHORTEST_PATH_DIJKSTRA_HPP_
#define EMTHRM_GRAPH_SHORTEST_PATH_DIJKSTRA_HPP_

#include <algorithm>
#include <cassert>
#include <functional>
#include <limits>
#include <queue>
#include <utility>
#include <vector>

#include "emthrm/graph/edge.hpp"

namespace emthrm {

template <typename CostType>
struct Dijkstra {
  const CostType inf;

  Dijkstra(const std::vector<std::vector<Edge<CostType>>>& graph,
           const CostType inf = std::numeric_limits<CostType>::max())
      : inf(inf), is_built(false), graph(graph) {}

  std::vector<CostType> build(const int s) {
    is_built = true;
    const int n = graph.size();
    std::vector<CostType> dist(n, inf);
    dist[s] = 0;
    prev.assign(n, -1);
    std::priority_queue<std::pair<CostType, int>,
                        std::vector<std::pair<CostType, int>>,
                        std::greater<std::pair<CostType, int>>> que;
    que.emplace(0, s);
    while (!que.empty()) {
      const auto [d, ver] = que.top();
      que.pop();
      if (d > dist[ver]) continue;
      for (const Edge<CostType>& e : graph[ver]) {
        if (dist[ver] + e.cost < dist[e.dst]) {
          dist[e.dst] = dist[ver] + e.cost;
          prev[e.dst] = ver;
          que.emplace(dist[e.dst], e.dst);
        }
      }
    }
    return dist;
  }

  std::vector<int> build_path(int t) const {
    assert(is_built);
    std::vector<int> res;
    for (; t != -1; t = prev[t]) {
      res.emplace_back(t);
    }
    std::reverse(res.begin(), res.end());
    return res;
  }

 private:
  bool is_built;
  std::vector<int> prev;
  std::vector<std::vector<Edge<CostType>>> graph;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_SHORTEST_PATH_DIJKSTRA_HPP_
#line 1 "include/emthrm/graph/shortest_path/dijkstra.hpp"



#include <algorithm>
#include <cassert>
#include <functional>
#include <limits>
#include <queue>
#include <utility>
#include <vector>

#line 1 "include/emthrm/graph/edge.hpp"
/**
 * @title 辺
 */

#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_

#include <compare>

namespace emthrm {

template <typename CostType>
struct Edge {
  CostType cost;
  int src, dst;

  explicit Edge(const int src, const int dst, const CostType cost = 0)
      : cost(cost), src(src), dst(dst) {}

  auto operator<=>(const Edge& x) const = default;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_EDGE_HPP_
#line 13 "include/emthrm/graph/shortest_path/dijkstra.hpp"

namespace emthrm {

template <typename CostType>
struct Dijkstra {
  const CostType inf;

  Dijkstra(const std::vector<std::vector<Edge<CostType>>>& graph,
           const CostType inf = std::numeric_limits<CostType>::max())
      : inf(inf), is_built(false), graph(graph) {}

  std::vector<CostType> build(const int s) {
    is_built = true;
    const int n = graph.size();
    std::vector<CostType> dist(n, inf);
    dist[s] = 0;
    prev.assign(n, -1);
    std::priority_queue<std::pair<CostType, int>,
                        std::vector<std::pair<CostType, int>>,
                        std::greater<std::pair<CostType, int>>> que;
    que.emplace(0, s);
    while (!que.empty()) {
      const auto [d, ver] = que.top();
      que.pop();
      if (d > dist[ver]) continue;
      for (const Edge<CostType>& e : graph[ver]) {
        if (dist[ver] + e.cost < dist[e.dst]) {
          dist[e.dst] = dist[ver] + e.cost;
          prev[e.dst] = ver;
          que.emplace(dist[e.dst], e.dst);
        }
      }
    }
    return dist;
  }

  std::vector<int> build_path(int t) const {
    assert(is_built);
    std::vector<int> res;
    for (; t != -1; t = prev[t]) {
      res.emplace_back(t);
    }
    std::reverse(res.begin(), res.end());
    return res;
  }

 private:
  bool is_built;
  std::vector<int> prev;
  std::vector<std::vector<Edge<CostType>>> graph;
};

}  // namespace emthrm
Back to top page