本文整理汇总了C++中PriorityQueue::popOffTop方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::popOffTop方法的具体用法?C++ PriorityQueue::popOffTop怎么用?C++ PriorityQueue::popOffTop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::popOffTop方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
vector<Road *> shortestPath(vector<Node *> nodes, int const startingPosition, int const endingPosition){
cout << "\n\n\n";
PriorityQueue nodePQueue;
nodePQueue.addNode(nodes[startingPosition]);
nodes[startingPosition]->priority = 0;
nodePQueue.lowerPriority(1, nodes[startingPosition]);
Node * temp;
bool pathFound = false;
while(nodePQueue.size() > 0){
temp = nodePQueue.popOffTop();
temp->visited = true;
if(temp->lineNumber == endingPosition){ // we have reached our destination
pathFound = true;
break; }
// update children
for(int i = 0; i < temp->roads.size(); i+=1){
// get the node number of the node on the other side of the road
int otherSide = temp->roads[i]->connection2;
if(temp->roads[i]->connection2 == temp->lineNumber)
otherSide = temp->roads[i]->connection1;
if(nodes[otherSide]->visited) continue; // child already visited, no action should be taken
// if the node isnt in the heap, add it
if(nodes[otherSide]->positionInHeap == -1){
nodes[otherSide]->priority = temp->priority + temp->roads[i]->length;
nodePQueue.addNode(nodes[otherSide]); }
// if the node is in the heap and its priority is greater than the one we just found, update it
else if((nodes[otherSide]->priority) > (temp->priority + temp->roads[i]->length)){
nodes[otherSide]->priority = temp->priority + temp->roads[i]->length;
nodePQueue.lowerPriority(nodes[otherSide]->positionInHeap, nodes[otherSide]); }
else continue; // the node is in the heap and its priority is better than the one we just found
}
}
vector<Road *> pathToTake;
if(!pathFound) return pathToTake;
cout << "Destination found. Info following (priority is the distance from our starting point)\n";
cout << "endingPosition: " << endingPosition << " temp->lineNumber: " << temp->lineNumber << endl << endl;
temp->print();
// draw the path (need to figure it out first)
double pathLength = temp->priority;
Node * traversalHelp = temp;
int tooManyLoops = 5;
while(pathLength > 0){
if(!tooManyLoops){
pathFound == false;
break; }
//cout << "remaining length: " << pathLength << endl;
// search for the correct road
for(int i = 0; i < traversalHelp->roads.size(); i+=1){
// find other side of road
int otherSide = traversalHelp->roads[i]->connection2;
if(traversalHelp->roads[i]->connection2 == traversalHelp->lineNumber)
otherSide = traversalHelp->roads[i]->connection1;
// check to see if this is the correct road
double calculation = (pathLength - traversalHelp->roads[i]->length) - nodes[otherSide]->priority;
if(calculation < 0.01 && calculation > -0.01){
tooManyLoops = 5;
pathToTake.push_back(traversalHelp->roads[i]); // add the road to the path
pathLength -= traversalHelp->roads[i]->length; // adjust the remaining length
traversalHelp = nodes[otherSide]; // adjust which node we are considering
break; }
}
tooManyLoops--;
}
//if(!pathFound) {
// pathToTake.clear();
// return pathToTake; }
int lastLocation = startingPosition;
string oldName = "";
double sumOfSmallRoads = 0.0;
for(int i = pathToTake.size()-1; i >=0; i-=1){
int nextPlace = pathToTake[i]->connection1;
if(nextPlace == lastLocation) nextPlace = pathToTake[i]->connection2;
if(oldName == ""){
oldName = pathToTake[i]->name;
sumOfSmallRoads = pathToTake[i]->length;
continue;
}
if(pathToTake[i]->name != oldName){
cout << "Take " << oldName << " towards ";
cout << nodes[lastLocation]->placeName << ", " << nodes[lastLocation]->placeState << " for " << sumOfSmallRoads << " miles.\n";
sumOfSmallRoads = pathToTake[i]->length;
oldName = pathToTake[i]->name;
}
else{
sumOfSmallRoads += pathToTake[i]->length;
}
lastLocation = nextPlace;
}
cout << "Take " << pathToTake[0]->name << " towards your destination " << " for " << pathToTake[0]->length << " miles.\n";
return pathToTake;
}