本文整理汇总了C++中PriorityQueue::replaceKey方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::replaceKey方法的具体用法?C++ PriorityQueue::replaceKey怎么用?C++ PriorityQueue::replaceKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::replaceKey方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dijkstra
void Graph::dijkstra(Vertex *src)
{
comparisons = 0;//tallies number of comparisons made
src->d = 0;
src->p = NULL;
PriorityQueue<Vertex*> pq;
for(int i = 0;i<vertList.size();++i){//Fill priority queue
if(vertList[i]->getID() != src->getID()){
vertList[i]->d = INT_MAX;
vertList[i]->p = NULL;
}
pq.insertItem(vertList[i]->d, vertList[i], vertList[i]->getID());
}
while(!pq.isEmpty()){//Start sifting through the vertices
Vertex* u = pq.minElement();
pq.removeMin();
int sz = u->outList.size();
for(int i =0; i < sz;++i){
int alt = u->d + u->outList[i]->getWeight();
Vertex* evalVert = u->outList[i]->geteVertP();
comparisons++;
if(u->outList[i]->geteVertP()->d == INT_MAX){//relax function
Locator repkey = pq.getLoc(evalVert->getID());
pq.replaceKey(repkey,alt);
evalVert->setD(alt);
evalVert->setP(u);
}
else if(alt < u->outList[i]->geteVertP()->d){//relax function
Locator repkey = pq.getLoc(evalVert->getID());
pq.replaceKey(repkey,alt);
evalVert->setD(alt);
evalVert->setP(u);
}
}
}
comparisons += pq.getComps();//grab comparisons from priority queue
return;
}
示例2: DijkstraDistances
// DESC: Dijkstra's algorithm to find shortest distance from source to destination on a graph.
// INPUT: Graph to expand on, Vertex to represent as the source.
// OUTPUT: Linked list of the path
LinkedListT* DijkstraDistances(Graph* graph, Vertex* source) {
// form a cloud to every point.
// Store this cloud in the PQueue.
LinkedListT* verticies = graph->verticies();
PriorityQueue* pq = new PriorityQueue();
// loop through verticies
for(int i=0; i<verticies->size(); i++) {
Vertex* vv = (Vertex*)verticies->get(i);
if(vv == source) {
vv->distance = 0; // set vertex to zero if it is the source vertex.
} else {
vv->distance = 999999999; // set verticies to infinity to represent an unreachable location.
}
// insert into queue.
pq->insert(vv->distance,vv);
}
// empty out the set
while(!pq->empty()) {
// remove the minimum distance from the set. (usually source vertex on start)
Vertex* v = (Vertex*)pq->removeMin();
LinkedListT* vt = v->incidentEdges;
// loop through incident Edges
for(int i=0; i<vt->size(); i++) {
Edge* e = (Edge*)vt->get(i);
Vertex* v2 = e->opposite(v);
int r = v->distance + e->data;
// if the total range is less than v2's distance, then set it.
if(r < v2->distance) {
v2->distance = r;
pq->replaceKey(pq->top(),(void*)v2,r);
}
}
}
}
示例3: main
int main()
{
int comps=0; //BH comparisons
string txt;
cout << "Enter name of txt file without extension: " << endl;
cin >> txt;
string str(txt + ".txt");
Graph graph(str);
graph.PrintGraph();
int sVert, dVert;
cout << "Enter the source vertex: " << endl;
cin >> sVert;
cout << "Enter the destination vertex: " << endl;
cin >> dVert;
cout << " " << endl;
// for storing distances and extracting the minimum; equivalent to Q and d[] on the slides
PriorityQueue<int> pq;
int size = graph.getSize(); //graph size
int kArray[size];
for(int i = 0; i < size; i++) {kArray[i] = INT_MAX;} //key/weight
int xArray[size];
for(int i = 1; i <= size; i++) {xArray[i-1] = i;} //element
pq.createPriorityQueue(kArray, xArray, xArray, size);
// for storing the parent (previous node) on the path; equivalent to pi[] on the slides
int* shortestPathParent = new int[size + 1];
for(int i = 0; i < size; i++) {shortestPathParent[i] = 0;}
// Dijkstra's algorithm
Locator sl = pq.getLocator(sVert); //locator keeps track of element's memory address
pq.replaceKey(sl,0);
if(sVert > size || dVert > size || sVert < 0 || dVert < 0) throw "No such vertex exists";
vector<Vertex*> del; // removed vertices
vector<Vertex*> vertList = graph.getVertices();
while( !pq.isEmpty()) {
Vertex* u = vertList[pq.minElement() - 1];
Locator locu = pq.getLocator(u->getID());
int ukey = pq.getKey(locu);
pq.removeMin();
del.push_back(u);
vector<Edge*> edges = u->getOutEdges();
for( int i = 0; i < edges.size(); i++ ) {
Vertex* v = edges[i] -> geteVertP();
int w = edges[i] -> getWeight();
Locator locv = pq.getLocator(v->getID());
if(comps++, pq.getKey(locv) > (ukey + w)) //if (d[b] > d[a] + w(a,b))
{
pq.replaceKey(locv, ukey + w); //d[b] = d[a] + w(a,b)
shortestPathParent[v->getID()] = u->getID(); //P[b] = a
}
}
}
Vertex* d = graph.getVertex(dVert);
Vertex* s = graph.getVertex(sVert);
vector<int>vers, wt;
vector<Edge*> e = vertList[dVert-1]->getInEdges();
int c = 0;
vers.push_back(dVert);
if (sVert == dVert)
{
vers.push_back(dVert);
wt.push_back(0);
}
else
{
while (c != sVert)
{
//compare edges
int minw = e[0]->getWeight();
int minv = e[0]->getsVertP()->getID();
for (int i = 0; i < e.size(); i++)
{
if(e[i]->getWeight() < minw)
{
minw = e[i]->getWeight();
minv = e[i]->getsVertP()->getID();
}
}
wt.push_back(minw);
vers.push_back(minv);
c = vertList[minv-1]->getID();
e = vertList[minv-1]->getInEdges();
}
reverse(wt.begin(),wt.end());
reverse(vers.begin(),vers.end());
}
for (int i = 0; i < vers.size()-1; i++)
{
cout << vers[i] << "--[" << wt[i] << "]-->";
}
cout << dVert << endl;
//.........这里部分代码省略.........