kyopro-lib

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

View on GitHub

:x: test/yukicoder/1069/main.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/1069"
#define ERROR 1e-4

#include <cmath>
#include <iomanip>
#include <iostream>
#include "Mylib/Graph/ShortestPath/yen_algorithm.cpp"
#include "Mylib/IO/input_tuple_vector.cpp"
#include "Mylib/IO/input_tuples.cpp"

namespace hl = haar_lib;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int N, M, K, X, Y;
  std::cin >> N >> M >> K >> X >> Y;
  --X, --Y;

  auto [p, q] = hl::input_tuple_vector<long double, long double>(N);

  hl::graph<long double> g(N);
  for (auto [P, Q] : hl::input_tuples<int, int>(M)) {
    --P, --Q;

    long double dx = p[P] - p[Q];
    long double dy = q[P] - q[Q];

    long double L = std::sqrt(dx * dx + dy * dy);

    g.add_undirected(P, Q, L);
  }

  auto res = hl::yen_algorithm(g, X, Y, K);

  for (auto x : res) {
    if (not x) {
      std::cout << -1 << "\n";
    } else {
      std::cout << std::fixed << std::setprecision(12) << x->first << "\n";
    }
  }

  return 0;
}
#line 1 "test/yukicoder/1069/main.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1069"
#define ERROR 1e-4

#include <cmath>
#include <iomanip>
#include <iostream>
#line 2 "Mylib/Graph/ShortestPath/yen_algorithm.cpp"
#include <functional>
#include <optional>
#include <queue>
#include <utility>
#include <vector>
#line 4 "Mylib/Graph/Template/graph.cpp"

namespace haar_lib {
  template <typename T>
  struct edge {
    int from, to;
    T cost;
    int index = -1;
    edge() {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    edge(int from, int to, T cost, int index) : from(from), to(to), cost(cost), index(index) {}
  };

  template <typename T>
  struct graph {
    using weight_type = T;
    using edge_type   = edge<T>;

    std::vector<std::vector<edge<T>>> data;

    auto& operator[](size_t i) { return data[i]; }
    const auto& operator[](size_t i) const { return data[i]; }

    auto begin() const { return data.begin(); }
    auto end() const { return data.end(); }

    graph() {}
    graph(int N) : data(N) {}

    bool empty() const { return data.empty(); }
    int size() const { return data.size(); }

    void add_edge(int i, int j, T w, int index = -1) {
      data[i].emplace_back(i, j, w, index);
    }

    void add_undirected(int i, int j, T w, int index = -1) {
      add_edge(i, j, w, index);
      add_edge(j, i, w, index);
    }

    template <size_t I, bool DIRECTED = true, bool WEIGHTED = true>
    void read(int M) {
      for (int i = 0; i < M; ++i) {
        int u, v;
        std::cin >> u >> v;
        u -= I;
        v -= I;
        T w = 1;
        if (WEIGHTED) std::cin >> w;
        if (DIRECTED)
          add_edge(u, v, w, i);
        else
          add_undirected(u, v, w, i);
      }
    }
  };

  template <typename T>
  using tree = graph<T>;
}  // namespace haar_lib
#line 8 "Mylib/Graph/ShortestPath/yen_algorithm.cpp"

namespace haar_lib {
  namespace yen_algorithm_impl {
    template <typename T>
    auto shortest_path(
        const graph<T> &g,
        int from,
        int t,
        const std::vector<bool> &usable,
        const std::vector<std::vector<bool>> &valid) {
      using Path = std::pair<T, std::vector<int>>;
      using P    = std::pair<T, int>;

      const int N = g.size();
      std::vector<bool> visited(N, false);
      std::vector<std::optional<T>> dist(N);
      std::vector<std::pair<int, int>> restore(N);

      std::priority_queue<P, std::vector<P>, std::greater<P>> pq;

      dist[from] = 0;
      pq.emplace(0, from);

      while (not pq.empty()) {
        auto [d, i] = pq.top();
        pq.pop();

        if (visited[i]) continue;
        visited[i] = true;

        for (int k = 0; k < (int) g[i].size(); ++k) {
          if (not valid[i][k] or not usable[g[i][k].to]) continue;
          auto &e = g[i][k];

          if (not dist[e.to] or *dist[e.to] > d + e.cost) {
            dist[e.to]    = d + e.cost;
            restore[e.to] = std::make_pair(i, k);
            if (not visited[e.to]) pq.emplace(*dist[e.to], e.to);
          }
        }
      }

      std::optional<Path> ret;

      if (dist[t]) {
        std::vector<int> p;

        int cur = t;
        while (cur != from) {
          auto [i, j] = restore[cur];
          p.push_back(j);
          cur = i;
        }

        std::reverse(p.begin(), p.end());

        ret = std::make_pair(*dist[t], p);
      }

      return ret;
    }
  }  // namespace yen_algorithm_impl

