I use following code for the Dijkstra's algoritm:
int Graph::ShortestPath(Vertex *start, Vertex *end)
{
int posStart = IndexOfNode(start);
int posEnd = IndexOfNode(end);
int result;
bool visit[cntNodes];
int distance[cntNodes];
// Initialization: set every distance to -1 until we discover a path
for (int i = 0; i < cntNodes; i++) {
visit[i] = false;
distance[i] = -1;
} // for
// The distance from the source to the source is defined to be zero
distance[posStart] = 0;
Queue *q = new Queue(maxNodes);
q->Enqueue(posStart);
while (!q->Empty()) {
int next, alternative;
q.Dequeue(next);
for (int i = 0; i < cntNodes; i++) {
if (adjmatrix[next][i]) {
alternative = distance[next] + adjmatrix[next][i];
if (visit[i]) {
if (alternative < distance[i]) {
distance[i] = alternative;
q.Enqueue(i);
} // if
}// if
else {
distance[i] = alternative;
visit[i] = true;
q.Enqueue(i);
} // else
} // if
} // for
}
delete q;
result = distance[posEnd];
delete[] visit;
delete[] distance;
return result;
}
Right now I use an own created class called Queue:
Queue *q = new Queue(maxNodes)
Is there any C++ standard Queue which I could use instead of my own implementation.
I only need this functionality:
Queue *q = new Queue(maxNodes);
q->Enqueue(posStart);
q.Dequeue(next);
Thanks!
priority_queue非常适合Dijkstra的算法,并降低了算法的运行时复杂度(与普通队列相比)
Yes. Please check the Standard Template Library for this and numerous other collection classes.
Here's a link: http://www.cplusplus.com/reference/queue/queue/
There are at least three things that come to mind:
There are also other containers and algorithms that can be found in C++ Standard Library.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.