sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

出発時刻で変わる経路

Graph/Time_Dependent_Paths.cpp · ソース · 文書 Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

乗り物を待てるグラフで、最早到着時刻や終電に間に合う最遅出発時刻を求めます。本体

  • earliest_arrival_times(n, edges, source, start_time, arrival) は始点から各頂点に最も早く着く時刻を返します。arrival(i, t) は辺 i に時刻 t 以降で乗ったときの最早到着時刻です。
  • latest_departure_times(n, edges, target, deadline, departure) は各頂点から期限までに終点に着ける最遅出発時刻を返します。departure(i, t) は辺 i で時刻 t までに着ける最遅出発時刻です。

有向辺は $0$ 始まりの端点の組です。移動できない辺や到達できない頂点の時刻には std::nullopt を使います。早く着けば待てるので、乗車可能な時刻が早いほど最早到着は遅くならず、期限が遅いほど最遅出発は早まりません。この条件でダイクストラ法を適用します。移動関数を1回呼ぶ費用を $T$ として、$O(n+m \log(m+1)+mT)$ 時間、$O(n+m)$ 領域です。

使用例

時刻 3 に頂点 0 を発車し、時刻 5 に頂点 1 に着く便だけがあるとします。

const std::array edges{std::pair{0, 1}};
const auto board = [](std::size_t, std::int64_t ready) -> std::optional<std::int64_t> {
    return ready <= 3 ? std::optional<std::int64_t>(5) : std::nullopt;
};
const auto arrive = sirkit::earliest_arrival_times(2, edges, 0, 1, board);
// arrive[1] は 5。

使用例・最早到着の Verify・最遅出発の Verify

ソースのAPI注記
earliest_arrival_times(n,edges,source,start_time,arrival): 始点から全頂点への最早到着時刻。
  arrival(edge_id,time)はtime以降に乗るときの最早到着時刻、移動できなければnullopt。
latest_departure_times(n,edges,target,deadline,departure): 期限までに終点へ着く全頂点の最遅出発時刻。
  departure(edge_id,time)はtimeまでに着く最遅出発時刻、移動できなければnullopt。
  edgesは0始まりの(from,to)、移動関数と結果の時刻はoptional<int64_t>。
  早く到着すれば待てる。時刻を早めると最早到着が遅くならず、期限を延ばすと最遅出発が早まらないこと。
  コールバック1回の費用をTとし、O(n+m log(m+1)+mT)時間、O(n+m)領域。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Train time_dependent_paths_earliest.cpp 未提出 —
Last Train time_dependent_paths_latest.cpp 未提出 —

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 無し
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

  • Train time_dependent_paths_earliest.cpp
  • Last Train time_dependent_paths_latest.cpp

Code

// SPDX-License-Identifier: CC0-1.0
// 参考(時刻を距離として扱うダイクストラ法): https://atcoder.jp/contests/ABC192/editorial/764
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/time_dependent_paths.md
//
// API一覧
// earliest_arrival_times(n,edges,source,start_time,arrival): 始点から全頂点への最早到着時刻。
//   arrival(edge_id,time)はtime以降に乗るときの最早到着時刻、移動できなければnullopt。
// latest_departure_times(n,edges,target,deadline,departure): 期限までに終点へ着く全頂点の最遅出発時刻。
//   departure(edge_id,time)はtimeまでに着く最遅出発時刻、移動できなければnullopt。
//   edgesは0始まりの(from,to)、移動関数と結果の時刻はoptional<int64_t>。
//   早く到着すれば待てる。時刻を早めると最早到着が遅くならず、期限を延ばすと最遅出発が早まらないこと。
//   コールバック1回の費用をTとし、O(n+m log(m+1)+mT)時間、O(n+m)領域。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <cstdint>
#include <functional>
#include <numeric>
#include <optional>
#include <queue>
#include <span>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_TIME_DEPENDENT_PATHS_DEFINED
#define SIRKIT_TIME_DEPENDENT_PATHS_DEFINED

namespace detail
{
template <bool Reverse, class Transition>
std::vector<std::optional<std::int64_t>> search_timetable(
    int n, std::span<const std::pair<int, int>> edges, int start, std::int64_t start_time, Transition transition)
{
#ifndef SKIP_RUNTIME_CHECKS
    if (start < 0 || start >= n)
    {
        throw std::out_of_range("timetable: start vertex out of range");
    }
#endif
    std::vector<std::size_t> offsets(static_cast<std::size_t>(n) + 1);
    for (const auto &[from, to] : edges)
    {
#ifndef SKIP_RUNTIME_CHECKS
        if (from < 0 || from >= n || to < 0 || to >= n)
        {
            throw std::out_of_range("timetable: endpoint out of range");
        }
#endif
        ++offsets[static_cast<std::size_t>(Reverse ? to : from) + 1];
    }
    std::partial_sum(offsets.begin(), offsets.end(), offsets.begin());
    std::vector<std::size_t> ids(edges.size());
    {
        auto write = offsets;
        for (std::size_t i = 0; i < edges.size(); ++i)
        {
            const auto [from, to] = edges[i];
            ids[write[Reverse ? to : from]++] = i;
        }
    }

    using Entry = std::pair<std::int64_t, int>;
    using QueueOrder = std::conditional_t<Reverse, std::less<Entry>, std::greater<Entry>>;
    const std::conditional_t<Reverse, std::greater<std::int64_t>, std::less<std::int64_t>> better;
    std::priority_queue<Entry, std::vector<Entry>, QueueOrder> pending;
    std::vector<std::optional<std::int64_t>> times(n);
    times[start] = start_time;
    pending.emplace(start_time, start);
    while (!pending.empty())
    {
        const auto [time, u] = pending.top();
        pending.pop();
        if (time != *times[u])
        {
            continue;
        }
        const auto until = offsets[static_cast<std::size_t>(u) + 1];
        for (auto i = offsets[u]; i < until; ++i)
        {
            const auto id = ids[i];
            const std::optional<std::int64_t> next = std::invoke(transition, id, time);
            if (!next)
            {
                continue;
            }
#ifndef SKIP_RUNTIME_CHECKS
            if (better(*next, time))
            {
                throw std::invalid_argument("timetable: transition reverses time");
            }
#endif
            const int v = Reverse ? edges[id].first : edges[id].second;
            if (!times[v] || better(*next, *times[v]))
            {
                times[v] = *next;
                pending.emplace(*next, v);
            }
        }
    }
    return times;
}
} // namespace detail

template <class Arrival>
std::vector<std::optional<std::int64_t>> earliest_arrival_times(
    int n, std::span<const std::pair<int, int>> edges, int source, std::int64_t start_time, Arrival arrival)
{
    return detail::search_timetable<false>(n, edges, source, start_time, std::move(arrival));
}

template <class Departure>
std::vector<std::optional<std::int64_t>> latest_departure_times(
    int n, std::span<const std::pair<int, int>> edges, int target, std::int64_t deadline, Departure departure)
{
    return detail::search_timetable<true>(n, edges, target, deadline, std::move(departure));
}

#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page