126 SettledNodeCallbackType settled_node_callback) {
128 const int num_sources = source_sets.
size();
129 std::vector<absl::flat_hash_map<int, DistanceAndParentArc<DistanceType>>>
130 reached_from(num_sources);
136 struct StateComparator {
137 bool operator()(
const State& s0,
const State& s1)
const {
138 if (s0.distance != s1.distance)
return s0.distance > s1.distance;
139 if (s0.node != s1.node)
return s0.node > s1.node;
140 return s0.source_index > s1.source_index;
143 StateComparator pq_state_comparator;
144 std::priority_queue<State, std::vector<State>, StateComparator> pq(
145 pq_state_comparator);
146 std::vector<bool> dijkstra_is_active(num_sources,
false);
147 int num_active_dijkstras = 0;
148 for (
int source_index = 0; source_index < num_sources; ++source_index) {
149 if (!source_sets[source_index].empty()) {
150 dijkstra_is_active[source_index] =
true;
151 ++num_active_dijkstras;
153 for (
const int node : source_sets[source_index]) {
155 &reached_from[source_index],
157 pq.push({0, node, source_index});
163 while (!pq.empty() && num_active_dijkstras > 0) {
164 const State state = pq.top();
167 if (!dijkstra_is_active[state.source_index])
continue;
172 if (
gtl::FindOrDie(reached_from[state.source_index], state.node).distance <
177 if (settled_node_callback(state.node, state.source_index, state.distance)) {
178 dijkstra_is_active[state.source_index] =
false;
179 --num_active_dijkstras;
183 for (
const int arc :
graph.OutgoingArcs(state.node)) {
184 const DistanceType
distance = arc_length_functor(
arc) + state.distance;
185 const int head_node =
graph.Head(
arc);
186 auto insertion_result = reached_from[state.source_index].insert(
188 if (!insertion_result.second) {
189 if (insertion_result.first->second.distance <=
distance)
continue;
190 insertion_result.first->second = {
distance,
194 state.source_index});
std::vector< absl::flat_hash_map< int, DistanceAndParentArc< DistanceType > > > MultiDijkstra(const Graph &graph, ArcLengthFunctor arc_length_functor, const std::vector< std::vector< int > > &source_sets, SettledNodeCallbackType settled_node_callback)