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)
- {
- if (!exitPath.empty()) exitPath.clear();
- std::vector<bool> vis(m_nodes.size(), false);
- std::vector<int> dist(m_nodes.size(), 0x3f3f3f3f);
- std::priority_queue < std::pair<int, Node>, std::deque<std::pair<int, Node>>, comparator> pq;
- for (const auto& node : m_nodes)
- {
- if (node == start) continue;
- vis[node.GetInfo()] = false;
- }
- vis[start.GetInfo()] = false;
- dist[start.GetInfo()] = 0;
- pq.push({dist[start.GetInfo()], start});
- finished = false;
- Node curr;
- while (!pq.empty())
- {
- do
- {
- curr = pq.top().second;
- pq.pop();
- } while (vis[curr.GetInfo()] == true);
- vis[curr.GetInfo()] = true;
- if (curr == end)
- {
- finished = true;
- break;
- }
- auto vec = GetConnectionsOfCurrNode(curr);
- for (const auto& it : vec)
- {
- if (vis[it.GetSecondNode().GetInfo()] == false)
- {
- Node next = it.GetSecondNode();
- int cost = it.GetCost();
- if (dist[next.GetInfo()] > dist[curr.GetInfo()] + cost)
- {
- dist[next.GetInfo()] = dist[curr.GetInfo()] + cost;
- exitPath.emplace_back(it);
- pq.push({dist[next.GetInfo()], next});
- }
- }
- }
- }
- if (finished)
- {
- qDebug() << "Am gasit pathul cel mai scurt";
- }
- else
- {
- qDebug() << "N-am gasit...";
- }
- }
- std::vector<Edge> Graph::GetConnectionsOfCurrNode(const Node& node)
- {
- std::vector<Edge> ans;
- for (const auto& edge : m_edges)
- {
- if (edge.GetFirstNode() == node)
- {
- ans.emplace_back(edge);
- }
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement