弗洛伊德算法Floyed(求各顶点间最短路径):可打印最短路径

本文详细介绍了如何使用C++编程语言实现弗洛伊德算法来解决有向图中的最短路径问题。通过输入顶点数量和边信息,算法能够自动计算并输出任意两个顶点之间的最短路径长度及其路径。包括创建有向图、调用弗洛伊德算法、打印最短路径矩阵以及展示特定顶点对之间的最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>  
#include <string>  
#include <iomanip>  
using namespace std;  
  
#define INFINITY 65535  
#define MAX_VERTEX_NUM 10  
  
typedef struct MGraph{  
    string vexs[10];//顶点信息  
    int arcs[10][10];//邻接矩阵  
    int vexnum, arcnum;//顶点数和边数  
}MGraph;  
  
int LocateVex(MGraph G, string u)//返回顶点u在图中的位置  
{  
    for(int i=0; i<G.vexnum; i++)  
        if(G.vexs[i]==u)  
            return i;  
    return -1;  
}  
  
void CreateDN(MGraph &G)//构造有向图  
{  
    string v1, v2;  
    int w;  
    int i, j, k;  
    cout<<"请输入顶点数和边数:";  
    cin>>G.vexnum>>G.arcnum;  
  
    cout<<"请输入顶点:";  
    for(i=0; i<G.vexnum; i++)  
        cin>>G.vexs[i];  
  
    for(i=0; i<G.vexnum; i++)  
        for(j=0; j<G.vexnum; j++)  
            G.arcs[i][j]=INFINITY;  
  
    cout<<"请输入边和权值:"<<endl;  
    for(k=0; k<G.arcnum; k++)  
    {  
        cin>>v1>>v2>>w;  
        i=LocateVex(G, v1);  
        j=LocateVex(G, v2);  
        G.arcs[i][j]=w;  
    }  
}  
  
//弗洛伊德算法求每一对顶点间的最短路径  
//p[v][w][i]表示当前求得的顶点v到顶点w的最短路径中的第i+1个顶点,这是打印最短路径的关键  
//D[v][w]表示当前求得的顶点v到顶点w的最短路径的长度  
void ShortestPath_FLOYD(MGraph G, int p[MAX_VERTEX_NUM][MAX_VERTEX_NUM][MAX_VERTEX_NUM], int D[][MAX_VERTEX_NUM])  
{  
    int u, v, w, i, j;  
      
    for(v=0; v<G.vexnum; v++)  
        for(w=0; w<G.vexnum; w++)  
        {  
            D[v][w]=G.arcs[v][w];  
            for(u=0; u<G.vexnum; u++)  
                p[v][w][u]=-1;  
            if(D[v][w] < INFINITY)  
            {  
                p[v][w][0]=v;  
                p[v][w][1]=w;  
            }  
        }  
          
        for(u=0; u<G.vexnum; u++)  
            for(v=0; v<G.vexnum; v++)  
                for(w=0; w<G.vexnum; w++)  
                    if(D[v][u] < INFINITY && D[u][w] < INFINITY && D[v][u]+D[u][w] < D[v][w])  
                    {  
                        //更新D  
                        D[v][w]=D[v][u]+D[u][w];  
                        //更新p,从v到w的路径是从v到u,再从u到w的所有路径  
                        for(i=0; i<G.vexnum; i++)  
                        {  
                            if(p[v][u][i]!=-1)  
                                p[v][w][i]=p[v][u][i];  
                            else  
                                break;  
                        }  
                        for(j=1; j<G.vexnum; j++)//注意:这里j从1开始而不是从0开始,因为从v到u的路径最后一个顶点是u, 而从u到w的路径第一个顶点是u,只需打印u一次即可。  
                        {  
                            if(p[u][w][j]!=-1)  
                                p[v][w][i++]=p[u][w][j];  
                            else  
                                break;  
                        }  
                          
                    }  
                    
}  
  
