Need help with C++ STL & Dijkstra

Revision en1, by ronakchauhan, 2017-04-16 17:30:49

I understood why a priority queue is needed for Dijkstra's algorithm, but i have a doubt when it comes to the implementation.Here's one of the implementations I came across : https://gist.github.com/johngian/1821625/.

I donot understand why the following wouldn't work (here's my code for comparing)

struct comp {
    bool operator() (const pair<int, int> &a,  const pair<int, int> &b) {
        return a.second < b.second;
    }
};

and my code for defining the priority queue is priority_queue <pair<int, int>, comp> Q

Also, why did he (and almost all other implementations) have an additional vector while defining a priority queue? Something similar to this priority_queue <pair<int, int>, vector <pair<int, int> > , comp > Q;

Any help is greatly appreciated :)

Tags c++, stl

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English ronakchauhan 2017-04-16 17:30:49 844 Initial revision (published)