競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
非負重みのグラフで、1つの始点から全頂点への最短距離を求めます。
WGraphはstd::vector<std::vector<Edge>>、辺はEdge{int to, std::int64_t w}です。重みの範囲は0<=w<=1'000'000'000、頂点番号はグラフの範囲内で指定します。
各呼び出しの末尾に正のstd::int64_t infを指定できます。省略時はkInf = std::int64_t{1} << 60です。
未到達の距離はinf、有限な最短距離がinf以上となる場合はoverflow_errorです。
V=頂点数、E=辺数、L=返す経路の頂点数です。以下は全てO(V+E log(E+1))時間・O(V+E)追加領域です。
Dijkstrainline void Dijkstra(const WGraph &graph, std::vector<std::int64_t> &distances, int start_index, std::int64_t inf = (std::int64_t{1} << 60));
inline void Dijkstra(const WGraph &graph, std::vector<std::int64_t> &distances, int start_index, int target_index, std::vector<int> &path, std::int64_t inf = (std::int64_t{1} << 60));
inline void Dijkstra(int n, std::span<const std::tuple<int,int,std::int64_t>> edges, std::vector<std::int64_t> &distances, int start_index, std::int64_t inf = (std::int64_t{1} << 60));
inline void Dijkstra(int n, std::span<const std::tuple<int,int,std::int64_t>> edges, std::vector<std::int64_t> &distances, int start_index, int target_index, std::vector<int> &path, std::int64_t inf = (std::int64_t{1} << 60));
WGraph版は全頂点の距離をdistancesに上書きします。終点を指定すると、始点から終点までの頂点列もpathに上書きします。tupleの辺列を渡す版でも、終点の指定に応じて同じ出力を上書きします。
制約: 頂点・重み・infと有限距離の範囲は下記の前提を満たすこと。
計算量: O(V+E log(E+1))時間・O(V+E)追加領域。経路出力のO(L+1)も含みます。
経路復元のO(L+1)時間・領域も上の計算量に含みます。終点が到達不能ならpathは空です。経路版も終点への到達時には打ち切りません。
辺列版のedgesは(u,v,w)がu→vと重みを表すstd::tuple<int,int,std::int64_t>の連続列です。std::span<const std::tuple<int,int,std::int64_t>>で受けるので、同じ型のvectorも渡せます。入力が辺列として揃っている場合に使い、内部でO(V+E)の隣接表を構築します。呼出し中は入力を変更しません。
std::vector<std::tuple<int,int,std::int64_t>> edges{{0,1,2},{1,2,3}};
std::vector<std::int64_t> distance;
std::vector<int> path;
Dijkstra(3, edges, distance, 0, 1, path);
// distance={0,2,5}, path={0,1}。終点より先の距離も確定する。
const std::int64_t inf = 1'000'000'000'000;
Dijkstra(3, edges, distance, 0, inf); // 到達不能はこのinfになる。
最小距離の頂点の出辺を緩和し、改善した距離を待ち行列へ入れます。
非最短の経路がinfを超えていても、全ての有限な最短距離が範囲内なら正常に返ります。
頂点数V、辺数Eに対し、時間はO(V+E log(E+1))、追加領域はO(V+E)です。
距離のみの呼び出しでは経路用配列を作りません。入力のグラフは変更しません。
入力検査は既定で有効です。WGraph版は始点・終点と、処理中に参照した辺の行先・重みを検査します。検査のために未到達成分の全辺を走査することはありません。
辺列版は構築時に全辺を参照し、両端点と重みを検査します。不正な頂点・重み・始点・終点はout_of_range、負のnや容量超過はlength_errorです。
SKIP_RUNTIME_CHECKSを定義する場合も、重み・頂点番号の前提は同じです。容量上限や有限距離とinfの衝突への対策は維持します。inf<=0は検査省略時もinvalid_argumentです。
SIRKIT_DIJKSTRA_STANDARD_HEAPを定義すると、小規模グラフで標準のstd::priority_queueを使う経路を選びます。距離と経路のAPIは共通です。
EdgeEdge(int destination, std::int64_t weight);
行先toと重みwを持つ辺を作ります。引数なしでは両方0です。
制約: グラフへ追加して使うときは各最短路APIの頂点・重みの前提に従います。
計算量: O(1)。
[共通型・定数]
Edge(to,w): 行先toとint64_tの重みwを持つ有向辺。
WGraph: 出発点を隣接リストの添字とする辺列。kInf=std::int64_t{1}<<60。
[Dijkstra] V=頂点数、E=辺数、L=返す経路の頂点数。
Dijkstra(graph, distances, start_index, inf=2^60): 全距離を上書き。O(V+E log(E+1))時間・O(V+E)追加領域。
Dijkstra(graph, distances, start_index, target_index, path, inf=2^60): 全距離と頂点列を上書き。上と同じ計算量(復元O(L+1)を含む)。到達不能なら空。
重みは[0, 1e9]、未到達はinf。inf>0。有限最短距離がinf以上ならoverflow_error。
Dijkstra(n, edges, distances, start_index[, target_index, path]): (始点,終点,int64重み)のtuple列を
span<const tuple<int,int,int64_t>>で受け、内部CSRを作って全頂点の距離を返す。vectorからも渡せる。
辺列版も末尾にinfを指定できる。既定呼出しは従来の定数番兵の高速経路を使う。
辺列2版もO(V+E log(E+1))時間・O(V+E)追加領域。CSR構築のO(V+E)を含む。
SKIP_RUNTIME_CHECKSを定義すると入力検査を省く。
WGraph版の検査は始点・終点と処理中に参照する辺のみ。未到達成分は走査しない。
辺列版は構築時に全辺を参照し、両端点と重みを検査する。n<0・容量超過はlength_error。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Shortest Path | dijkstra.cpp | 現行未照合・要再提出 | AC |
| 独立参照による補足検査 | graph_local.cpp | ローカル補足 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 未測定 |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考: https://zenn.dev/reputeless/books/standard-cpp-for-competitive-programming/viewer/dijkstra
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/dijkstra.md
//
// API一覧
// [共通型・定数]
// Edge(to,w): 行先toとint64_tの重みwを持つ有向辺。
// WGraph: 出発点を隣接リストの添字とする辺列。kInf=std::int64_t{1}<<60。
//
// [Dijkstra] V=頂点数、E=辺数、L=返す経路の頂点数。
// Dijkstra(graph, distances, start_index, inf=2^60): 全距離を上書き。O(V+E log(E+1))時間・O(V+E)追加領域。
// Dijkstra(graph, distances, start_index, target_index, path, inf=2^60): 全距離と頂点列を上書き。上と同じ計算量(復元O(L+1)を含む)。到達不能なら空。
// 重みは[0, 1e9]、未到達はinf。inf>0。有限最短距離がinf以上ならoverflow_error。
// Dijkstra(n, edges, distances, start_index[, target_index, path]): (始点,終点,int64重み)のtuple列を
// span<const tuple<int,int,int64_t>>で受け、内部CSRを作って全頂点の距離を返す。vectorからも渡せる。
// 辺列版も末尾にinfを指定できる。既定呼出しは従来の定数番兵の高速経路を使う。
// 辺列2版もO(V+E log(E+1))時間・O(V+E)追加領域。CSR構築のO(V+E)を含む。
// SKIP_RUNTIME_CHECKSを定義すると入力検査を省く。
// WGraph版の検査は始点・終点と処理中に参照する辺のみ。未到達成分は走査しない。
// 辺列版は構築時に全辺を参照し、両端点と重みを検査する。n<0・容量超過はlength_error。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <queue>
#include <stdexcept>
#include <span>
#include <type_traits>
#include <tuple>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_INF_DEFINED
#define SIRKIT_INF_DEFINED
inline constexpr std::int64_t kInf = std::int64_t{1} << 60;
#endif
#ifndef SIRKIT_GRAPH_TYPES_DEFINED
#define SIRKIT_GRAPH_TYPES_DEFINED
struct Edge
{
int to;
std::int64_t w;
Edge(int destination, std::int64_t weight) : to(destination), w(weight) {}
};
using WGraph = std::vector<std::vector<Edge>>;
#endif
// 小規模は二分ヒープ、大規模はキューが64件を超えたらradix heapへ切り替える。
#ifndef SIRKIT_DIJKSTRA_DEFINED
#define SIRKIT_DIJKSTRA_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
// バルク入力専用。行spanはowned arcs_を参照し、コピー/移動しない。
class DijkstraCsrGraph {
struct Arc {
int to;
std::uint32_t w;
Arc() noexcept {} // 全要素を下の散布で上書きするため先行初期化不要。
};
std::vector<Arc> arcs_;
std::vector<std::span<const Arc>> rows_;
public:
static void check_capacity(std::size_t vertices, std::size_t edges) {
if (edges > std::vector<Arc>{}.max_size() || vertices > std::vector<std::span<const Arc>>{}.max_size())
throw std::length_error("Dijkstra: CSR capacity exceeded");
}
DijkstraCsrGraph(int n, std::span<const std::tuple<int,int,std::int64_t>> input) {
if(n<0) throw std::length_error("Dijkstra: negative vertex count");
check_capacity(static_cast<std::size_t>(n), input.size());
std::vector<std::size_t> begin(static_cast<std::size_t>(n)+1);
for(const auto& [u,v,w]:input) {
#ifndef SKIP_RUNTIME_CHECKS
if(u<0 || u>=n || v<0 || v>=n)
throw std::out_of_range("Dijkstra: invalid bulk endpoint");
if(w<0 || w>1'000'000'000)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
#endif
++begin[static_cast<std::size_t>(u)+1];
}
for(int v=0;v<n;++v)begin[v+1]+=begin[v];
arcs_.resize(input.size());
rows_.reserve(n);
for(int v=0;v<n;++v) {
const auto length=begin[v+1]-begin[v];
if(length==0) rows_.emplace_back();
else rows_.emplace_back(arcs_.data()+begin[v],length);
}
// beginを直接cursorへ変え、次数配列のコピーを省く。
for(const auto& [u,v,w]:input) {
auto& arc=arcs_[begin[u]++];arc.to=v;arc.w=static_cast<std::uint32_t>(w);
}
}
DijkstraCsrGraph(const DijkstraCsrGraph&)=delete;
DijkstraCsrGraph& operator=(const DijkstraCsrGraph&)=delete;
DijkstraCsrGraph(DijkstraCsrGraph&&)=delete;
DijkstraCsrGraph& operator=(DijkstraCsrGraph&&)=delete;
std::size_t size() const noexcept{return rows_.size();}
const auto& operator[](std::size_t v) const noexcept{return rows_[v];}
const auto* data() const noexcept{return rows_.data();}
};
// 内部用の単調整数ヒープ。格納する有限距離はkInf未満。
#ifndef SIRKIT_DIJKSTRA_RADIX_HEAP_DEFINED
#define SIRKIT_DIJKSTRA_RADIX_HEAP_DEFINED
class DijkstraRadixHeap
{
// 待機距離はlastから最大辺重み以内なので、下位32bitから復元できる。
using State = std::pair<std::uint32_t, int>;
std::array<std::vector<State>, 64> buckets_;
std::vector<int> zero_;
std::array<std::uint64_t, 64> minima_;
std::uint64_t last_ = 0, occupied_ = 0;
#if defined(__clang__)
[[gnu::noinline]]
#elif defined(__GNUC__)
[[gnu::always_inline]]
#endif
void refill()
{
const auto bucket = std::countr_zero(occupied_);
auto &source = buckets_[bucket];
last_ = minima_[bucket];
for (const auto &state : source)
{
const auto key = last_ + static_cast<std::uint32_t>(state.first - static_cast<std::uint32_t>(last_));
if (key == last_) {
zero_.emplace_back(state.second);
occupied_ |= 1;
continue;
}
const auto target = std::bit_width(key ^ last_);
buckets_[target].push_back(state);
minima_[target] = std::min(minima_[target], key);
occupied_ |= std::uint64_t{1} << target;
}
source.clear();
minima_[bucket] = std::numeric_limits<std::uint64_t>::max();
occupied_ &= ~(std::uint64_t{1} << bucket);
}
public:
explicit DijkstraRadixHeap(std::uint64_t last = 0) : last_(last)
{
minima_.fill(std::numeric_limits<std::uint64_t>::max());
}
bool empty() const noexcept { return occupied_ == 0; }
#if defined(__GNUC__) || defined(__clang__)
[[gnu::always_inline]]
#endif
void emplace(std::int64_t distance, int vertex)
{
const auto key = static_cast<std::uint64_t>(distance);
if (key == last_) {
zero_.emplace_back(vertex);
occupied_ |= 1;
return;
}
const auto bucket = std::bit_width(key ^ last_);
buckets_[bucket].emplace_back(static_cast<std::uint32_t>(key), vertex);
minima_[bucket] = std::min(minima_[bucket], key);
occupied_ |= std::uint64_t{1} << bucket;
}
#if defined(__clang__)
[[gnu::always_inline]]
#endif
std::pair<std::uint64_t, int> pop()
{
if (zero_.empty()) refill();
const auto vertex = zero_.back();
zero_.pop_back();
if (zero_.empty())
occupied_ &= ~std::uint64_t{1};
return {last_, vertex};
}
#if defined(__GNUC__) || defined(__clang__)
[[gnu::always_inline]]
#endif
std::pair<std::uint64_t, int> pop_singleton()
{
if (zero_.empty()) {
const auto bucket = std::countr_zero(occupied_);
auto& source = buckets_[bucket];
if (source.size() == 1) {
const int vertex = source.back().second;
source.pop_back();
last_ = minima_[bucket];
minima_[bucket] = std::numeric_limits<std::uint64_t>::max();
occupied_ &= ~(std::uint64_t{1} << bucket);
return {last_, vertex};
}
}
return pop();
}
};
#endif
// radix移行後の末端処理。確認が64回続けて外れたら元のループへ戻す。
template <bool RestorePath, bool MayOverflow, std::int64_t kDistanceLimit = kInf, class GraphLike = WGraph>
#if defined(__GNUC__) || defined(__clang__)
[[gnu::noinline]]
#endif
inline void DijkstraRadixRun(const GraphLike& graph,
std::vector<std::int64_t>& distance, std::vector<int>& previous,
std::vector<int>& overflow_targets, DijkstraRadixHeap& radix,
int first_from = -1, std::int64_t first_distance = 0, std::size_t first_begin = 0)
{
#ifndef SKIP_RUNTIME_CHECKS
[[maybe_unused]] const int n = static_cast<int>(graph.size());
[[maybe_unused]] constexpr std::int64_t max_weight = 1'000'000'000;
#endif
unsigned terminal_budget = 64;
auto* const distance_data = distance.data();
auto* const previous_data = previous.data();
const auto relax = [&, distance_data, previous_data]
#if defined(__clang__)
[[gnu::always_inline]]
#endif
(std::int64_t current, int from, std::size_t begin = 0) {
(void)previous_data;
const auto& edges = graph[from];
const auto end = edges.end();
for (auto cursor = edges.begin() + begin; cursor != end; ++cursor) {
const auto& edge = *cursor;
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (!std::is_same_v<GraphLike, DijkstraCsrGraph>) {
if (edge.to < 0 || edge.to >= n)
throw std::out_of_range("Dijkstra: invalid destination vertex");
if (edge.w < 0 || edge.w > max_weight)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
}
#endif
const auto candidate = current + edge.w;
if constexpr (MayOverflow) if (candidate >= kDistanceLimit) [[unlikely]] {
if (distance_data[edge.to] == kDistanceLimit) overflow_targets.push_back(edge.to);
continue;
}
if (candidate >= distance_data[edge.to]) continue;
distance_data[edge.to] = candidate;
if constexpr (RestorePath) previous_data[edge.to] = from;
if (terminal_budget != 0) {
--terminal_budget;
// 終端と、その1辺前だけを即時処理する。長鎖はたどらない。
const auto& next = graph[edge.to];
if (next.empty()) { terminal_budget = 64; continue; }
if (next.size() == 1) {
const auto& tail = next.front();
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (!std::is_same_v<GraphLike, DijkstraCsrGraph>) {
if (tail.to < 0 || tail.to >= n)
throw std::out_of_range("Dijkstra: invalid destination vertex");
if (tail.w < 0 || tail.w > max_weight)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
}
#endif
if (graph[tail.to].empty()) {
terminal_budget = 64;
const auto terminal_distance = candidate + tail.w;
if constexpr (MayOverflow) if (terminal_distance >= kDistanceLimit) {
if (distance_data[tail.to] == kDistanceLimit) overflow_targets.push_back(tail.to);
continue;
}
if (terminal_distance < distance_data[tail.to]) {
distance_data[tail.to] = terminal_distance;
if constexpr (RestorePath) previous_data[tail.to] = edge.to;
}
continue;
}
}
}
radix.emplace(candidate, edge.to);
}
};
if (first_from >= 0) {
relax(first_distance, first_from, first_begin);
if (terminal_budget == 0) return;
}
while (!radix.empty()) {
#if defined(__GNUC__) && !defined(__clang__)
const auto [key, vertex] = RestorePath ? radix.pop_singleton() : radix.pop();
#else
const auto [key, vertex] = radix.pop();
#endif
const auto value = static_cast<std::int64_t>(key);
if (value != distance_data[vertex]) continue;
relax(value, vertex);
if (terminal_budget == 0) return;
}
}
// radix移行後の所有と処理を分け、二分ヒープ側の作業領域を小さくする。
template <bool RestorePath, bool MayOverflow, std::int64_t kDistanceLimit = kInf, class InitialRange, class GraphLike = WGraph>
#if defined(__GNUC__) || defined(__clang__)
[[gnu::noinline]]
#endif
inline void DijkstraRadixFinish(const GraphLike& graph,
std::vector<std::int64_t>& distance, std::vector<int>& previous,
std::vector<int>& overflow_targets,
const InitialRange& initial,
std::int64_t current, int from, std::size_t resume)
{
#ifndef SKIP_RUNTIME_CHECKS
[[maybe_unused]] const int n = static_cast<int>(graph.size());
[[maybe_unused]] constexpr std::int64_t max_weight = 1'000'000'000;
#endif
DijkstraRadixHeap radix(static_cast<std::uint64_t>(current));
for (const auto& value : initial) radix.emplace(value.first, value.second);
auto* const distance_data = distance.data();
auto* const previous_data = previous.data();
auto* const adjacency = graph.data();
const auto relax = [&, distance_data, previous_data, adjacency]
#if defined(__GNUC__) || defined(__clang__)
[[gnu::always_inline]]
#endif
(std::int64_t current, int from, std::size_t begin = 0) {
(void)previous_data;
const auto& edges = adjacency[from];
const auto end = edges.end();
for (auto cursor = edges.begin() + begin; cursor != end; ++cursor)
{
const auto& edge = *cursor;
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (!std::is_same_v<GraphLike, DijkstraCsrGraph>) {
if (edge.to < 0 || edge.to >= n)
throw std::out_of_range("Dijkstra: invalid destination vertex");
if (edge.w < 0 || edge.w > max_weight)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
}
#endif
const std::int64_t candidate = current + edge.w;
if constexpr (MayOverflow) if (candidate >= kDistanceLimit) [[unlikely]]
{
if (distance_data[edge.to] == kDistanceLimit) overflow_targets.push_back(edge.to);
continue;
}
if (candidate >= distance_data[edge.to])
continue;
distance_data[edge.to] = candidate;
if constexpr (RestorePath)
previous_data[edge.to] = from;
radix.emplace(candidate, edge.to);
}
};
if (graph.size() > 4096) {
DijkstraRadixRun<RestorePath, MayOverflow, kDistanceLimit>(
graph, distance, previous, overflow_targets, radix, from, current, resume);
} else if (resume < graph[from].size()) {
relax(current, from, resume);
}
while (!radix.empty()) {
const auto [key, vertex] = radix.pop();
const auto value = static_cast<std::int64_t>(key);
if (value == distance_data[vertex]) relax(value, vertex);
}
}
// キューが65件になった辺でradix heapへ移し、残りの辺もそのまま処理する。
// 初期ヒープ用。未使用領域を初期化しない。
struct DijkstraInitialState {
std::int64_t first;
int second;
};
template <bool RestorePath, bool MayOverflow, bool PruneTerminal = true, bool RecordInitialChain = false, std::int64_t kDistanceLimit = kInf, class GraphLike = WGraph>
#if defined(__GNUC__) || defined(__clang__)
[[gnu::always_inline]]
#endif
inline void DijkstraBigImpl(const GraphLike &graph, std::vector<std::int64_t> &distances,
int start_index, int target_index, std::vector<int> *path)
{
static_assert(!RecordInitialChain || (RestorePath && PruneTerminal));
constexpr bool DeferPrevious = RecordInitialChain;
#if defined(__GNUC__) && !defined(__clang__)
constexpr bool UseBounded = RecordInitialChain;
#else
constexpr bool UseBounded = true;
#endif
#ifndef SKIP_RUNTIME_CHECKS
[[maybe_unused]] constexpr std::int64_t max_weight = 1'000'000'000;
#endif
if (graph.size() > static_cast<std::size_t>(std::numeric_limits<int>::max()))
throw std::length_error("Dijkstra: too many vertices");
#ifndef SKIP_RUNTIME_CHECKS
[[maybe_unused]] const int n = static_cast<int>(graph.size());
#endif
#ifndef SKIP_RUNTIME_CHECKS
if (start_index < 0 || start_index >= n ||
(RestorePath && (target_index < 0 || target_index >= n)))
throw std::out_of_range("Dijkstra: invalid start or target vertex");
#endif
// 有限距離は上限未満。上限に最大辺重みを足してもint64_tに収まる。
constexpr std::int64_t unreachable = kDistanceLimit;
#ifndef SKIP_RUNTIME_CHECKS
std::vector<std::int64_t> local_distance;
auto& distance = local_distance;
#else
auto& distance = distances;
#endif
distance.assign(graph.size(), unreachable);
std::vector<int> overflow_targets;
std::vector<int> previous, result;
// 始点に出辺がなければ、経路用配列やヒープは不要。
if constexpr (PruneTerminal) if (graph[start_index].empty()) {
distance[start_index] = 0;
if constexpr (RestorePath)
if (start_index == target_index) result.push_back(start_index);
#ifndef SKIP_RUNTIME_CHECKS
distances.swap(local_distance);
#endif
if constexpr (RestorePath) path->swap(result);
return;
}
int initial_vertex = start_index;
std::int64_t initial_distance = 0;
bool target_in_prefix = false;
if constexpr (RecordInitialChain) {
auto* const distance_data = distance.data();
auto* const adjacency = graph.data();
distance_data[start_index] = 0;
result.push_back(start_index);
target_in_prefix = start_index == target_index;
while (adjacency[initial_vertex].size() == 1) {
const auto& edge = adjacency[initial_vertex].front();
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (!std::is_same_v<GraphLike, DijkstraCsrGraph>) {
if (edge.to < 0 || edge.to >= n)
throw std::out_of_range("Dijkstra: invalid destination vertex");
if (edge.w < 0 || edge.w > max_weight)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
}
#endif
const auto candidate = initial_distance + edge.w;
if constexpr (MayOverflow) if (candidate >= kDistanceLimit) {
// 一本道のため、未到達の行先に別経路はない。
if (distance_data[edge.to] == kDistanceLimit)
throw std::overflow_error("Dijkstra: finite distance collides with INF");
break;
}
if (candidate >= distance_data[edge.to]) break;
distance_data[edge.to] = candidate;
initial_distance = candidate;
initial_vertex = edge.to;
if (!target_in_prefix) {
result.push_back(initial_vertex);
target_in_prefix = initial_vertex == target_index;
}
}
// 出辺が1本以下なら未処理の到達頂点はない。
if (adjacency[initial_vertex].size() <= 1) {
if (!target_in_prefix) result.clear();
#ifndef SKIP_RUNTIME_CHECKS
distances.swap(local_distance);
#endif
path->swap(result);
return;
}
}
if constexpr (RestorePath) {
if constexpr (DeferPrevious) previous.reserve(graph.size());
else previous.assign(graph.size(), -1);
}
// 到達範囲が小さい間は直前頂点を64件まで記録し、必要になってから全頂点分を初期化する。
struct PreviousRecord { int vertex, parent; };
std::array<PreviousRecord, DeferPrevious ? 64 : 0> previous_records;
std::size_t previous_record_count = 0;
const auto commit_previous = [&] {
if (!previous.empty()) return;
previous.assign(graph.size(), -1);
for (std::size_t i = 0; i < previous_record_count; ++i) {
const auto& record = previous_records[i];
previous[record.vertex] = record.parent;
}
};
const auto record_previous = [&](int vertex, int parent) {
if (previous.empty()) {
if (previous_record_count < previous_records.size()) {
previous_records[previous_record_count++] = {vertex, parent};
return;
}
commit_previous();
}
previous[vertex] = parent;
};
using State = std::conditional_t<UseBounded, DijkstraInitialState, std::pair<std::int64_t, int>>;
struct Compare {
bool operator()(const State& a, const State& b) const noexcept {
return a.first > b.first || (a.first == b.first && a.second > b.second);
}
};
using InitialCompare = std::conditional_t<UseBounded, Compare, std::greater<State>>;
struct InitialQueue : std::priority_queue<State, std::vector<State>, InitialCompare> {
const std::vector<State>& states() const noexcept { return this->c; }
};
struct BoundedQueue {
std::array<State, 65> storage_;
std::size_t size_ = 0;
bool empty() const noexcept { return size_ == 0; }
std::size_t size() const noexcept { return size_; }
const State& top() const noexcept { return storage_.front(); }
void emplace(std::int64_t distance, int vertex) {
const State value{distance, vertex};
std::size_t hole = size_++;
while (hole != 0) {
const auto parent = (hole - 1) / 2;
if (!Compare{}(storage_[parent], value)) break;
storage_[hole] = storage_[parent];
hole = parent;
}
storage_[hole] = value;
}
void pop() {
const State value = storage_[--size_];
if (size_ == 0) return;
std::size_t hole = 0;
for (;;) {
auto child = hole * 2 + 1;
if (child >= size_) break;
if (child + 1 < size_ && Compare{}(storage_[child], storage_[child + 1])) ++child;
if (!Compare{}(value, storage_[child])) break;
storage_[hole] = storage_[child];
hole = child;
}
storage_[hole] = value;
}
std::span<const State> states() const noexcept { return {storage_.data(), size_}; }
};
// 固定配列を選ぶ版は緩和の65件目でradixへ移るため、66件目は入らない。
std::conditional_t<UseBounded, BoundedQueue, InitialQueue> queue;
distance[start_index] = 0;
queue.emplace(initial_distance, initial_vertex);
const auto relax = [&] {
if constexpr (!DeferPrevious) {
return [&]<bool StopWhenLarge>
#if defined(__clang__)
[[gnu::always_inline]]
#endif
(auto& pending, std::int64_t current, int from, std::size_t begin = 0) -> std::size_t {
auto* const distance_data = distance.data();
auto* const previous_data = previous.data();
const auto& edges = graph[from];
const auto end = edges.end();
for (auto cursor = edges.begin() + begin; cursor != end; ++cursor)
{
const auto& edge = *cursor;
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (!std::is_same_v<GraphLike, DijkstraCsrGraph>) {
if (edge.to < 0 || edge.to >= n)
throw std::out_of_range("Dijkstra: invalid destination vertex");
if (edge.w < 0 || edge.w > max_weight)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
}
#endif
const std::int64_t candidate = current + edge.w;
if constexpr (MayOverflow) if (candidate >= kDistanceLimit) [[unlikely]]
{
if (distance_data[edge.to] == kDistanceLimit) overflow_targets.push_back(edge.to);
continue;
}
if (candidate >= distance_data[edge.to])
continue;
distance_data[edge.to] = candidate;
if constexpr (RestorePath)
previous_data[edge.to] = from;
pending.emplace(candidate, edge.to);
if constexpr (StopWhenLarge) if (pending.size() > 64) return static_cast<std::size_t>(cursor - edges.begin()) + 1;
}
return edges.size();
};
} else {
return [&]<bool StopWhenLarge>
#if defined(__clang__)
[[gnu::always_inline]]
#endif
(auto& pending, std::int64_t current, int from, std::size_t begin = 0) -> std::size_t {
auto* const distance_data = distance.data();
const auto& edges = graph[from];
const auto end = edges.end();
for (auto cursor = edges.begin() + begin; cursor != end; ++cursor)
{
const auto& edge = *cursor;
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (!std::is_same_v<GraphLike, DijkstraCsrGraph>) {
if (edge.to < 0 || edge.to >= n)
throw std::out_of_range("Dijkstra: invalid destination vertex");
if (edge.w < 0 || edge.w > max_weight)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
}
#endif
const std::int64_t candidate = current + edge.w;
if constexpr (MayOverflow) if (candidate >= kDistanceLimit) [[unlikely]]
{
if (distance_data[edge.to] == kDistanceLimit) overflow_targets.push_back(edge.to);
continue;
}
if (candidate >= distance_data[edge.to])
continue;
distance_data[edge.to] = candidate;
record_previous(edge.to, from);
pending.emplace(candidate, edge.to);
if constexpr (StopWhenLarge) if (pending.size() > 64) return static_cast<std::size_t>(cursor - edges.begin()) + 1;
}
return edges.size();
};
}
}();
while (!queue.empty()) {
auto [current, from] = queue.top(); queue.pop();
if (current != distance[from]) continue;
while (queue.empty() && graph[from].size() == 1) {
const auto& edge = graph[from].front();
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (!std::is_same_v<GraphLike, DijkstraCsrGraph>) {
if (edge.to < 0 || edge.to >= n)
throw std::out_of_range("Dijkstra: invalid destination vertex");
if (edge.w < 0 || edge.w > max_weight)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
}
#endif
const auto candidate = current + edge.w;
if constexpr (MayOverflow) if (candidate >= kDistanceLimit) {
if (distance[edge.to] == kDistanceLimit) overflow_targets.push_back(edge.to);
break;
}
if (candidate >= distance[edge.to]) break;
distance[edge.to] = candidate;
if constexpr (RestorePath) {
if constexpr (DeferPrevious) record_previous(edge.to, from);
else previous[edge.to] = from;
}
current = candidate; from = edge.to;
}
std::size_t resume = graph[from].size();
if (graph[from].size() != 1 || !queue.empty())
resume = relax.template operator()<true>(queue, current, from);
if (queue.size() > 64) {
if constexpr (DeferPrevious) commit_previous();
if constexpr (PruneTerminal) {
DijkstraRadixFinish<RestorePath, MayOverflow, kDistanceLimit>(
graph, distance, previous, overflow_targets, queue.states(), current, from, resume);
} else {
DijkstraRadixHeap radix(static_cast<std::uint64_t>(current));
// 初期キューを取り出し順に並べ直す必要はない。
for (const auto& value : queue.states())
radix.emplace(value.first, value.second);
if (resume < graph[from].size())
relax.template operator()<false>(radix, current, from, resume);
while (!radix.empty()) {
const auto [key, vertex] = radix.pop();
const auto value = static_cast<std::int64_t>(key);
if (value == distance[vertex]) relax.template operator()<false>(radix, value, vertex);
}
}
break;
}
}
if constexpr (RestorePath)
{
if constexpr (RecordInitialChain) {
if (!target_in_prefix) {
if (distance[target_index] == unreachable) result.clear();
else {
const auto prefix_size = result.size();
if (!previous.empty()) {
for (int vertex = target_index; vertex != initial_vertex; vertex = previous[vertex])
result.push_back(vertex);
} else {
for (int vertex = target_index; vertex != initial_vertex;) {
result.push_back(vertex);
for (std::size_t i = previous_record_count; i != 0; --i) {
if (previous_records[i - 1].vertex == vertex) {
vertex = previous_records[i - 1].parent;
break;
}
}
}
}
std::reverse(result.begin() + static_cast<std::ptrdiff_t>(prefix_size), result.end());
}
}
} else if (distance[target_index] != unreachable)
{
for (int vertex = target_index; vertex != -1; vertex = previous[vertex])
result.push_back(vertex);
std::reverse(result.begin(), result.end());
}
}
if constexpr (MayOverflow) for (const int vertex : overflow_targets)
if (distance[vertex] == kDistanceLimit)
throw std::overflow_error("Dijkstra: finite distance collides with INF");
#ifndef SKIP_RUNTIME_CHECKS
distances.swap(local_distance);
#endif
if constexpr (RestorePath)
path->swap(result);
}
// 始点から一本道を処理する版だけを別の作業領域に展開する。
template <bool RestorePath, bool MayOverflow, bool PruneTerminal, std::int64_t kDistanceLimit = kInf, class GraphLike = WGraph>
#if defined(__GNUC__) || defined(__clang__)
[[gnu::noinline]]
#endif
inline void DijkstraInitialChain(const GraphLike& graph, std::vector<std::int64_t>& distances,
int start_index, int target_index, std::vector<int>* path) {
DijkstraBigImpl<RestorePath, MayOverflow, PruneTerminal, true, kDistanceLimit>(
graph, distances, start_index, target_index, path);
}
template <bool RestorePath, bool MayOverflow, bool PruneTerminal = true, std::int64_t kDistanceLimit = kInf, class GraphLike = WGraph>
#if defined(__GNUC__) || defined(__clang__)
[[gnu::noinline]]
#endif
inline void DijkstraLarge(const GraphLike& graph, std::vector<std::int64_t>& distances,
int start_index, int target_index, std::vector<int>* path) {
if constexpr (RestorePath && PruneTerminal) {
#ifndef SKIP_RUNTIME_CHECKS
const bool valid_start = start_index >= 0 && static_cast<std::size_t>(start_index) < graph.size();
#else
constexpr bool valid_start = true;
#endif
if (graph.size() > 4096 && valid_start && graph[start_index].size() == 1) {
DijkstraInitialChain<RestorePath, MayOverflow, PruneTerminal, kDistanceLimit>(
graph, distances, start_index, target_index, path);
return;
}
}
DijkstraBigImpl<RestorePath, MayOverflow, PruneTerminal, false, kDistanceLimit>(graph, distances, start_index, target_index, path);
}
// 共通処理。距離のみの場合、経路用の確保と更新をコンパイル時に除く。
template <bool RestorePath, class GraphLike = WGraph>
inline void DijkstraImpl(const GraphLike &graph, std::vector<std::int64_t> &distances,
int start_index, int target_index, std::vector<int> *path)
{
#ifndef SKIP_RUNTIME_CHECKS
[[maybe_unused]] constexpr std::int64_t max_weight = 1'000'000'000;
#endif
if (graph.size() > static_cast<std::size_t>(std::numeric_limits<int>::max()))
throw std::length_error("Dijkstra: too many vertices");
#ifndef SKIP_RUNTIME_CHECKS
[[maybe_unused]] const int n = static_cast<int>(graph.size());
#endif
#ifndef SKIP_RUNTIME_CHECKS
if (start_index < 0 || start_index >= n ||
(RestorePath && (target_index < 0 || target_index >= n)))
throw std::out_of_range("Dijkstra: invalid start or target vertex");
#endif
// ここは256頂点以下。全単純路がkInf未満なので出力変換は不要。
constexpr std::int64_t unreachable = kInf;
#ifndef SKIP_RUNTIME_CHECKS
std::vector<std::int64_t> local_distance;
auto& distance = local_distance;
#else
auto& distance = distances;
#endif
distance.assign(graph.size(), kInf);
std::vector<int> previous, result;
if constexpr (RestorePath)
previous.assign(graph.size(), -1);
#if defined(__clang__) && !defined(SIRKIT_DIJKSTRA_STANDARD_HEAP)
// 256頂点以下・重み<=1e9なら距離は38bit以内。下位8bitに頂点を詰める。
using State = std::uint64_t;
#else
using State = std::pair<std::int64_t, int>;
#endif
#if !defined(__clang__) || defined(SIRKIT_DIJKSTRA_STANDARD_HEAP)
struct Compare {
bool operator()(const State& a, const State& b) const noexcept {
return a.first > b.first || (a.first == b.first && a.second > b.second);
}
};
std::priority_queue<State, std::vector<State>, Compare> queue;
#else
struct Queue {
std::vector<State> values;
bool empty() const noexcept { return values.empty(); }
const State& top() const noexcept { return values.front(); }
void emplace(State value) {
auto hole = values.size();
values.push_back(value);
auto* const data = values.data();
while (hole != 0) {
const auto parent = (hole - 1) / 2;
if (data[parent] <= value) break;
data[hole] = data[parent];
hole = parent;
}
data[hole] = value;
}
void pop() {
if (values.size() == 1) { values.pop_back(); return; }
const auto value = values.back();
values.pop_back();
const auto count = values.size();
auto* const data = values.data();
std::size_t hole = 0;
while (hole < count / 2) {
auto child = hole * 2 + 1;
if (child + 1 < count && data[child + 1] < data[child]) ++child;
if (value <= data[child]) break;
data[hole] = data[child];
hole = child;
}
data[hole] = value;
}
};
Queue queue;
#endif
distance[start_index] = 0;
#if defined(__clang__) && !defined(SIRKIT_DIJKSTRA_STANDARD_HEAP)
queue.emplace(static_cast<State>(start_index));
#else
queue.emplace(0, start_index);
#endif
while (!queue.empty())
{
#if defined(__clang__) && !defined(SIRKIT_DIJKSTRA_STANDARD_HEAP)
const auto key = queue.top();
const auto current = static_cast<std::int64_t>(key >> 8);
const int from = static_cast<int>(key & 255);
#else
const auto [current, from] = queue.top();
#endif
queue.pop();
if (current != distance[from])
continue;
for (const auto &edge : graph[from])
{
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (!std::is_same_v<GraphLike, DijkstraCsrGraph>) {
if (edge.to < 0 || edge.to >= n)
throw std::out_of_range("Dijkstra: invalid destination vertex");
if (edge.w < 0 || edge.w > max_weight)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
}
#endif
const std::int64_t candidate = current + edge.w;
if (candidate >= distance[edge.to])
continue;
distance[edge.to] = candidate;
if constexpr (RestorePath)
previous[edge.to] = from;
#if defined(__clang__) && !defined(SIRKIT_DIJKSTRA_STANDARD_HEAP)
queue.emplace((static_cast<State>(candidate) << 8) | static_cast<State>(edge.to));
#else
queue.emplace(candidate, edge.to);
#endif
}
}
if constexpr (RestorePath)
{
if (distance[target_index] != unreachable)
{
for (int vertex = target_index; vertex != -1; vertex = previous[vertex])
result.push_back(vertex);
std::reverse(result.begin(), result.end());
}
}
#ifndef SKIP_RUNTIME_CHECKS
distances.swap(local_distance);
#endif
if constexpr (RestorePath)
path->swap(result);
}
// 任意の番兵用。非負の候補を符号なしで加算し、範囲外の最短距離だけを報告する。
template <bool RestorePath, class GraphLike>
inline void DijkstraCustomInf(const GraphLike& graph, std::vector<std::int64_t>& distances,
int start_index, int target_index, std::vector<int>* path, std::int64_t inf)
{
if (graph.size() > static_cast<std::size_t>(std::numeric_limits<int>::max()))
throw std::length_error("Dijkstra: too many vertices");
#ifndef SKIP_RUNTIME_CHECKS
const int n = static_cast<int>(graph.size());
if (start_index < 0 || start_index >= n ||
(RestorePath && (target_index < 0 || target_index >= n)))
throw std::out_of_range("Dijkstra: invalid start or target vertex");
std::vector<std::int64_t> local_distance;
auto& distance = local_distance;
#else
auto& distance = distances;
#endif
distance.assign(graph.size(), inf);
std::vector<int> previous, result, overflow_targets;
if constexpr (RestorePath) previous.assign(graph.size(), -1);
using State = std::pair<std::int64_t, int>;
std::priority_queue<State, std::vector<State>, std::greater<State>> queue;
distance[start_index] = 0;
queue.emplace(0, start_index);
while (!queue.empty()) {
const auto [current, from] = queue.top();
queue.pop();
if (current != distance[from]) continue;
for (const auto& edge : graph[from]) {
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (!std::is_same_v<GraphLike, DijkstraCsrGraph>) {
if (edge.to < 0 || edge.to >= n)
throw std::out_of_range("Dijkstra: invalid destination vertex");
if (edge.w < 0 || edge.w > 1'000'000'000)
throw std::out_of_range("Dijkstra: weight must be in [0, 1e9]");
}
#endif
const auto candidate = static_cast<std::uint64_t>(current) + static_cast<std::uint64_t>(edge.w);
if (candidate >= static_cast<std::uint64_t>(inf)) {
if (distance[edge.to] == inf) overflow_targets.push_back(edge.to);
continue;
}
if (candidate >= static_cast<std::uint64_t>(distance[edge.to])) continue;
distance[edge.to] = static_cast<std::int64_t>(candidate);
if constexpr (RestorePath) previous[edge.to] = from;
queue.emplace(distance[edge.to], edge.to);
}
}
for (const int vertex : overflow_targets)
if (distance[vertex] == inf)
throw std::overflow_error("Dijkstra: finite distance collides with inf");
if constexpr (RestorePath) {
if (distance[target_index] != inf) {
for (int vertex = target_index; vertex != -1; vertex = previous[vertex]) result.push_back(vertex);
std::reverse(result.begin(), result.end());
}
}
#ifndef SKIP_RUNTIME_CHECKS
distances.swap(local_distance);
#endif
if constexpr (RestorePath) path->swap(result);
}
// 最大辺重みから走査なしで有限距離の上界を判定する。
template <std::int64_t kDistanceLimit = kInf>
constexpr bool DijkstraMayOverflow(std::size_t vertices) noexcept {
static_assert(0 < kDistanceLimit && kDistanceLimit <= kInf);
return vertices > static_cast<std::uint64_t>((kDistanceLimit - 1) / 1'000'000'000);
}
template <bool RestorePath, std::int64_t kDistanceLimit = kInf, class GraphLike>
#if defined(__GNUC__) || defined(__clang__)
[[gnu::always_inline]]
#endif
inline void DijkstraDefaultLarge(const GraphLike& graph, std::vector<std::int64_t>& distances,
int start_index, int target_index, std::vector<int>* path) {
#if defined(__GNUC__) && !defined(__clang__)
// GCCの距離版は従来のカーネルを維持する。
constexpr bool prune_terminal = RestorePath;
#else
constexpr bool prune_terminal = true;
#endif
if (!DijkstraMayOverflow<kDistanceLimit>(graph.size()))
DijkstraLarge<RestorePath, false, prune_terminal, kDistanceLimit>(graph, distances, start_index, target_index, path);
else
DijkstraLarge<RestorePath, true, prune_terminal, kDistanceLimit>(graph, distances, start_index, target_index, path);
}
} // namespace reinsirk::detail
inline void Dijkstra(const WGraph &graph, std::vector<std::int64_t> &distances,
int start_index, std::int64_t inf = (std::int64_t{1} << 60))
{
if (inf <= 0) throw std::invalid_argument("Dijkstra: inf must be positive");
if (inf != kInf) return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraCustomInf<false>(
graph, distances, start_index, -1, nullptr, inf);
if (graph.size() <= 256) /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraImpl<false>(graph, distances, start_index, -1, nullptr);
else if (graph.size() <= 4096)
/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraLarge<false, false, false>(graph, distances, start_index, -1, nullptr);
else /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraDefaultLarge<false>(graph, distances, start_index, -1, nullptr);
}
inline void Dijkstra(const WGraph &graph, std::vector<std::int64_t> &distances,
int start_index, int target_index, std::vector<int> &path,
std::int64_t inf = (std::int64_t{1} << 60))
{
if (inf <= 0) throw std::invalid_argument("Dijkstra: inf must be positive");
if (inf != kInf) return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraCustomInf<true>(
graph, distances, start_index, target_index, &path, inf);
if (graph.size() <= 256) /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraImpl<true>(graph, distances, start_index, target_index, &path);
else if (graph.size() <= 4096)
/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraLarge<true, false, false>(graph, distances, start_index, target_index, &path);
else /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraDefaultLarge<true>(graph, distances, start_index, target_index, &path);
}
// 辺列入力版も全頂点の距離を返す。CSR構築後は上と同じengineを使う。
inline void Dijkstra(int n, std::span<const std::tuple<int,int,std::int64_t>> edges, std::vector<std::int64_t> &distances,
int start_index, std::int64_t inf = (std::int64_t{1} << 60))
{
if (inf <= 0) throw std::invalid_argument("Dijkstra: inf must be positive");
const /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraCsrGraph graph(n,edges);
if (inf != kInf) return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraCustomInf<false>(
graph, distances, start_index, -1, nullptr, inf);
if (graph.size() <= 256) /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraImpl<false>(graph, distances, start_index, -1, nullptr);
else if (graph.size() <= 4096)
/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraLarge<false, false, false>(graph, distances, start_index, -1, nullptr);
else /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraDefaultLarge<false>(graph, distances, start_index, -1, nullptr);
}
inline void Dijkstra(int n, std::span<const std::tuple<int,int,std::int64_t>> edges, std::vector<std::int64_t> &distances,
int start_index, int target_index, std::vector<int> &path,
std::int64_t inf = (std::int64_t{1} << 60))
{
if (inf <= 0) throw std::invalid_argument("Dijkstra: inf must be positive");
const /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraCsrGraph graph(n,edges);
if (inf != kInf) return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraCustomInf<true>(
graph, distances, start_index, target_index, &path, inf);
if (graph.size() <= 256) /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraImpl<true>(graph, distances, start_index, target_index, &path);
else if (graph.size() <= 4096)
/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraLarge<true, false, false>(graph, distances, start_index, target_index, &path);
else /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::DijkstraDefaultLarge<true>(graph, distances, start_index, target_index, &path);
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1