Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct comparator
- {
- bool operator()(const std::pair<int, Node>& pair1, const std::pair<int, Node>& pair2)
- {
- return pair1.first < pair2.first;
- }
- };
- void Graph::Dijkstra(Node start, Node end)
- {
- std::vector<int> dist(m_nodes.size(), 0x3f3f3f3f);
- std::priority_queue < std::pair<int, Node>, std::deque<std::pair<int, Node>>, comparator> pq;
- pq.push({0, start});
- dist[start.GetInfo()] = 0;
- if (!exitPath.empty()) exitPath.clear();
- finished = false;
- while (!pq.empty())
- {
- Node curr = pq.top().second;
- pq.pop();
- if (curr == end)
- {
- finished = true;
- break;
- }
- auto vec = GetConnectionsOfCurrNode(curr);
- for (const auto& it : vec)
- {
- Node next = it.GetSecondNode();
- int cost = it.GetCost();
- if (dist[next.GetInfo()] > dist[curr.GetInfo()] + cost)
- {
- exitPath.push_back(it);
- dist[next.GetInfo()] = dist[curr.GetInfo()] + cost;
- pq.push({dist[next.GetInfo()], next});
- }
- }
- }
- if (finished)
- {
- qDebug() << "Am gasit pathul cel mai scurt";
- }
- else
- {
- qDebug() << "N-am gasit...";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement