ZBLOG

void Dijkstra(ALDirNetwork& graph, int v0, int* dist, int* path) { int mindist, infinity = graph.GetInfinity(); int v, u; for (v = 0; v < graph.GetVexNum(); v++) { dist[v] = graph.GetWeight(v0, v); if (dist[v] == infinity) path[v...

根据您提供的代码,我稍作修改实现了利用Dijkstra算法求指定源点到其余各顶点的最短路径,并输出最短路径及其长度。以下是修改后的代码:

#include <iostream>
#include "ALDirNetwork.h"
#include "SqStack.h"

void Dijkstra(ALDirNetwork<char, int>& graph, int v0, int* dist, int* path)
{
    const int infinity = graph.GetInfinity();
    int vertexCount = graph.GetVexNum();

    for (int v = 0; v < vertexCount; ++v) {
        dist[v] = graph.GetWeight(v0, v);
        if (dist[v] == infinity) {
            path[v] = -1;
        } else {
            path[v] = v0;
        }
    }

    graph.SetVisitedTag(v0, VISITED);

    for (int i = 1; i < vertexCount; ++i) {
        int u = v0;
        int minDist = infinity;

        // Find the unvisited vertex with minimum distance
        for (int v = 0; v < vertexCount; ++v) {
            if (graph.GetVisitedTag(v) == UNVISITED && dist[v] < minDist) {
                u = v;
                minDist = dist[v];
            }
        }

        graph.SetVisitedTag(u, VISITED);

        // Update distances and paths for adjacent vertices
        for (int v = graph.GetFirstAdjvex(u); v != -1; v = graph.GetNextAdjvex(u, v)) {
            if (graph.GetVisitedTag(v) == UNVISITED && minDist + graph.GetWeight(u, v) < dist[v]) {
                dist[v] = minDist + graph.GetWeight(u, v);
                path[v] = u;
            }
        }
    }
}

void OutputShortestPath(ALDirNetwork<char, int>& graph, int v0, int* dist, int* path)
{
    char vertex;
    graph.GetElem(v0, vertex);
    int vertexCount = graph.GetVexNum();

    for (int i = 0; i < vertexCount; ++i) {
        if (i != v0) {
            char destVertex;
            graph.GetElem(i, destVertex);

            if (dist[i] == graph.GetInfinity()) {
                std::cout << "There is no path between " << vertex << " and " << destVertex << "\n";
                continue;
            }

            std::cout << "The shortest path between " << vertex << " and " << destVertex << " is: ";

            // Output the vertices on the shortest path
            SqStack<int> stack;
            int index = i;

            while (path[index] != -1) {
                stack.Push(index);
                index = path[index];
            }

            std::cout << vertex;

            while (!stack.IsEmpty()) {
                char tmp;
                int k;
                stack.Pop(k);
                graph.GetElem(k, tmp);
                std::cout << " -> " << tmp;
            }

			std::cout << "\nThe distance is: " << dist[i] << "\n";
        }
    }
}

int main()
{
    ALDirNetwork<char, int> graph(6); // 创建一个有6个顶点的邻接表表示的有向图

    // 添加边和权值
    graph.AddEdge(0, 1, 2);
    graph.AddEdge(0, 2, 4);
    graph.AddEdge(1, 2, 1);
    graph.AddEdge(1, 3, 4);
    graph.AddEdge(2, 3, 1);
    graph.AddEdge(2, 4, 3);
    graph.AddEdge(3, 4, 5);
    graph.AddEdge(3, 5, 6);
    graph.AddEdge(4, 5, 2);

    int* dist = new int[graph.GetVexNum()];
    int* path = new int[graph.GetVexNum()];

    // 指定源点为顶点0
    Dijkstra(graph, 0, dist, path);

    // 输出最短路径及其长度
    OutputShortestPath(graph, 0, dist, path);

    delete[] dist;
    delete[] path;

    return 0;
}

请根据需要将该代码添加到您的程序中,并根据实际情况进行适当修改。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9390

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?