Advertisement
aimon1337

Untitled

Dec 20th, 2022
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. struct comparator
  2. {
  3.     bool operator()(const std::pair<int, Node>& pair1, const std::pair<int, Node>& pair2)
  4.     {
  5.         return pair1.first < pair2.first;
  6.     }
  7. };
  8.  
  9. void Graph::Dijkstra(Node start, Node end)
  10. {
  11.     std::vector<int> dist(m_nodes.size(), 0x3f3f3f3f);
  12.     std::priority_queue < std::pair<int, Node>, std::deque<std::pair<int, Node>>, comparator> pq;
  13.     pq.push({0, start});
  14.     dist[start.GetInfo()] = 0;
  15.     if (!exitPath.empty()) exitPath.clear();
  16.     finished = false;
  17.     while (!pq.empty())
  18.     {
  19.         Node curr = pq.top().second;
  20.         pq.pop();
  21.         if (curr == end)
  22.         {
  23.             finished = true;
  24.             break;
  25.         }
  26.         auto vec = GetConnectionsOfCurrNode(curr);
  27.         for (const auto& it : vec)
  28.         {
  29.             Node next = it.GetSecondNode();
  30.             int cost = it.GetCost();
  31.             if (dist[next.GetInfo()] > dist[curr.GetInfo()] + cost)
  32.             {
  33.                 exitPath.push_back(it);
  34.                 dist[next.GetInfo()] = dist[curr.GetInfo()] + cost;
  35.                 pq.push({dist[next.GetInfo()], next});
  36.             }
  37.         }
  38.     }
  39.     if (finished)
  40.     {
  41.         qDebug() << "Am gasit pathul cel mai scurt";
  42.     }
  43.     else
  44.     {
  45.         qDebug() << "N-am gasit...";
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement