Luzhiled's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ei1333/library

:heavy_check_mark: K-Shortest-Walk (graph/shortest-path/k-shortest-walk.hpp)

概要

頂点 $s$ から $t$ へのウォーク(Walk) のうち, 昇順 $k$ 個のウォークの長さを Eppstein’s Algorithm により求める.

ウォーク(Walk, 歩道, 経路) とは重複して頂点や辺が現れることを許容した頂点 $s$ から $t$ への経路を指す.

ちなみにトレイル(Trail) は同じ辺を通らない経路, 道(Path) は同じ頂点を通らない経路である.

計算量

Depends on

Verified with

Code

#pragma once

#include "../graph-template.hpp"

/**
 * @brief K-Shortest-Walk
 *
 * @see https://qiita.com/hotman78/items/42534a01c4bd05ed5e1e
 */
template <typename T>
vector<T> k_shortest_walk(const Graph<T> &g, int s, int t, int k) {
  int N = (int)g.size();
  Graph<T> rg(N);
  for (int i = 0; i < N; i++) {
    for (auto &e : g[i]) rg.add_directed_edge(e.to, i, e.cost);
  }
  auto dist = dijkstra(rg, t);
  if (dist.from[s] == -1) return {};
  auto &p = dist.dist;
  vector<vector<int> > ch(N);
  for (int i = 0; i < N; i++) {
    if (dist.from[i] >= 0) ch[dist.from[i]].emplace_back(i);
  }
  using PHeap = PersistentLeftistHeap<T>;
  using Node = typename PHeap::Node;
  PHeap heap;
  vector<Node *> h(N, heap.make_root());
  {
    queue<int> que;
    que.emplace(t);
    while (!que.empty()) {
      int idx = que.front();
      que.pop();
      if (dist.from[idx] >= 0) {
        h[idx] = heap.meld(h[idx], h[dist.from[idx]]);
      }
      bool used = true;
      for (auto &e : g[idx]) {
        if (e.to != t && dist.from[e.to] == -1) continue;
        if (used && dist.from[idx] == e.to && p[e.to] + e.cost == p[idx]) {
          used = false;
          continue;
        }
        h[idx] = heap.push(h[idx], e.cost - p[idx] + p[e.to], e.to);
      }
      for (auto &to : ch[idx]) que.emplace(to);
    }
  }
  using pi = pair<T, Node *>;
  auto comp = [](const pi &x, const pi &y) { return x.first > y.first; };
  priority_queue<pi, vector<pi>, decltype(comp)> que(comp);
  Node *root = heap.make_root();
  root = heap.push(root, p[s], s);
  que.emplace(p[s], root);
  vector<T> ans;
  while (!que.empty()) {
    T cost;
    Node *cur;
    tie(cost, cur) = que.top();
    que.pop();
    ans.emplace_back(cost);
    if ((int)ans.size() == k) break;
    if (cur->l) que.emplace(cost + cur->l->key - cur->key, cur->l);
    if (cur->r) que.emplace(cost + cur->r->key - cur->key, cur->r);
    if (h[cur->idx]) que.emplace(cost + h[cur->idx]->key, h[cur->idx]);
  }
  return ans;
}
#line 2 "graph/shortest-path/k-shortest-walk.hpp"

#line 2 "graph/graph-template.hpp"

/**
 * @brief Graph Template(グラフテンプレート)
 */
template <typename T = int>
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1)
      : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template <typename T = int>
struct Graph {
  vector<vector<Edge<T> > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  size_t size() const { return g.size(); }

  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(to, from, cost, es++);
  }

  void read(int M, int padding = -1, bool weighted = false,
            bool directed = false) {
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if (weighted) cin >> c;
      if (directed)
        add_directed_edge(a, b, c);
      else
        add_edge(a, b, c);
    }
  }

  inline vector<Edge<T> > &operator[](const int &k) { return g[k]; }

  inline const vector<Edge<T> > &operator[](const int &k) const { return g[k]; }
};

template <typename T = int>
using Edges = vector<Edge<T> >;
#line 4 "graph/shortest-path/k-shortest-walk.hpp"

/**
 * @brief K-Shortest-Walk
 *
 * @see https://qiita.com/hotman78/items/42534a01c4bd05ed5e1e
 */
template <typename T>
vector<T> k_shortest_walk(const Graph<T> &g, int s, int t, int k) {
  int N = (int)g.size();
  Graph<T> rg(N);
  for (int i = 0; i < N; i++) {
    for (auto &e : g[i]) rg.add_directed_edge(e.to, i, e.cost);
  }
  auto dist = dijkstra(rg, t);
  if (dist.from[s] == -1) return {};
  auto &p = dist.dist;
  vector<vector<int> > ch(N);
  for (int i = 0; i < N; i++) {
    if (dist.from[i] >= 0) ch[dist.from[i]].emplace_back(i);
  }
  using PHeap = PersistentLeftistHeap<T>;
  using Node = typename PHeap::Node;
  PHeap heap;
  vector<Node *> h(N, heap.make_root());
  {
    queue<int> que;
    que.emplace(t);
    while (!que.empty()) {
      int idx = que.front();
      que.pop();
      if (dist.from[idx] >= 0) {
        h[idx] = heap.meld(h[idx], h[dist.from[idx]]);
      }
      bool used = true;
      for (auto &e : g[idx]) {
        if (e.to != t && dist.from[e.to] == -1) continue;
        if (used && dist.from[idx] == e.to && p[e.to] + e.cost == p[idx]) {
          used = false;
          continue;
        }
        h[idx] = heap.push(h[idx], e.cost - p[idx] + p[e.to], e.to);
      }
      for (auto &to : ch[idx]) que.emplace(to);
    }
  }
  using pi = pair<T, Node *>;
  auto comp = [](const pi &x, const pi &y) { return x.first > y.first; };
  priority_queue<pi, vector<pi>, decltype(comp)> que(comp);
  Node *root = heap.make_root();
  root = heap.push(root, p[s], s);
  que.emplace(p[s], root);
  vector<T> ans;
  while (!que.empty()) {
    T cost;
    Node *cur;
    tie(cost, cur) = que.top();
    que.pop();
    ans.emplace_back(cost);
    if ((int)ans.size() == k) break;
    if (cur->l) que.emplace(cost + cur->l->key - cur->key, cur->l);
    if (cur->r) que.emplace(cost + cur->r->key - cur->key, cur->r);
    if (h[cur->idx]) que.emplace(cost + h[cur->idx]->key, h[cur->idx]);
  }
  return ans;
}
Back to top page