This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "graph/shortest-path/dijkstra-radix-heap.hpp"#pragma once
#include <algorithm>
#include <limits>
#include <tuple>
#include <vector>
#include "../../structure/heap/radix-heap.hpp"
#include "../graph-template.hpp"
/**
* @brief Dijkstra-Radix-Heap(単一始点最短路)
*/
template <typename T>
std::vector<T> dijkstra_radix_heap(Graph<T>& g, int s) {
const auto INF = std::numeric_limits<T>::max();
std::vector<T> dist(g.size(), INF);
RadixHeap<T, int> heap;
dist[s] = 0;
heap.push(dist[s], s);
while (!heap.empty()) {
T cost;
int idx;
std::tie(cost, idx) = heap.pop();
if (dist[idx] < cost) continue;
for (auto& e : g.g[idx]) {
auto next_cost = cost + e.cost;
if (dist[e.to] <= next_cost) continue;
dist[e.to] = next_cost;
heap.push(dist[e.to], e.to);
}
}
return dist;
}
#line 2 "graph/shortest-path/dijkstra-radix-heap.hpp"
#include <algorithm>
#include <limits>
#include <tuple>
#include <vector>
#line 2 "structure/heap/radix-heap.hpp"
#line 4 "structure/heap/radix-heap.hpp"
#include <array>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <utility>
#line 10 "structure/heap/radix-heap.hpp"
template <typename key_t, typename val_t>
struct RadixHeap {
static constexpr int bit = sizeof(key_t) * 8;
std::array<std::vector<std::pair<key_t, val_t> >, bit> vs;
std::size_t sz;
key_t last;
RadixHeap() : sz(0), last(0) {}
bool empty() const { return sz == 0; }
std::size_t size() const { return sz; }
inline int getbit(int a) const { return a ? bit - __builtin_clz(a) : 0; }
inline int getbit(std::int64_t a) const {
return a ? bit - __builtin_clzll(a) : 0;
}
void push(const key_t& key, const val_t& val) {
sz++;
vs[getbit(key ^ last)].emplace_back(key, val);
}
std::pair<key_t, val_t> pop() {
if (vs[0].empty()) {
int idx = 1;
while (vs[idx].empty()) idx++;
last = std::min_element(vs[idx].begin(), vs[idx].end())->first;
for (auto& p : vs[idx]) vs[getbit(p.first ^ last)].emplace_back(p);
vs[idx].clear();
}
--sz;
auto res = vs[0].back();
vs[0].pop_back();
return res;
}
};
#line 2 "graph/graph-template.hpp"
#line 4 "graph/graph-template.hpp"
#include <iostream>
#line 6 "graph/graph-template.hpp"
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 {
std::vector<std::vector<Edge<T> > > g;
int es;
Graph() = default;
explicit Graph(int n) : g(n), es(0) {}
std::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;
std::cin >> a >> b;
a += padding;
b += padding;
T c = T(1);
if (weighted) std::cin >> c;
if (directed)
add_directed_edge(a, b, c);
else
add_edge(a, b, c);
}
}
inline std::vector<Edge<T> >& operator[](const int& k) { return g[k]; }
inline const std::vector<Edge<T> >& operator[](const int& k) const {
return g[k];
}
};
template <typename T = int>
using Edges = std::vector<Edge<T> >;
#line 10 "graph/shortest-path/dijkstra-radix-heap.hpp"
/**
* @brief Dijkstra-Radix-Heap(単一始点最短路)
*/
template <typename T>
std::vector<T> dijkstra_radix_heap(Graph<T>& g, int s) {
const auto INF = std::numeric_limits<T>::max();
std::vector<T> dist(g.size(), INF);
RadixHeap<T, int> heap;
dist[s] = 0;
heap.push(dist[s], s);
while (!heap.empty()) {
T cost;
int idx;
std::tie(cost, idx) = heap.pop();
if (dist[idx] < cost) continue;
for (auto& e : g.g[idx]) {
auto next_cost = cost + e.cost;
if (dist[e.to] <= next_cost) continue;
dist[e.to] = next_cost;
heap.push(dist[e.to], e.to);
}
}
return dist;
}