競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
始点から終点までの最短路が複数あるとき、通る頂点列が辞書順で最小の路を求めます。頂点列も同じなら、辺 ID 列が辞書順で最小の路が選ばれます。
lexicographic_shortest_path(n, edges, source, target, undirected=false) に頂点数、辺の列、始点、終点を渡します。辺は ShortestPathEdge{from, to, weight} です。頂点は 0 始まりで、重みは正の uint64_t です。
返り値には距離 distance、頂点列 vertices、入力順の辺 ID 列 edges が入ります。終点に届かない場合や距離が uint64_t を超える場合は nullopt です。
終点から逆向きに距離を求め、残りの距離が最短のままになる隣の頂点から最小番号のものを選びます。時間は O(n + m log(m + 1))、領域は O(n + m) です。
lexicographic_shortest_path(n,edges,source,target,undirected=false): 頂点列が辞書順最小の最短路。
ShortestPathEdge{from,to,weight} の端点は0始まり、重みは正の uint64_t。自己辺・多重辺可。
LexicographicShortestPath{distance,vertices,edges} の edges は入力順の辺 ID。
頂点列も同じなら辺 ID 列が辞書順最小。到達不能・距離の uint64_t 範囲超過は nullopt。
時間 O(n+m log(m+1))、領域 O(n+m)。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| 最短経路のうち辞書順最小 | lexicographic_shortest_path.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/lexicographic_shortest_path.md
//
// API一覧
// lexicographic_shortest_path(n,edges,source,target,undirected=false): 頂点列が辞書順最小の最短路。
// ShortestPathEdge{from,to,weight} の端点は0始まり、重みは正の uint64_t。自己辺・多重辺可。
// LexicographicShortestPath{distance,vertices,edges} の edges は入力順の辺 ID。
// 頂点列も同じなら辺 ID 列が辞書順最小。到達不能・距離の uint64_t 範囲超過は nullopt。
// 時間 O(n+m log(m+1))、領域 O(n+m)。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <optional>
#include <queue>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_SHORTEST_PATH_DISTANCE_DEFINED
#define SIRKIT_SHORTEST_PATH_DISTANCE_DEFINED
struct ShortestPathEdge
{
int from;
int to;
std::uint64_t weight;
};
namespace detail
{
using ShortestPathAdjacency = std::vector<std::vector<std::pair<std::uint64_t, int>>>;
inline std::vector<std::optional<std::uint64_t>> shortest_path_edge_distances(const ShortestPathAdjacency &graph, int source)
{
using State = std::pair<std::uint64_t, int>;
std::priority_queue<State, std::vector<State>, std::greater<State>> pending;
std::vector<std::optional<std::uint64_t>> distance(graph.size());
distance[source] = 0;
pending.emplace(0, source);
while (!pending.empty())
{
const auto [prefix, vertex] = pending.top();
pending.pop();
if (prefix != distance[vertex].value())
{
continue;
}
for (const auto &[weight, next] : graph[vertex])
{
if (weight > std::numeric_limits<std::uint64_t>::max() - prefix)
{
continue;
}
const std::uint64_t candidate = prefix + weight;
if (!distance[next] || candidate < distance[next].value())
{
distance[next] = candidate;
pending.emplace(candidate, next);
}
}
}
return distance;
}
} // namespace detail
#endif
#ifndef SIRKIT_LEXICOGRAPHIC_SHORTEST_PATH_DEFINED
#define SIRKIT_LEXICOGRAPHIC_SHORTEST_PATH_DEFINED
struct LexicographicShortestPath
{
std::uint64_t distance;
std::vector<int> vertices;
std::vector<std::size_t> edges;
};
inline std::optional<LexicographicShortestPath> lexicographic_shortest_path(
int n, std::span<const ShortestPathEdge> edges, int source, int target, bool undirected = false)
{
#ifndef SKIP_RUNTIME_CHECKS
if (source < 0 || source >= n || target < 0 || target >= n)
{
throw std::out_of_range("lexicographic_shortest_path: invalid terminal");
}
#endif
struct Arc
{
int to;
std::size_t id;
};
std::vector<std::vector<Arc>> graph(n);
detail::ShortestPathAdjacency reverse(n);
auto add = [&](int u, int v, std::uint64_t weight, std::size_t id)
{
graph[u].push_back({v, id});
reverse[v].emplace_back(weight, u);
};
for (std::size_t id = 0; id < edges.size(); ++id)
{
const auto [u, v, weight] = edges[id];
#ifndef SKIP_RUNTIME_CHECKS
if (u < 0 || u >= n || v < 0 || v >= n)
{
throw std::out_of_range("lexicographic_shortest_path: invalid endpoint");
}
if (weight == 0)
{
throw std::invalid_argument("lexicographic_shortest_path: weight must be positive");
}
#endif
add(u, v, weight, id);
if (undirected && u != v)
{
add(v, u, weight, id);
}
}
const auto distances = detail::shortest_path_edge_distances(reverse, target);
if (!distances[source])
{
return std::nullopt;
}
LexicographicShortestPath result{*distances[source], {source}, {}};
int u = source;
while (u != target)
{
// 正の重みにより、最短路を保つ候補のどれに進んでも終点までの距離が減る。
const auto remaining = *distances[u];
std::optional<std::pair<int, std::size_t>> next;
for (const auto &[v, id] : graph[u])
{
const auto weight = edges[id].weight;
if (distances[v] && weight <= remaining && *distances[v] == remaining - weight)
{
const auto candidate = std::pair{v, id};
if (!next || candidate < *next)
{
next = candidate;
}
}
}
const auto [v, id] = next.value();
result.vertices.push_back(v);
result.edges.push_back(id);
u = v;
}
return result;
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1