Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. 50 CostMap extended_dijkstra(const DistGraph &g, std::string start_node) {
  2. 51 //3a
  3. 52 CostMap info_map;
  4. 53 CostMap answer_map;
  5. 54 //3b
  6. 55 CostPQ sorter;
  7. 56
  8. 57 for (auto nodes : g.all_nodes()){
  9. 58 Info inf = Info(nodes.first);
  10. 59 if (nodes.first == start_node)
  11. 60 inf.cost = 0;
  12. 61 info_map[nodes.first] = inf;
  13. 62 sorter.enqueue(inf);
  14. 63 }
  15. 64 //3c
  16. 65 while(!info_map.empty()){
  17. 66 //3c1
  18. 67 Info current = sorter.dequeue();
  19. 68 //3c2
  20. 69 std::string min_node = current.node;
  21. 70 int min_cost = current.cost;
  22. 71 if (min_cost == std::numeric_limits<int>::max())
  23. 72 break;
  24. 73 //std::cout << info_map << std::endl;
  25. 74 if (!answer_map.has_key(min_node)){
  26. 75 //3c3
  27. 76 answer_map[min_node] = info_map[min_node];
  28. 77 info_map.erase(min_node);
  29. 78
  30. 79 //3c4
  31. 80 for (const auto d : g.out_nodes(min_node)){
  32. 81 if (!answer_map.has_key(d)){
  33. 82 int real_cost = min_cost + g.edge_value(min_node, d);
  34. 83 Info &inf = info_map[d];
  35. 84 if (real_cost < inf.cost){
  36. 85 //1
  37. 86 inf.cost = real_cost;
  38. 87 inf.from = min_node;
  39. 88 //2
  40. 89 sorter.enqueue(inf);
  41. 90 }
  42. 91 }
  43. 92 }
  44. 93 }
  45. 94 }
  46. 95 //3d
  47. 96 return answer_map;
  48. 97 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement