我对 Dijkstra 的算法使用以下代码:
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;
}
现在我使用自己创建的名为 Queue 的类:
Queue *q = new Queue(maxNodes)
是否有任何 C++ 标准队列可以用来代替我自己的实现。
我只需要这个功能:
Queue *q = new Queue(maxNodes);
q->Enqueue(posStart);
q.Dequeue(next);
谢谢!