21 #include "absl/container/flat_hash_set.h" 32 bool operator<(
const Element& other)
const {
33 return distance_ != other.distance_ ? distance_ > other.distance_
34 : node_ > other.node_;
36 void SetHeapIndex(
int h) { heap_index_ = h; }
37 int GetHeapIndex()
const {
return heap_index_; }
39 int64_t
distance()
const {
return distance_; }
40 void set_node(
int node) { node_ = node; }
41 int node()
const {
return node_; }
44 int64_t distance_ = 0;
56 std::function<int64_t(
int,
int)> graph,
57 int64_t disconnected_distance)
58 : node_count_(node_count),
59 start_node_(start_node),
60 graph_(std::move(graph)),
61 disconnected_distance_(disconnected_distance),
62 predecessor_(new int[node_count]),
63 elements_(node_count) {}
70 int node = SelectClosestNode(&
distance);
74 }
else if (node == end_node) {
81 FindPath(end_node,
nodes);
88 for (
int i = 0; i < node_count_; i++) {
89 elements_[i].set_node(i);
90 if (i == start_node_) {
92 elements_[i].set_distance(0);
93 frontier_.
Add(&elements_[i]);
96 predecessor_[i] = start_node_;
97 not_visited_.insert(i);
102 int SelectClosestNode(int64_t*
distance) {
103 const int node = frontier_.
Top()->node();
106 not_visited_.erase(node);
107 added_to_the_frontier_.erase(node);
111 void Update(
int node) {
112 for (
const auto& other_node : not_visited_) {
113 const int64_t graph_node_i = graph_(node, other_node);
114 if (graph_node_i != disconnected_distance_) {
115 if (added_to_the_frontier_.find(other_node) ==
116 added_to_the_frontier_.end()) {
117 frontier_.
Add(&elements_[other_node]);
118 added_to_the_frontier_.insert(other_node);
120 const int64_t other_distance =
121 elements_[node].distance() + graph_node_i;
122 if (elements_[other_node].
distance() > other_distance) {
123 elements_[other_node].set_distance(other_distance);
125 predecessor_[other_node] = node;
131 void FindPath(
int dest, std::vector<int>*
nodes) {
134 while (predecessor_[j] != -1) {
135 nodes->push_back(predecessor_[j]);
140 const int node_count_;
141 const int start_node_;
142 std::function<int64_t(
int,
int)> graph_;
143 const int64_t disconnected_distance_;
144 std::unique_ptr<int[]> predecessor_;
146 std::vector<Element> elements_;
148 S added_to_the_frontier_;
152 std::function<int64_t(
int,
int)> graph,
153 int64_t disconnected_distance,
154 std::vector<int>*
nodes) {
156 node_count, start_node, std::move(graph), disconnected_distance);
161 std::function<int64_t(
int,
int)> graph,
162 int64_t disconnected_distance,
163 std::vector<int>*
nodes) {
165 disconnected_distance);
static constexpr int64_t kInfinity
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)
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)
void NoteChangedPriority(T *val)
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)
Collection of objects used to extend the Constraint Solver library.