Min heap in Dijkstra Implementation

Hello guys,

Today I was implementing Dijkstra Algorithm and i wanted to do something different
I wanted to push ( dist[b], b ) instead of pushing ( -dist[b] , b) . Here we are pushing -dist[b] because we want it to be in ascending order
I wanted to push positive value in the priority queue
So I would like to create min heap in a priority queue.

Here is the min-heap in priority queue :

47%20AM

How to do it for priority queue which contains pairs
Here is my code :

06%20AM

And it didn’t work in my IDE and here is what error it showed :

It will be helpful if someone tells me how to declare it
@ssjgz @sarthakmanna

Thanks a lot ! :slight_smile:

Post the full main.cpp, please (with formatting, and not a screenshot) :slight_smile: What compiler is being used?

1 Like

Clion
Ya will post the screenshot

https://pastebin.com/G9Lw0Min

1 Like

Actually, no need; here’s some sample code:

#include <iostream>
#include <queue>

using namespace std;

using ll = long long;

int main(){
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<>> pq;
    pq.push({7,8});
    pq.push({1,3});
    pq.push({5,7});

    while (!pq.empty())
    {
        cout << "popping: " << pq.top().first << ", " << pq.top().second << endl;
        pq.pop();
    }
}
3 Likes

Thanks a lot !! @ssjgz
It worked !!

1 Like

Only for pairs or for anything this works ?

Pretty much anything with an operator>, I would think.

2 Likes