Advertisement
mfgnik

Untitled

Nov 26th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. std::vector<int64_t> Dijkstra(int vertex) {
  2. std::vector<int64_t > distances(number_of_vertices_, std::numeric_limits<int64_t>::max());
  3. distances[vertex] = 0;
  4. std::priority_queue<Vertex> grey;
  5. std::unordered_set<int> black;
  6. grey.emplace(vertex, 0);
  7. while (not grey.empty()) {
  8. auto current_vertex = grey.top();
  9. grey.pop();
  10. black.insert(current_vertex.index);
  11. if (current_vertex.distance > distances[current_vertex.index]) {
  12. continue;
  13. }
  14. for (auto [other_vertex, edge]: adjacency_list_[current_vertex.index]) {
  15. if (black.count(other_vertex) == 0 &&
  16. (distances[other_vertex] == std::numeric_limits<int64_t>::max() ||
  17. distances[current_vertex.index] + edge.weight < distances[other_vertex])) {
  18. distances[other_vertex] = distances[current_vertex.index] + edge.weight;
  19. grey.emplace(other_vertex, distances[other_vertex]);
  20. }
  21. }
  22. }
  23. return distances;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement