Floyd算法(多源最短路径问题)

Floyd算法

弗洛伊德算法,常用于多源最短路径问题,即求每对顶点间的最短路径。

image-20210721120937277

/*  
有权图的多源最短路径问题
*/

#include <iostream>
#include <queue>
using namespace std;

#define MaxVertexNum 10
#define infinity 1e9
struct Graph
{
    int edgenum;
    int vertexnum;
    int edgeList[MaxVertexNum][MaxVertexNum];
    int path[MaxVertexNum][MaxVertexNum]; // 前驱结点
    int dist[MaxVertexNum][MaxVertexNum]; // 从源点到其他各顶点当前最短路径长度
};

void BuildGraph(Graph *G)
{
    int start, end;
    cout << "Please enter the number of vertices and edges" << endl;
    cin >> G->vertexnum >> G->edgenum;
    // 图的权重初始化
    for (int i = 1; i <= G->vertexnum; i++)
    {
        for (int j = 1; j <= G->vertexnum; j++)
        {
            if (i == j)
            {
                G->edgeList[i][j] = 0;
            }
            else
            {
                G->edgeList[i][j] = infinity;
            }
            G->dist[i][j] = G->edgeList[i][j];
            G->path[i][j] = -1;
        }
    }
    // 输入权重信息
    for (int i = 1; i <= G->edgenum; i++)
    {
        cout << "Please enter the Start number, end number, weight" << endl;
        cin >> start >> end;
        cin >> G->edgeList[start][end];
        G->dist[start][end] = G->edgeList[start][end];
    }
}

void Floyd(Graph *G)
{
    for (int k = 1; k <= G->vertexnum; k++)
    {
        for (int i = 1; i <= G->vertexnum; i++)
        {
            for (int j = 1; j <= G->vertexnum; j++)
            {
                if (G->dist[i][k] + G->dist[k][j] < G->dist[i][j])
                {
                    G->dist[i][j] = G->dist[i][k] + G->dist[k][j];
                    G->path[i][j] = k;
                }
            }
        }
    }
}

int main()
{
    Graph G;
    BuildGraph(&G);
    Floyd(&G);
    cout << "edge" << endl;
    for (int i = 1; i <= G.vertexnum; i++)
    {
        for (int j = 1; j <= G.vertexnum; j++)
        {
            if (G.edgeList[i][j] != infinity)
            {
                printf("%10d", G.edgeList[i][j]);
            }
            else
            {
                printf("%10s", "infinity");
            }
        }
        cout << endl;
    }
    cout << "distance" << endl;
    for (int i = 1; i <= G.vertexnum; i++)
    {
        for (int j = 1; j <= G.vertexnum; j++)
        {
            cout << G.dist[i][j] << " ";
        }
        cout << endl;
    }
    cout << "path" << endl;
    for (int i = 1; i <= G.vertexnum; i++)
    {
        for (int j = 1; j <= G.vertexnum; j++)
        {
            cout << G.path[i][j] << " ";
        }
        cout << endl;
    }
    system("pause");
    return 0;
}

/*  
4 8
1 4 4
4 1 5
4 3 12
3 4 1
1 3 6
3 1 7
1 2 2
2 3 3
*/

可以这么理解,将第一层循环的 k 看成是中转点,嵌套的2层循环里得到的 i,j 一对顶点,求的就是 ij 的距离。判断从 i 出发经由 k 再到 j 的路径是否比当前储存的路径短,然后更新。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值