本文整理汇总了C++中PriorityQueue::remove_element方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::remove_element方法的具体用法?C++ PriorityQueue::remove_element怎么用?C++ PriorityQueue::remove_element使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::remove_element方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
bool Hex<T>::shortest_path(T source, T target, player_t player)
{
PriorityQueue<T,T> priorityQueue;
// Apart from board positions, we also add source and target in the priority queue.
// source & target: These are virtual home and goal.
//Initialize cost of vertices as infinite except the source vertex
for (T vtx_i = 0; vtx_i != graph_.nVertex(); ++vtx_i)
{
vecVisited_[vtx_i] = false;
if (vtx_i == source)
{
vecCost_[vtx_i] = 0;
priorityQueue.insert_element(vtx_i,0);
}
else
{
vecCost_[vtx_i] = std::numeric_limits<T>::max();
if ( ( is_pos_on_board(vtx_i) && (board_.position_player(vtx_i) == player) ) ||
(vtx_i == target) )
{
priorityQueue.insert_element(vtx_i,std::numeric_limits<T>::max());
}
}
}
bool reachedTarget = false;
while (!priorityQueue.empty())
{
std::pair<T,T> topElem = priorityQueue.top();
T vtxTop = topElem.first;
T costTop = topElem.second;
vecVisited_[vtxTop] = true;
priorityQueue.remove_element(vtxTop,costTop);
if (costTop == std::numeric_limits<T>::max())
{
break; // reaching here means path from source to target is not present
}
if (vtxTop == target)
{
reachedTarget = true;
break;
}
std::vector<T> neighbors = graph_.neighbors(vtxTop);
for (std::vector<T>::iterator it = neighbors.begin(); it != neighbors.end(); ++it)
{
if (vecVisited_[*it])
{
continue;
}
// For a board position consider this neighbor only if the current player has placed
// his/her hex on this position.
if (is_pos_on_board(*it))
{
if (board_.position_player(*it) != player)
{
continue;
}
}
else if ((*it) != target)
{
continue;
}
T newCost = vecCost_[vtxTop] + 1;
if (newCost < vecCost_[*it])
{
priorityQueue.change_priority(*it,vecCost_[*it],newCost);
vecCost_[*it] = newCost;
}
}
}
return reachedTarget;
}