void main()  
{  
    MGraph g;  
    int p[MAX_VERTEX_NUM][MAX_VERTEX_NUM][MAX_VERTEX_NUM];  
    int D[MAX_VERTEX_NUM][MAX_VERTEX_NUM];  
  
    CreateDN(g);  
    for(int  i=0; i<g.vexnum; i++)  
        g.arcs[i][i]=0;  
    ShortestPath_FLOYD(g, p, D);  
  
    cout<<"d矩阵(最短路径长度矩阵):"<<endl;  
    for(i=0; i<g.vexnum; i++)  
    {  
        for(int j=0; j<g.vexnum; j++)  
            cout<<setw(5)<<D[i][j]<<" ";  
        cout<<endl;  
    }  
  
    cout<<endl;  
    cout<<"各顶点间最短长度及路径如下:"<<endl;  
    for(i=0; i<g.vexnum; i++)  
    {  
        for(int j=0; j<g.vexnum; j++)  
        {  
            if(i!=j)  
            {  
                if(D[i][j]!=INFINITY)  
                {  
                    cout<<g.vexs[i]<<"到"<<g.vexs[j]<<"的最短长度为:"<<setw(5)<<D[i][j]<<", 最短路径为:";  
                    for(int k=0; k<g.vexnum; k++)  
                    {  
                        if(p[i][j][k]!=-1)  
                            cout<<g.vexs[p[i][j][k]]<<" ";  
                        else  
                            break;  
                    }  
                    cout<<endl;  
                }  
                else  
                    cout<<g.vexs[i]<<"到"<<g.vexs[j]<<"不可达"<<endl;  
            }  
              
        }  
        cout<<endl;         
            
    }  
  
  
  
}  

测试一:

测试二:


转载于:https://www.cnblogs.com/tham/p/6827201.html

6-3 最短路径弗洛伊德算法) 分数 10 作者 王东 单位 贵州师范学院 试实现弗洛伊德最短路径算法。 函数接口定义: void ShortestPath_Floyed(AMGraph G); 其中 G 是基于邻接矩阵存储表示的有向图。 裁判测试程序样例: #include <iostream> using namespace std; #define MaxInt 32767 #define MVNum 100 typedef char VerTexType; typedef int ArcType; int Path[MVNum][MVNum]; int D[MVNum][MVNum]; typedef struct{ VerTexType vexs[MVNum]; ArcType arcs[MVNum][MVNum]; int vexnum,arcnum; }AMGraph; void CreateUDN(AMGraph &G);//实现细节隐藏 void ShortestPath_Floyed(AMGraph G); void DisplayPath(AMGraph G , int begin ,int temp ){ if(Path[begin][temp] != -1){ DisplayPath(G , begin ,Path[begin][temp]); cout << G.vexs[Path[begin][temp]] << "->"; } } int main(){ AMGraph G; char start , destination; int num_start , num_destination; CreateUDN(G); ShortestPath_Floyed(G); cin >> start >> destination; num_start = LocateVex(G , start); num_destination = LocateVex(G , destination); DisplayPath(G , num_start , num_destination); cout << G.vexs[num_destination]<<endl; cout << D[num_start][num_destination]; return 0; } /* 请在这里填写答案 */ 输入样例: 第1行输入结点数vexnum和边数arcnum。第2行输入vexnum个字符表示结点的值,接下来依次输入arcnum行,每行输入3个值,前两个字符表示结点,后一个数表示两个结点之边的权值。最后一行输入源点及终点。 6 8 012345 0 5 100 0 2 10 0 4 30 1 2 5 2 3 50 3 5 10 4 3 20 4 5 60 0 5 输样例: 第一行输源点到终点的最短路径,第二行输源点到终点的最短路径距离。 0->4->3->5 60 hh.png 代码长度限制 16 KB 时限制 400 ms 内存限制 64 MB
最新发布
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值