競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
複数の始点をグループに分けて、各頂点に近いグループを二つ求めます。あるグループを除いた最短距離が欲しいときは、除外対象と異なる方を距離順に選べば求まります。
two_nearest_source_groups(n, edges, sources, undirected=false) に、頂点数、辺 {from, to, weight}、始点 {vertex, group} を渡します。辺の重みは非負の uint64_t です。頂点番号は 0 始まりで、グループ番号は任意の int です。有向グラフでは始点から各頂点に向かう距離を求めます。
result[v][0] と result[v][1] は頂点 v に近い順の候補です。各候補に distance、始点の頂点番号 source、group が入ります。距離が等しいときの順番は未規定です。候補が足りない場合は nullopt が入ります。
辺数を m、始点数を s とすると、領域は O(n + m + s) です。全辺の重みが 1 なら時間も O(n + m + s) で、それ以外は O(n + (m + s) log(m + s + 1)) です。距離が uint64_t を超える候補は除外します。
使用例 · 重み付きの Verify · 重みが 1 の Verify
ShortestPathEdge{from,to,weight}: 重みが非負の uint64_t の辺。
GroupedShortestPathSource{vertex,group}: 始点の頂点 ID とグループ ID。
two_nearest_source_groups(n,edges,sources,undirected=false): 各頂点へ近い始点の上位2グループ。
各要素は距離順の optional<SourceGroupDistance> の長さ2の配列。
SourceGroupDistance{distance,source,group} は距離、始点の頂点 ID、グループ ID。
頂点は0始まり。グループ ID は任意の int。距離が同じ候補の順は未規定。
未到達・距離が uint64_t を超える候補は除く。始点数 s、x=m+s とする。
時間 O(n+x log(x+1))、全辺の重みが1なら O(n+x)。領域 O(n+x)。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Foreign Friends | two_nearest_source_groups.cpp | 未提出 | — |
| Hit and Away | two_nearest_source_groups_unit.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/two_nearest_source_groups.md
//
// API一覧
// ShortestPathEdge{from,to,weight}: 重みが非負の uint64_t の辺。
// GroupedShortestPathSource{vertex,group}: 始点の頂点 ID とグループ ID。
// two_nearest_source_groups(n,edges,sources,undirected=false): 各頂点へ近い始点の上位2グループ。
// 各要素は距離順の optional<SourceGroupDistance> の長さ2の配列。
// SourceGroupDistance{distance,source,group} は距離、始点の頂点 ID、グループ ID。
// 頂点は0始まり。グループ ID は任意の int。距離が同じ候補の順は未規定。
// 未到達・距離が uint64_t を超える候補は除く。始点数 s、x=m+s とする。
// 時間 O(n+x log(x+1))、全辺の重みが1なら O(n+x)。領域 O(n+x)。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <array>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <optional>
#include <queue>
#include <span>
#include <stdexcept>
#include <type_traits>
#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_TWO_NEAREST_SOURCE_GROUPS_DEFINED
#define SIRKIT_TWO_NEAREST_SOURCE_GROUPS_DEFINED
struct GroupedShortestPathSource
{
int vertex;
int group;
};
struct SourceGroupDistance
{
std::uint64_t distance;
int source;
int group;
};
namespace detail
{
template <bool Unit>
auto search_two_source_groups(const ShortestPathAdjacency &graph, std::span<const GroupedShortestPathSource> sources)
{
struct State
{
std::uint64_t distance;
int vertex;
int source;
int group;
};
struct Farther
{
bool operator()(const State &a, const State &b) const noexcept
{
return a.distance > b.distance;
}
};
using Queue = std::conditional_t<Unit, std::queue<State>, std::priority_queue<State, std::vector<State>, Farther>>;
Queue pending;
for (const auto &[vertex, group] : sources)
{
pending.emplace(0, vertex, vertex, group);
}
std::vector<std::array<std::optional<SourceGroupDistance>, 2>> result(graph.size());
while (!pending.empty())
{
const auto [distance, vertex, source, group] = [&]() -> State
{
if constexpr (Unit)
{
return pending.front();
}
else
{
return pending.top();
}
}();
pending.pop();
auto &[first, second] = result[vertex];
if (second || (first && first->group == group))
{
continue;
}
(first ? second : first) = SourceGroupDistance{distance, source, group};
for (const auto &[weight, next] : graph[vertex])
{
if constexpr (!Unit)
{
if (weight > std::numeric_limits<std::uint64_t>::max() - distance)
{
continue;
}
}
pending.emplace(distance + weight, next, source, group);
}
}
return result;
}
} // namespace detail
inline auto two_nearest_source_groups(
int n, std::span<const ShortestPathEdge> edges, std::span<const GroupedShortestPathSource> sources,
bool undirected = false)
{
#ifndef SKIP_RUNTIME_CHECKS
if (n < 0)
{
throw std::length_error("two_nearest_source_groups: negative vertex count");
}
for (const auto &source : sources)
{
if (source.vertex < 0 || source.vertex >= n)
{
throw std::out_of_range("two_nearest_source_groups: invalid source vertex");
}
}
#endif
detail::ShortestPathAdjacency graph(n);
bool unit = true;
for (const auto &[from, to, weight] : edges)
{
#ifndef SKIP_RUNTIME_CHECKS
if (from < 0 || from >= n || to < 0 || to >= n)
{
throw std::out_of_range("two_nearest_source_groups: invalid edge endpoint");
}
#endif
graph[from].emplace_back(weight, to);
if (undirected)
{
graph[to].emplace_back(weight, from);
}
unit = unit && weight == 1;
}
return unit ? detail::search_two_source_groups<true>(graph, sources)
: detail::search_two_source_groups<false>(graph, sources);
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1