Hello all, I was building a program which needs to output in alphabetical order and i got stuck.
(Program reads in text file and then do dijkstra's shortest path)
My output is fine except that it is not in alphabetical order....
can anyone help me with this???
here is my code:
void dijkstra(string s, NodeMap &nodes)
{
// check and report or abort
Node* source = nodes.nodemap[s];
if(source==0)
{
cout << "Sorry we don't fly from "<< s << endl; return;
}
reset_nodes(nodes);
// put the source into pq and loop until empty
priority_queue<Node *, deque<Node*>, compare>pq;
pq.push(source);
while(!pq.empty())
{
// process least cost node.
Node* curr = pq.top();
pq.pop();
curr->visited = true;
// process neighbors
list<Edge*>::iterator edge;
for(edge = curr->neighbors.begin(); edge != curr->neighbors.end(); edge++)
{
Node *ne = (*edge)->dest;
if(!ne->visited)
{
ne->cost += (*edge)->weight + curr->cost;
ne->visited = true;
ne->back_pointer = curr;
cout << "Cheapest price to "<< ne->name << " = " << ne->cost << endl;;
pq.push(ne);
}
}
}
}