  template <typename T>
  auto yen_algorithm(const graph<T> &g, int s, int t, int K) {
    using Path = std::pair<T, std::vector<int>>;

    const int N = g.size();

    std::vector<std::vector<bool>> valid(N);
    std::vector<std::optional<Path>> result(K);
    std::priority_queue<Path, std::vector<Path>, std::greater<Path>> stock;

    for (int i = 0; i < N; ++i) {
      valid[i].assign(g[i].size(), true);
    }

    for (int i = 0; i < K; ++i) {
      if (i == 0) {
        std::vector<bool> usable(N, true);
        if (auto res = yen_algorithm_impl::shortest_path(g, s, t, usable, valid); res) stock.push(*res);
      } else {
        std::vector<int> prev_path;

        {
          int cur = s;
          for (auto u : result[i - 1]->second) {
            prev_path.push_back(cur);
            cur = g[cur][u].to;
          }
          prev_path.push_back(t);
        }

        std::vector<bool> check(i, true);
        std::vector<bool> usable(N, true);

        for (int k = 0; k < (int) prev_path.size() - 1; ++k) {
          const int u = prev_path[k];

          for (int j = 0; j < i; ++j) {
            if (check[j]) {
              valid[prev_path[k]][result[j]->second[k]] = false;
            }
          }

          if (auto res = yen_algorithm_impl::shortest_path(g, u, t, usable, valid); res) {
            auto [c, p] = *res;

            std::vector<int> temp;
            for (int j = 0; j < k; ++j) {
              int v = result[i - 1]->second[j];

              c += g[prev_path[j]][v].cost;
              temp.push_back(v);
            }

            temp.insert(temp.end(), p.begin(), p.end());
            stock.emplace(c, temp);
          }

          usable[u] = false;

          for (int j = 0; j < i; ++j) {
            if (check[j]) {
              valid[prev_path[k]][result[j]->second[k]] = true;
            }
          }

          for (int j = 0; j < i; ++j) {
            if (check[j]) {
              if (prev_path[k + 1] != g[prev_path[k]][result[j]->second[k]].to) {
                check[j] = false;
              }
            }
          }
        }
      }

      if (stock.empty()) break;

      result[i] = stock.top();
      stock.pop();

      while (not stock.empty() and stock.top() == result[i]) {
        stock.pop();
      }
    }

    return result;
  }
}  // namespace haar_lib
#line 2 "Mylib/IO/input_tuple_vector.cpp"
#include <initializer_list>
#line 4 "Mylib/IO/input_tuple_vector.cpp"
#include <tuple>
#line 7 "Mylib/IO/input_tuple_vector.cpp"

namespace haar_lib {
  template <typename T, size_t... I>
  void input_tuple_vector_init(T &val, int N, std::index_sequence<I...>) {
    (void) std::initializer_list<int>{(void(std::get<I>(val).resize(N)), 0)...};
  }

  template <typename T, size_t... I>
  void input_tuple_vector_helper(T &val, int i, std::index_sequence<I...>) {
    (void) std::initializer_list<int>{(void(std::cin >> std::get<I>(val)[i]), 0)...};
  }

  template <typename... Args>
  auto input_tuple_vector(int N) {
    std::tuple<std::vector<Args>...> ret;

    input_tuple_vector_init(ret, N, std::make_index_sequence<sizeof...(Args)>());
    for (int i = 0; i < N; ++i) {
      input_tuple_vector_helper(ret, i, std::make_index_sequence<sizeof...(Args)>());
    }

    return ret;
  }
}  // namespace haar_lib
#line 6 "Mylib/IO/input_tuple.cpp"

namespace haar_lib {
  template <typename T, size_t... I>
  static void input_tuple_helper(std::istream &s, T &val, std::index_sequence<I...>) {
    (void) std::initializer_list<int>{(void(s >> std::get<I>(val)), 0)...};
  }

  template <typename T, typename U>
  std::istream &operator>>(std::istream &s, std::pair<T, U> &value) {
    s >> value.first >> value.second;
    return s;
  }

  template <typename... Args>
  std::istream &operator>>(std::istream &s, std::tuple<Args...> &value) {
    input_tuple_helper(s, value, std::make_index_sequence<sizeof...(Args)>());
    return s;
  }
}  // namespace haar_lib
#line 8 "Mylib/IO/input_tuples.cpp"

namespace haar_lib {
  template <typename... Args>
  class InputTuples {
    struct iter {
      using value_type = std::tuple<Args...>;
      value_type value;
      bool fetched = false;
      int N, c = 0;

      value_type operator*() {
        if (not fetched) {
          std::cin >> value;
        }
        return value;
      }

      void operator++() {
        ++c;
        fetched = false;
      }

      bool operator!=(iter &) const {
        return c < N;
      }

      iter(int N) : N(N) {}
    };

    int N;

  public:
    InputTuples(int N) : N(N) {}

    iter begin() const { return iter(N); }
    iter end() const { return iter(N); }
  };

  template <typename... Args>
  auto input_tuples(int N) {
    return InputTuples<Args...>(N);
  }
}  // namespace haar_lib
#line 10 "test/yukicoder/1069/main.test.cpp"

namespace hl = haar_lib;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int N, M, K, X, Y;
  std::cin >> N >> M >> K >> X >> Y;
  --X, --Y;

  auto [p, q] = hl::input_tuple_vector<long double, long double>(N);

  hl::graph<long double> g(N);
  for (auto [P, Q] : hl::input_tuples<int, int>(M)) {
    --P, --Q;

    long double dx = p[P] - p[Q];
    long double dy = q[P] - q[Q];

    long double L = std::sqrt(dx * dx + dy * dy);

    g.add_undirected(P, Q, L);
  }

  auto res = hl::yen_algorithm(g, X, Y, K);

  for (auto x : res) {
    if (not x) {
      std::cout << -1 << "\n";
    } else {
      std::cout << std::fixed << std::setprecision(12) << x->first << "\n";
    }
  }

  return 0;
}
Back to top page