19 #include "absl/container/flat_hash_set.h"
30 bool operator<(
const Element& other)
const {
31 return distance_ != other.distance_ ? distance_ > other.distance_
32 : node_ > other.node_;
34 void SetHeapIndex(
int h) { heap_index_ = h; }
35 int GetHeapIndex()
const {
return heap_index_; }
37 int64_t
distance()
const {
return distance_; }
38 void set_node(
int node) { node_ = node; }
39 int node()
const {
return node_; }
42 int64_t distance_ = 0;
54 std::function<int64_t(
int,
int)> graph,
55 int64_t disconnected_distance)
56 : node_count_(node_count),
57 start_node_(start_node),
58 graph_(std::move(graph)),
59 disconnected_distance_(disconnected_distance),
60 predecessor_(new int[node_count]),
61 elements_(node_count) {}
68 int node = SelectClosestNode(&
distance);
72 }
else if (node == end_node) {
79 FindPath(end_node,
nodes);
86 for (
int i = 0; i < node_count_; i++) {
87 elements_[i].set_node(i);
88 if (i == start_node_) {
90 elements_[i].set_distance(0);
91 frontier_.
Add(&elements_[i]);
94 predecessor_[i] = start_node_;
95 not_visited_.insert(i);
100 int SelectClosestNode(int64_t*
distance) {
101 const int node = frontier_.
Top()->node();
104 not_visited_.erase(node);
105 added_to_the_frontier_.erase(node);
109 void Update(
int node) {
110 for (
const auto& other_node : not_visited_) {
111 const int64_t graph_node_i = graph_(node, other_node);
112 if (graph_node_i != disconnected_distance_) {
113 if (added_to_the_frontier_.find(other_node) ==
114 added_to_the_frontier_.end()) {
115 frontier_.
Add(&elements_[other_node]);
116 added_to_the_frontier_.insert(other_node);
118 const int64_t other_distance =
119 elements_[node].distance() + graph_node_i;
120 if (elements_[other_node].
distance() > other_distance) {
121 elements_[other_node].set_distance(other_distance);
123 predecessor_[other_node] = node;
129 void FindPath(
int dest, std::vector<int>*
nodes) {
132 while (predecessor_[j] != -1) {
133 nodes->push_back(predecessor_[j]);
138 const int node_count_;
139 const int start_node_;
140 std::function<int64_t(
int,
int)> graph_;
141 const int64_t disconnected_distance_;
142 std::unique_ptr<int[]> predecessor_;
144 std::vector<Element> elements_;
146 S added_to_the_frontier_;
150 std::function<int64_t(
int,
int)> graph,
151 int64_t disconnected_distance,
152 std::vector<int>*
nodes) {
154 node_count, start_node, std::move(graph), disconnected_distance);
159 std::function<int64_t(
int,
int)> graph,
160 int64_t disconnected_distance,
161 std::vector<int>*
nodes) {
163 disconnected_distance);
void NoteChangedPriority(T *val)
bool ShortestPath(int end_node, std::vector< int > *nodes)
DijkstraSP(int node_count, int start_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance)
static constexpr int64_t kInfinity
static const int64_t kint64max
Collection of objects used to extend the Constraint Solver library.
bool DijkstraShortestPath(int node_count, int start_node, int end_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance, std::vector< int > *nodes)
bool StableDijkstraShortestPath(int node_count, int start_node, int end_node, std::function< int64_t(int, int)> graph, int64_t disconnected_distance, std::vector< int > *nodes)