DS图—图的最短路径【用迪迦特斯拉算法求最短路径】

该博客介绍了一个使用迪杰斯特拉算法求解图中顶点间最短路径的C++程序。程序首先读取图的邻接矩阵和起始顶点,然后通过迪杰斯特拉算法找出从起始顶点到所有其他顶点的最短路径。输出格式为每行显示起始顶点到目标顶点的最短距离和路径。示例输入和输出展示了算法的运行情况。

题目描述
给出一个图的邻接矩阵,输入顶点v,用迪杰斯特拉算法求顶点v到其它顶点的最短路径。

输入
第一行输入t,表示有t个测试实例

第二行输入顶点数n和n个顶点信息

第三行起,每行输入邻接矩阵的一行,以此类推输入n行

第i个结点与其它结点如果相连则为距离,无连接则为0,数据之间用空格

隔开。第四行输入v0,表示求v0到其他顶点的最短路径距离

以此类推输入下一个示例

输出
对每组测试数据,输出:

每行输出v0到某个顶点的最短距离和最短路径

每行格式:v0编号-其他顶点编号-最短路径值----[最短路径]。没有路径输出:v0编号-其他顶点编号–1。具体请参考示范数据

样例输入
2
5 0 1 2 3 4
0 5 0 7 15
0 0 5 0 0
0 0 0 0 1
0 0 2 0 0
0 0 0 0 0
0
6 V0 V1 V2 V3 V4 V5
0 0 10 0 30 100
0 0 5 0 0 0
0 0 0 50 0 0
0 0 0 0 0 10
0 0 0 20 0 60
0 0 0 0 0 0
V0
样例输出
0-1-5----[0 1 ]
0-2-9----[0 3 2 ]
0-3-7----[0 3 ]
0-4-10----[0 3 2 4 ]
V0-V1–1
V0-V2-10----[V0 V2 ]
V0-V3-50----[V0 V4 V3 ]
V0-V4-30----[V0 V4 ]
V0-V5-60----[V0 V4 V3 V5 ]

#include <iostream>
using namespace std;
const int INFINITY=1000;  //无穷大
class Graph{
    int    VertexNum;      //顶点数
    string *Vertex;        //顶点数组
    int    **AdjMatrix;      //邻接矩阵
    string *Path;            //存放每个顶点对应的最短路径
    int    *Dest;            //记录每个顶点最短路径长度
    string start;            //存放起始顶点
public:
    Graph()
    {
        cin>>VertexNum;
        Vertex=new string[VertexNum];
        AdjMatrix=new int*[VertexNum];
        Path=new string[VertexNum];
        Dest=new int[VertexNum];
        for(int i=0;i<VertexNum;i++){
            AdjMatrix[i]=new int[VertexNum];
            cin>>Vertex[i];
        }
        for(int i=0;i<VertexNum;i++){
            for(int j=0;j<VertexNum;j++){
                cin>>AdjMatrix[i][j];
                if(AdjMatrix[i][j]==0)AdjMatrix[i][j]=INFINITY;
            }
        }
        cin>>start;
    }
    void ShortestPath(string StartVexChar)
    {
        int i, j, m, StartVex, CurrentVex, MinDest;
        bool *Final=new bool[VertexNum];   //记录顶点是否已被选中
        for (i=0; i<VertexNum; i++) {//找到开始顶点在数组中的序号
            if (Vertex[i] == StartVexChar) {StartVex = i; break;}
        }
        for (i=0; i<VertexNum; i++) {     //初始化
            Path[i] = StartVexChar+" ";    //路径从StartVexChar开始
            Dest[i] = INFINITY; //所有顶点到开始顶点之间的距离初值设为无穷大
            if (AdjMatrix[StartVex][i] < INFINITY) {
                // 在开始顶点与当前顶点之间存在弧
                Dest[i] = AdjMatrix[StartVex][i];
                Path[i] += Vertex[i]+" ";
            }
            Final[i] = false;
        }
        Dest[StartVex] = 0;       //初始顶点到自己的最短距离设置为0,最后不输出
        Final[StartVex] = true;      //设置初始结点的访问标志位为true
        for (i=0; i<VertexNum-1; i++) {  //运行n-1趟
            MinDest = INFINITY;
            for (j=0; j<VertexNum; j++) {   //找当前未处理过顶点中到开始顶点最近的顶点
                if (Final[j] == false) {
                    if (Dest[j] < MinDest) {
                        CurrentVex = j;      // CurrentVex表示离开始顶点最近的顶点
                        MinDest = Dest[j];
                    }
                }
            }
            Final[CurrentVex] = true;        //设置新找到的结点的访问标志位为true
            for (j=0; j<VertexNum; j++) {   // 更新当前最短路径及距离
                if ((Final[j]==false) && (MinDest+AdjMatrix[CurrentVex][j] < Dest[j])){
                    Dest[j]=MinDest+AdjMatrix[CurrentVex][j];//更新顶点j到开始顶点的最短距离
                    Path[j]= Path[CurrentVex];               //更新顶点j到开始顶点的路径
                    Path[j]+= Vertex[j]+" ";
                }
            }
        }
    }
 
    void findMinPath(){
        ShortestPath(start);
        for(int i=1;i<VertexNum;i++){
            cout<<Vertex[0]<<"-"<<Vertex[i]<<"-";
            if(Dest[i]>=INFINITY){
                cout<<"-1"<<endl;
                continue;
            }
            cout<<Dest[i]<<"----[";
            cout<<Path[i]<<"]"<<endl;
        }
    }
 
 
};
int main()
{
    int t;
    cin>>t;
    while(t--){
        Graph graph;
        //cout<<endl;
        graph.findMinPath();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值