Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<int64_t> Dijkstra(int vertex) {
- std::vector<int64_t > distances(number_of_vertices_, std::numeric_limits<int64_t>::max());
- distances[vertex] = 0;
- std::priority_queue<Vertex> grey;
- std::unordered_set<int> black;
- grey.emplace(vertex, 0);
- while (not grey.empty()) {
- auto current_vertex = grey.top();
- grey.pop();
- black.insert(current_vertex.index);
- if (current_vertex.distance > distances[current_vertex.index]) {
- continue;
- }
- for (auto [other_vertex, edge]: adjacency_list_[current_vertex.index]) {
- if (black.count(other_vertex) == 0 &&
- (distances[other_vertex] == std::numeric_limits<int64_t>::max() ||
- distances[current_vertex.index] + edge.weight < distances[other_vertex])) {
- distances[other_vertex] = distances[current_vertex.index] + edge.weight;
- grey.emplace(other_vertex, distances[other_vertex]);
- }
- }
- }
- return distances;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement