競技プログラミング用 C++ ライブラリ
test/dijkstra_bigint_yuki.test.cpp · GitHub で見る
#define PROBLEM "https://yukicoder.me/problems/no/3669"
#include "graph/dijkstra.hpp"
#include "components/fast_io.hpp"
#include <boost/multiprecision/cpp_int.hpp>
#include <numeric>
int main() {
FastInput input;
FastOutput output;
int n = 0, m = 0;
input.read(n), input.read(m);
struct Edge { int u, v, a, b; };
std::vector<Edge> edges(m);
int max_b = 1;
for (auto& e : edges) input.read(e.u), input.read(e.v), input.read(e.a), input.read(e.b), max_b = std::max(max_b, e.b);
using Integer = boost::multiprecision::cpp_int;
Integer denominator = 1;
for (int b = 2; b <= max_b; ++b) denominator *= b / std::gcd(b, int(Integer(denominator % b)));
std::vector<std::vector<std::pair<int, Integer>>> graph(n);
std::vector<Integer> unit(max_b + 1);
for (int b = 1; b <= max_b; ++b) unit[b] = denominator / b;
for (auto e : edges) {
Integer weight = unit[e.b] * e.a;
graph[e.u - 1].emplace_back(e.v - 1, weight);
graph[e.v - 1].emplace_back(e.u - 1, std::move(weight));
}
auto distance = dijkstra(graph, 0, Integer(denominator * (300LL * n + 1)));
for (int v = 1; v < n; ++v) {
Integer divisor = gcd(distance[v], denominator);
output.write(Integer(distance[v] / divisor).str());
output.write(' ');
output.println(Integer(denominator / divisor).str());
}
}
#line 1 "test/dijkstra_bigint_yuki.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/3669"
#line 1 "graph/dijkstra.hpp"
#line 1 "components/dijkstra_queue.hpp"
#line 1 "components/RadixHeap.hpp"
#include <algorithm>
#include <bit>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <utility>
#include <vector>
template <class Key = std::uint64_t, class T = int>
class RadixHeap {
static_assert(std::is_integral_v<Key> && !std::is_same_v<Key, bool> && sizeof(Key) <= 8);
using U = std::make_unsigned_t<Key>;
using State = std::pair<Key, T>;
std::vector<State> bucket[std::numeric_limits<U>::digits + 1];
Key lower;
std::size_t count = 0;
int index(Key key) const { return std::bit_width(U(U(key) ^ U(lower))); }
void refill() {
assert(count);
if (!bucket[0].empty()) return;
int i = 1;
while (bucket[i].empty()) ++i;
lower = bucket[i][0].first;
for (const auto &s : bucket[i]) lower = std::min(lower, s.first);
for (auto &s : bucket[i]) bucket[index(s.first)].push_back(std::move(s));
bucket[i].clear();
}
public:
explicit RadixHeap(Key minimum = 0) : lower(minimum) {}
RadixHeap(const RadixHeap &) = default;
RadixHeap &operator=(const RadixHeap &) = default;
RadixHeap(RadixHeap &&other) noexcept : lower(other.lower), count(std::exchange(other.count, 0)) {
for (int i = 0; i <= std::numeric_limits<U>::digits; ++i) bucket[i] = std::move(other.bucket[i]);
}
RadixHeap &operator=(RadixHeap &&other) noexcept {
if (this != &other) {
lower = other.lower;
count = std::exchange(other.count, 0);
for (int i = 0; i <= std::numeric_limits<U>::digits; ++i) bucket[i] = std::move(other.bucket[i]);
}
return *this;
}
bool empty() const { return count == 0; }
std::size_t size() const { return count; }
Key last() const { return lower; }
void clear(Key minimum = 0) {
for (auto &b : bucket) b.clear();
lower = minimum;
count = 0;
}
void push(State s) {
assert(s.first >= lower);
bucket[index(s.first)].push_back(std::move(s));
++count;
}
const State &top() {
refill();
return bucket[0].back();
}
void pop() {
refill();
bucket[0].pop_back();
--count;
}
State pop_value() {
refill();
State s = std::move(bucket[0].back());
bucket[0].pop_back();
--count;
return s;
}
};
#line 10 "components/dijkstra_queue.hpp"
#include <functional>
#line 15 "components/dijkstra_queue.hpp"
namespace shortest_path_detail {
template<class Weight, class Distance>
bool reaches_limit(const Weight &weight, const Distance &limit) {
if constexpr (std::numeric_limits<Weight>::is_integer && std::numeric_limits<Weight>::is_bounded &&
std::numeric_limits<Distance>::is_integer && std::numeric_limits<Distance>::is_bounded) {
using Compare = std::common_type_t<Weight, Distance>;
return Compare(weight) >= Compare(limit);
} else return weight >= limit;
}
template<class T> class RadixQueue {
static_assert(std::is_integral_v<T> && sizeof(T) <= 8);
struct State { T key; int vertex; };
State small_[32];
std::vector<std::uint64_t> buckets_[33];
std::uint32_t minimum_[33];
std::uint32_t occupied_ = 0;
std::uint64_t last_ = 0;
std::size_t count_ = 0;
int mode_ = 0;
RadixHeap<std::uint64_t> wide_;
void put(std::uint64_t state) {
std::uint32_t key = state >> 32;
int index = std::bit_width(key ^ std::uint32_t(last_));
buckets_[index].push_back(state);
if (index) {
occupied_ |= std::uint32_t(1) << (index - 1);
minimum_[index] = std::min(minimum_[index], key);
}
}
void promote() {
if (mode_ == 0) {
for (std::size_t i = 0; i < count_; ++i) wide_.push({std::uint64_t(small_[i].key), small_[i].vertex});
} else {
for (auto &bucket : buckets_) {
for (auto state : bucket) {
std::uint64_t key = last_ + std::uint32_t(std::uint32_t(state >> 32) - std::uint32_t(last_));
wide_.push({key, int(std::uint32_t(state))});
}
bucket.clear();
}
}
mode_ = 2;
}
public:
bool empty() const { return count_ == 0; }
void push(T key, int vertex) {
assert(key >= 0 && std::uint64_t(key) >= last_);
if (mode_ == 0 && count_ < 32) {
small_[count_++] = {key, vertex};
return;
}
if (mode_ == 0) {
bool narrow = std::uint64_t(key) - last_ <= std::uint64_t(std::numeric_limits<std::int32_t>::max());
for (std::size_t i = 0; i < count_; ++i)
narrow &= std::uint64_t(small_[i].key) - last_ <= std::uint64_t(std::numeric_limits<std::int32_t>::max());
if (narrow) {
std::fill_n(minimum_, 33, std::numeric_limits<std::uint32_t>::max());
for (std::size_t i = 0; i < count_; ++i)
put((std::uint64_t(std::uint32_t(small_[i].key)) << 32) | std::uint32_t(small_[i].vertex));
mode_ = 1;
} else promote();
}
if (mode_ == 1 && std::uint64_t(key) - last_ > std::uint64_t(std::numeric_limits<std::int32_t>::max())) promote();
if (mode_ == 2) wide_.push({std::uint64_t(key), vertex});
else put((std::uint64_t(std::uint32_t(key)) << 32) | std::uint32_t(vertex));
++count_;
}
std::pair<T, int> pop() {
assert(count_);
if (mode_ == 0) {
std::size_t best = 0;
for (std::size_t i = 1; i < count_; ++i)
if (small_[i].key < small_[best].key) best = i;
State state = small_[best];
small_[best] = small_[--count_];
last_ = std::uint64_t(state.key);
return {state.key, state.vertex};
}
if (mode_ == 2) {
auto state = wide_.pop_value();
last_ = state.first;
--count_;
return {T(state.first), state.second};
}
if (buckets_[0].empty()) {
int index = std::countr_zero(occupied_) + 1;
last_ += std::uint32_t(minimum_[index] - std::uint32_t(last_));
minimum_[index] = std::numeric_limits<std::uint32_t>::max();
occupied_ &= occupied_ - 1;
if (buckets_[index].size() == 1) {
auto state = buckets_[index].back();
buckets_[index].clear();
--count_;
return {T(last_), int(std::uint32_t(state))};
}
for (auto state : buckets_[index]) put(state);
buckets_[index].clear();
}
auto state = buckets_[0].back();
buckets_[0].pop_back();
--count_;
return {T(last_), int(std::uint32_t(state))};
}
};
template<class T> class BinaryQueue {
using State = std::pair<T, int>;
std::vector<State> heap_;
public:
bool empty() const { return heap_.empty(); }
void push(T cost, int vertex) {
heap_.emplace_back(std::move(cost), vertex);
std::push_heap(heap_.begin(), heap_.end(), std::greater<State>());
}
State pop() {
std::pop_heap(heap_.begin(), heap_.end(), std::greater<State>());
State state = std::move(heap_.back());
heap_.pop_back();
return state;
}
};
template<class T>
using DistanceQueue = std::conditional_t<std::is_integral_v<T> && sizeof(T) <= 8, RadixQueue<T>, BinaryQueue<T>>;
}
#line 1 "components/dijkstra_topological.hpp"
#line 11 "components/dijkstra_topological.hpp"
namespace shortest_path_detail {
template<bool Path, class G, class T>
std::vector<T> topological_run(const G &g, int start, int target, T inf, std::vector<int> &parent) {
std::size_t n = std::size_t(g.size());
assert(n <= static_cast<std::size_t>(std::numeric_limits<int>::max()));
assert(start >= 0 && static_cast<std::size_t>(start) < n);
assert(inf > 0);
std::vector<T> distance(n, inf);
std::vector<std::size_t> indegree(n);
std::vector<int> ready;
ready.reserve(n);
for (std::size_t v = 0; v < n; ++v)
for (const auto &[to, weight] : g[int(v)]) {
assert(to >= 0 && static_cast<std::size_t>(to) < n);
assert(weight >= 0);
++indegree[to];
}
for (std::size_t v = 0; v < n; ++v)
if (!indegree[v]) ready.push_back(int(v));
distance[start] = 0;
auto improve = [&](int from, const T &cost, const T &allowance, int to, const auto &weight) {
if (reaches_limit(weight, allowance)) return false;
T next = cost + weight;
if (next >= distance[to]) return false;
if constexpr (Path) {
if (next >= distance[target]) return false;
parent[to] = from;
}
distance[to] = std::move(next);
return true;
};
for (std::size_t head = 0; head < ready.size(); ++head) {
int v = ready[head];
if constexpr (Path) if (v == target) return distance;
T cost = distance[v];
bool active = cost < inf;
if constexpr (Path) active &= cost < distance[target];
T allowance = active ? inf - cost : T(0);
for (const auto &[to, weight] : g[v]) {
if (active) improve(v, cost, allowance, to, weight);
if (--indegree[to] == 0) ready.push_back(to);
}
}
DistanceQueue<T> queue;
for (std::size_t v = 0; v < n; ++v) {
if (!indegree[v] || distance[v] == inf || g[v].empty()) continue;
if constexpr (Path) if (int(v) == target || distance[v] >= distance[target]) continue;
queue.push(distance[v], int(v));
}
while (!queue.empty()) {
const auto [cost, v] = queue.pop();
if (cost != distance[v]) continue;
if constexpr (Path) if (cost >= distance[target]) break;
T allowance = inf - cost;
for (const auto &[to, weight] : g[v]) {
if (!improve(v, cost, allowance, to, weight)) continue;
if constexpr (Path) if (to == target) continue;
if (!g[to].empty()) queue.push(distance[to], to);
}
}
return distance;
}
}
#line 1 "components/dijkstra_unique.hpp"
#line 9 "components/dijkstra_unique.hpp"
namespace shortest_path_detail {
template<class G, class T>
std::pair<T, std::vector<int>> unique_path(const G &g, int st, int target, T inf, std::vector<int> &parent) {
using State = std::pair<T, int>;
std::vector<State> pending;
pending.emplace_back(T(0), st);
while (!pending.empty()) {
auto [cost, v] = std::move(pending.back());
pending.pop_back();
for (const auto &first : g[v]) {
const auto *edge = &first;
int from = v;
T prefix = cost;
for (;;) {
const auto &[to, weight] = *edge;
assert(weight >= 0);
if (to == st) break;
if (reaches_limit(weight, T(inf - prefix))) break;
T next = prefix + weight;
if (to == target) {
std::vector<int> path{to};
for (int u = from; u != st; u = parent[u]) path.push_back(u);
path.push_back(st);
std::reverse(path.begin(), path.end());
return {std::move(next), std::move(path)};
}
const auto &outgoing = g[to];
if (outgoing.empty()) break;
parent[to] = from;
if (outgoing.size() != 1) {
pending.emplace_back(std::move(next), to);
break;
}
from = to;
edge = &outgoing[0];
prefix = std::move(next);
}
}
}
return {T(-1), {}};
}
}
#line 1 "components/directed_csr.hpp"
#line 8 "components/directed_csr.hpp"
#include <memory>
#include <span>
#include <stdexcept>
#line 14 "components/directed_csr.hpp"
namespace directed_csr_detail { struct Access; }
template<class T>
class DirectedCSR {
public:
struct Edge { int from, to; T weight; };
using Arc = std::pair<int, T>;
using weight_type = T;
using distance_type = std::conditional_t<std::is_integral_v<T> && sizeof(T) < 8, std::int64_t, T>;
private:
friend struct directed_csr_detail::Access;
std::vector<std::size_t> offset_;
std::vector<Arc> arcs_;
int single_gap_ = -2;
bool unique_incoming_ = true;
void reset(int n, std::size_t m) {
assert(n >= 0);
offset_.assign(std::size_t(n) + 1, 0);
single_gap_ = -2;
unique_incoming_ = m <= std::size_t(n);
}
void count_edge(const Edge &edge, std::vector<std::uint64_t> &seen) {
assert(0 <= edge.from && edge.from < size() && 0 <= edge.to && edge.to < size());
++offset_[edge.from];
if (unique_incoming_) {
auto mask = std::uint64_t(1) << (edge.to % 64);
auto &word = seen[edge.to / 64];
unique_incoming_ = !(word & mask);
word |= mask;
}
}
void finish(std::span<const Edge> edges) {
const auto n = offset_.size() - 1;
single_gap_ = -2;
if (edges.size() >= n - (n != 0) && edges.size() <= n &&
std::all_of(offset_.begin(), offset_.end() - 1, [](std::size_t x) { return x <= 1; })) {
single_gap_ = -1;
if (edges.size() != n) single_gap_ = int(std::find(offset_.begin(), offset_.end() - 1, 0) - offset_.begin());
for (std::size_t v = 0; v <= n; ++v) offset_[v] = v - (single_gap_ >= 0 && v > std::size_t(single_gap_));
arcs_.resize(edges.size());
for (const auto &edge : edges) arcs_[edge.from - (single_gap_ >= 0 && edge.from > single_gap_)] = {edge.to, edge.weight};
return;
}
std::size_t active = offset_[0] != 0;
for (std::size_t v = 1; v < offset_.size(); ++v) {
active += offset_[v] != 0;
offset_[v] += offset_[v - 1];
}
arcs_.resize(edges.size());
std::size_t i = edges.size();
#if defined(__GNUC__) || defined(__clang__)
if (active >= 8192) {
for (; i > 32; --i) {
const auto from = edges[i - 33].from;
__builtin_prefetch(&offset_[from], 1, 3);
__builtin_prefetch(&arcs_[offset_[from] - 1], 1, 3);
const auto &edge = edges[i - 1];
arcs_[--offset_[edge.from]] = {edge.to, edge.weight};
}
}
#endif
for (; i; --i) {
const auto &edge = edges[i - 1];
arcs_[--offset_[edge.from]] = {edge.to, edge.weight};
}
}
void clear_edges() {
single_gap_ = -2;
unique_incoming_ = true;
std::fill(offset_.begin(), offset_.end(), 0);
arcs_.clear();
}
public:
explicit DirectedCSR(int n = 0) {
assert(n >= 0);
offset_.assign(std::size_t(n) + 1, 0);
}
DirectedCSR(int n, std::span<const Edge> edges) { build(n, edges); }
template<class Producer>
DirectedCSR(int n, std::size_t m, Producer &&produce) { build(n, m, std::forward<Producer>(produce)); }
void build(int n, std::span<const Edge> edges) {
reset(n, edges.size());
try {
std::vector<std::uint64_t> seen(unique_incoming_ ? (std::size_t(n) + 63) / 64 : 0);
for (const auto &edge : edges) count_edge(edge, seen);
finish(edges);
}
catch (...) { clear_edges(); throw; }
}
template<class Producer>
void build(int n, std::size_t m, Producer &&produce) {
reset(n, m);
try {
std::vector<std::uint64_t> seen(unique_incoming_ ? (std::size_t(n) + 63) / 64 : 0);
auto edges = std::make_unique_for_overwrite<Edge[]>(m);
std::size_t count = 0;
auto emit = [&](Edge edge) {
if (count == m) throw std::length_error("DirectedCSR edge count");
count_edge(edge, seen);
edges[count++] = std::move(edge);
};
std::forward<Producer>(produce)(emit);
if (count != m) throw std::length_error("DirectedCSR edge count");
finish(std::span<const Edge>(edges.get(), m));
} catch (...) { clear_edges(); throw; }
}
int size() const { return offset_.empty() ? 0 : int(offset_.size() - 1); }
bool empty() const { return size() == 0; }
std::size_t edge_count() const { return arcs_.size(); }
std::span<const Arc> operator[](int v) const {
assert(0 <= v && v < size());
return std::span<const Arc>(arcs_).subspan(offset_[v], offset_[v + 1] - offset_[v]);
}
};
namespace directed_csr_detail {
template<class T>
struct SingleEdgeView {
std::span<const typename DirectedCSR<T>::Arc> arcs;
int n, gap;
int size() const { return n; }
std::span<const typename DirectedCSR<T>::Arc> operator[](int v) const {
if (v == gap) return {};
return arcs.subspan(v - (gap >= 0 && v > gap), 1);
}
};
struct Access {
template<class T>
static bool unique_incoming(const DirectedCSR<T> &g) { return g.offset_.empty() || g.unique_incoming_; }
template<class T>
static bool has_single_edge_layout(const DirectedCSR<T> &g) { return !g.offset_.empty() && g.single_gap_ >= -1; }
template<class T>
static SingleEdgeView<T> single_edge_view(const DirectedCSR<T> &g) { return {g.arcs_, g.size(), g.single_gap_}; }
};
}
#line 16 "graph/dijkstra.hpp"
inline constexpr std::int64_t DIJKSTRA_INF = 4'000'000'000'000'000'000;
namespace shortest_path_detail {
template<class T> T infinity() {
if constexpr (std::numeric_limits<T>::is_bounded && std::numeric_limits<T>::is_integer && std::numeric_limits<T>::digits < 62)
return std::numeric_limits<T>::max();
else return T(DIJKSTRA_INF);
}
template<bool Path, class G, class T>
std::vector<T> run(const G &g, int st, int target, T inf, std::vector<int> &parent) {
const auto n = static_cast<std::size_t>(g.size());
assert(n <= static_cast<std::size_t>(std::numeric_limits<int>::max()));
assert(st >= 0 && static_cast<std::size_t>(st) < n);
assert(inf > 0);
std::vector<T> d(n, inf);
d[st] = 0;
DistanceQueue<T> pq;
pq.push(0, st);
while (!pq.empty()) {
const auto [cost, v] = pq.pop();
if (d[v] != cost) continue;
if constexpr (Path) if (cost >= d[target]) break;
T allowance = inf - cost;
for (const auto &[to, c] : g[v]) {
assert(to >= 0 && static_cast<std::size_t>(to) < n);
assert(c >= 0);
if (reaches_limit(c, allowance)) continue;
T next = cost + c;
if (next >= d[to]) continue;
if constexpr (Path) {
if (next >= d[target]) continue;
parent[to] = v;
}
d[to] = next;
if constexpr (Path) if (to == target) continue;
if (!g[to].empty()) pq.push(std::move(next), to);
}
}
return d;
}
template<bool Topological, class G, class T>
std::vector<T> distances(const G &g, int st, T inf) {
std::vector<int> parent;
auto d = [&] {
if constexpr (Topological) return topological_run<false>(g, st, -1, inf, parent);
else return run<false>(g, st, -1, inf, parent);
}();
for (T &val : d)
if (val == inf) val = -1;
return d;
}
template<bool Topological, class G, class T>
std::pair<T, std::vector<int>> path(const G &g, int st, int target, T inf) {
[[maybe_unused]] const auto n = static_cast<std::size_t>(g.size());
assert(n <= static_cast<std::size_t>(std::numeric_limits<int>::max()));
assert(st >= 0 && static_cast<std::size_t>(st) < n);
assert(target >= 0 && static_cast<std::size_t>(target) < n);
assert(inf > 0);
if (st == target) return {T(0), {st}};
if (g[st].empty()) return {T(-1), {}};
if (g[st].size() == 1) {
using Weight = std::remove_cvref_t<decltype(g[st][0].second)>;
std::vector<int> direct{st};
T cost = 0;
int v = st, checkpoint = st;
std::size_t power = 1, length = 0;
while (g[v].size() == 1) {
const auto &[to, weight] = g[v][0];
assert(to >= 0 && static_cast<std::size_t>(to) < n);
assert(weight >= 0);
if (reaches_limit(weight, T(inf - cost))) return {T(-1), {}};
cost += weight;
v = to;
direct.push_back(v);
if (v == target) return {std::move(cost), std::move(direct)};
if (v == checkpoint) return {T(-1), {}};
if (++length == power) {
checkpoint = v;
power *= 2;
length = 0;
}
}
if (g[v].empty()) return {T(-1), {}};
if constexpr (std::numeric_limits<Weight>::is_integer && std::numeric_limits<T>::is_integer && std::numeric_limits<T>::is_bounded) {
auto suffix = shortest_path_detail::path<Topological>(g, v, target, T(inf - cost));
if (suffix.first == T(-1)) return {T(-1), {}};
direct.pop_back();
direct.insert(direct.end(), suffix.second.begin(), suffix.second.end());
return {cost + suffix.first, std::move(direct)};
}
}
std::vector<int> parent(g.size(), -1);
if constexpr (requires { directed_csr_detail::Access::unique_incoming(g); })
if (directed_csr_detail::Access::unique_incoming(g)) return unique_path(g, st, target, inf, parent);
auto d = [&] {
if constexpr (Topological) return topological_run<true>(g, st, target, inf, parent);
else return run<true>(g, st, target, inf, parent);
}();
if (d[target] == inf) return {T(-1), {}};
std::vector<int> path;
for (int v = target; v != st; v = parent[v]) path.push_back(v);
path.push_back(st);
std::reverse(path.begin(), path.end());
return {std::move(d[target]), std::move(path)};
}
}
template<bool Topological, class T>
std::vector<T> dijkstra(const std::vector<std::vector<std::pair<int, T>>> &g, int st, T inf = shortest_path_detail::infinity<T>()) {
return shortest_path_detail::distances<Topological>(g, st, inf);
}
template<class T>
std::vector<T> dijkstra(const std::vector<std::vector<std::pair<int, T>>> &g, int st, T inf = shortest_path_detail::infinity<T>()) {
return dijkstra<false>(g, st, inf);
}
template<bool Topological, class T>
std::pair<T, std::vector<int>> dijkstra_path(const std::vector<std::vector<std::pair<int, T>>> &g, int st, int target, T inf = shortest_path_detail::infinity<T>()) {
return shortest_path_detail::path<Topological>(g, st, target, inf);
}
template<class T>
std::pair<T, std::vector<int>> dijkstra_path(const std::vector<std::vector<std::pair<int, T>>> &g, int st, int target, T inf = shortest_path_detail::infinity<T>()) {
return dijkstra_path<false>(g, st, target, inf);
}
template<bool Topological, class W, class T = typename DirectedCSR<W>::distance_type>
std::vector<T> dijkstra(const DirectedCSR<W> &g, int st, T inf = shortest_path_detail::infinity<T>()) {
return shortest_path_detail::distances<Topological>(g, st, inf);
}
template<class W, class T = typename DirectedCSR<W>::distance_type>
std::vector<T> dijkstra(const DirectedCSR<W> &g, int st, T inf = shortest_path_detail::infinity<T>()) {
return dijkstra<false>(g, st, inf);
}
template<bool Topological, class W, class T = typename DirectedCSR<W>::distance_type>
std::pair<T, std::vector<int>> dijkstra_path(const DirectedCSR<W> &g, int st, int target, T inf = shortest_path_detail::infinity<T>()) {
if (directed_csr_detail::Access::has_single_edge_layout(g))
return shortest_path_detail::path<Topological>(directed_csr_detail::Access::single_edge_view(g), st, target, inf);
return shortest_path_detail::path<Topological>(g, st, target, inf);
}
template<class W, class T = typename DirectedCSR<W>::distance_type>
std::pair<T, std::vector<int>> dijkstra_path(const DirectedCSR<W> &g, int st, int target, T inf = shortest_path_detail::infinity<T>()) {
return dijkstra_path<false>(g, st, target, inf);
}
#line 1 "components/fast_io.hpp"
#line 5 "components/fast_io.hpp"
#include <array>
#line 8 "components/fast_io.hpp"
#include <charconv>
#line 11 "components/fast_io.hpp"
#include <cstdio>
#include <cstring>
#line 15 "components/fast_io.hpp"
#include <string>
#include <string_view>
#line 18 "components/fast_io.hpp"
#if defined(__unix__) || defined(__APPLE__)
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace fast_io_detail { struct BulkInput; struct BulkOutput; }
class FastInput {
friend struct fast_io_detail::BulkInput;
static constexpr std::size_t capacity = 1 << 16;
FILE *file = nullptr;
std::unique_ptr<char[]> buffer;
const char *current = "", *end = current;
std::string scratch;
void *mapping = nullptr;
std::size_t mapped_size = 0;
bool refill() {
if (!file) return false;
std::size_t n = std::fread(buffer.get(), 1, capacity, file);
current = buffer.get();
end = current + n;
return n;
}
static const char *scan(const char *p, const char *end) {
while (end - p >= 8) {
std::uint64_t x;
std::memcpy(&x, p, 8);
if ((x - 0x2121212121212121ULL) & ~x & 0x8080808080808080ULL) break;
p += 8;
}
while (p != end && static_cast<unsigned char>(*p) > ' ') ++p;
return p;
}
public:
explicit FastInput(FILE *input = stdin) {
#if defined(__unix__) || defined(__APPLE__)
struct stat status;
int fd = fileno(input);
auto offset = ftello(input);
if (fd >= 0 && offset >= 0 && !fstat(fd, &status) && S_ISREG(status.st_mode) && status.st_size > offset) {
mapped_size = status.st_size;
mapping = mmap(nullptr, mapped_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapping != MAP_FAILED) {
current = static_cast<const char *>(mapping) + offset;
end = static_cast<const char *>(mapping) + mapped_size;
return;
}
mapping = nullptr;
}
#endif
file = input;
buffer = std::make_unique_for_overwrite<char[]>(capacity);
}
explicit FastInput(std::string_view input) : current(input.empty() ? "" : input.data()), end(current + input.size()) {}
FastInput(const FastInput &) = delete;
FastInput &operator=(const FastInput &) = delete;
~FastInput() {
#if defined(__unix__) || defined(__APPLE__)
if (mapping) munmap(mapping, mapped_size);
#endif
}
std::string_view token() {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return {};
}
const char *begin = current;
current = scan(current, end);
if (current != end || !file) return {begin, std::size_t(current - begin)};
scratch.assign(begin, end);
while (refill()) {
current = scan(current, end);
scratch.append(buffer.get(), std::size_t(current - buffer.get()));
if (current != end) break;
}
return scratch;
}
template <class T>
bool read(T &value) {
if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>) {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return false;
}
if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4 && std::endian::native == std::endian::little) {
if (end - current >= 11) {
std::uint64_t x;
std::memcpy(&x, current, 8);
if (!(((x + 0x4646464646464646ULL) | (x - 0x3030303030303030ULL)) & 0x8080808080808080ULL)) {
x &= 0x0f0f0f0f0f0f0f0fULL;
x = (x * 10 + (x >> 8)) & 0x00ff00ff00ff00ffULL;
x = (x * 100 + (x >> 16)) & 0x0000ffff0000ffffULL;
std::uint64_t v = std::uint32_t(x * 10000 + (x >> 32));
int n = 8;
for (; n < 10 && static_cast<unsigned char>(current[n]) - unsigned('0') < 10; ++n)
v = v * 10 + (current[n] - '0');
if (static_cast<unsigned char>(current[n]) <= ' ' && v <= 0xffffffffULL) {
current += n;
value = T(v);
return true;
}
}
}
}
T parsed{};
auto result = std::from_chars(current, end, parsed);
if (result.ec == std::errc{} && (result.ptr != end ? static_cast<unsigned char>(*result.ptr) <= ' ' : !file)) {
current = result.ptr;
value = parsed;
return true;
}
}
auto s = token();
if (s.empty()) return false;
if constexpr (std::is_same_v<T, bool>) {
assert(s == "0" || s == "1");
value = s[0] == '1';
} else if constexpr (std::is_integral_v<T>) {
auto result = std::from_chars(s.data(), s.data() + s.size(), value);
assert(result.ec == std::errc{} && result.ptr == s.data() + s.size());
(void)result;
} else value = T(s);
return true;
}
};
class FastOutput {
friend struct fast_io_detail::BulkOutput;
inline static constexpr auto quads = [] {
std::array<std::array<char, 4>, 10000> table{};
for (int i = 0; i < 10000; ++i)
table[i] = {char('0' + i / 1000), char('0' + i / 100 % 10), char('0' + i / 10 % 10), char('0' + i % 10)};
return table;
}();
FILE *file;
std::unique_ptr<char[]> buffer = std::make_unique_for_overwrite<char[]>(1 << 16);
std::size_t capacity = 1 << 16, length = 0;
char *reserve(std::size_t n) {
if (n > capacity - length) {
if (!flush()) throw std::runtime_error("output failed");
if (n > capacity) {
std::size_t next = std::max(n, 2 * capacity);
buffer = std::make_unique_for_overwrite<char[]>(next);
capacity = next;
}
}
return buffer.get() + length;
}
public:
explicit FastOutput(FILE *output = stdout) : file(output) {}
FastOutput(const FastOutput &) = delete;
FastOutput &operator=(const FastOutput &) = delete;
~FastOutput() { flush(); }
bool flush() {
std::size_t offset = 0;
while (offset < length) {
auto n = std::fwrite(buffer.get() + offset, 1, length - offset, file);
if (!n) {
std::memmove(buffer.get(), buffer.get() + offset, length - offset);
length -= offset;
return false;
}
offset += n;
}
length = 0;
return std::fflush(file) == 0;
}
void write(char c) { *reserve(1) = c; ++length; }
void write(__uint128_t value) {
char digits[40];
char *end = digits + sizeof(digits), *begin = end;
do { *--begin = char('0' + value % 10); value /= 10; } while (value);
write(std::string_view(begin, end - begin));
}
void write(__int128_t value) {
__uint128_t magnitude = __uint128_t(value);
if (value < 0) write('-'), magnitude = 0 - magnitude;
write(magnitude);
}
void write(std::string_view s) {
char *out = reserve(s.size());
if (!s.empty()) std::memcpy(out, s.data(), s.size());
length += s.size();
}
template <class T> requires std::is_integral_v<T>
void write(T value) {
char *out = reserve(32);
if constexpr (std::is_same_v<T, bool>) *out = '0' + value, ++length;
else if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4) {
char *p = out;
if (value >= 100000000) {
auto high = value / 100000000, low = value % 100000000;
p = std::to_chars(p, p + 2, high).ptr;
std::memcpy(p, quads[low / 10000].data(), 4);
std::memcpy(p + 4, quads[low % 10000].data(), 4);
p += 8;
} else if (value >= 10000) {
p = std::to_chars(p, p + 4, value / 10000).ptr;
std::memcpy(p, quads[value % 10000].data(), 4);
p += 4;
} else p = std::to_chars(p, p + 4, value).ptr;
length += p - out;
} else length += std::to_chars(out, out + 32, value).ptr - out;
}
template <class T> requires requires(const T &value, char *out) { value.decimal_capacity(); value.to_chars(out); }
void write(const T &value) {
char *out = reserve(value.decimal_capacity());
length += value.to_chars(out) - out;
}
template <class T>
void println(const T &value) { write(value); write('\n'); }
};
#line 4 "test/dijkstra_bigint_yuki.test.cpp"
#include <boost/multiprecision/cpp_int.hpp>
#include <numeric>
int main() {
FastInput input;
FastOutput output;
int n = 0, m = 0;
input.read(n), input.read(m);
struct Edge { int u, v, a, b; };
std::vector<Edge> edges(m);
int max_b = 1;
for (auto& e : edges) input.read(e.u), input.read(e.v), input.read(e.a), input.read(e.b), max_b = std::max(max_b, e.b);
using Integer = boost::multiprecision::cpp_int;
Integer denominator = 1;
for (int b = 2; b <= max_b; ++b) denominator *= b / std::gcd(b, int(Integer(denominator % b)));
std::vector<std::vector<std::pair<int, Integer>>> graph(n);
std::vector<Integer> unit(max_b + 1);
for (int b = 1; b <= max_b; ++b) unit[b] = denominator / b;
for (auto e : edges) {
Integer weight = unit[e.b] * e.a;
graph[e.u - 1].emplace_back(e.v - 1, weight);
graph[e.v - 1].emplace_back(e.u - 1, std::move(weight));
}
auto distance = dijkstra(graph, 0, Integer(denominator * (300LL * n + 1)));
for (int v = 1; v < n; ++v) {
Integer divisor = gcd(distance[v], denominator);
output.write(Integer(distance[v] / divisor).str());
output.write(' ');
output.println(Integer(denominator / divisor).str());
}
}