C++实现迪杰斯特拉(Dijkstra)算法

#include<iostream>
#include <windows.h>
#define MAX 9
using namespace std;
//网图类
template<class Type>
class NetGraph
{
private:
    Type vexs[MAX];//顶点表
    int arc[MAX][MAX];//邻接矩阵
    int numVertexes;//当前网中顶点数
    int numEdges;//当前网中边数

    int getPosition(Type el);//获取el在顶点表中的位置
public:
    NetGraph();//创建一个网
    //~NetGraph();//析构函数
    void print();//打印邻接矩阵
    void ShortestPath_Dijkstra(int v0, int *P, int *D);//迪杰斯特拉(Dijkstra)算法
};
//获取el在顶点表中的位置
template<class Type>
int NetGraph<Type>::getPosition(Type el)
{
    int i;
    for (i = 0; i < this->numVertexes; i++)
    {
        if (this->vexs[i] == el)
            return i;
    }
    return -1;
}
//创建一个网,手动输入
template<class Type>
NetGraph<Type>::NetGraph()
{
    Type c1, c2;
    int p1, p2;
    int i, j;
    int weight;//权值
    cout << "Input Vertexes number: ";
    cin >> this->numVertexes;
    cout << "Input Edges number: ";
    cin >> this->numEdges;
    //检测顶点和边数输入的合法性
    if (this->numVertexes < 1 || this->numEdges<1 || this->numEdges>this->numVertexes*(this->numVertexes - 1))
    {
        cout << "Input invalid!" << endl;
        return;
    }
    //初始化顶点
    for (i = 0; i < this->numVertexes; i++)
    {
        cout << "Input a Type data(" << i + 1 << "): ";
        cin >> this->vexs[i];
    }
    //初始化边
    for (i = 0; i < this->numVertexes; i++)
    {
        for (j = 0; j < this->numVertexes; j++)
        {
            if (i == j)
                this->arc[i][j] = 0;
            else
                this->arc[i][j] = 65535;
        }
    }
    for (i = 0; i < this->numEdges; i++)
    {
        cout << "Input a arc:  ";
        cin >> c1;
        cout << "Input a arc:  ";
        cin >> c2;

        p1 = getPosition(c1);
        p2 = getPosition(c2);
        if (p1 == -1 || p2 == -1 || p1 == p2)
        {
            cout << "Input Egde error!" << endl;
            return;
        }
        cout << "Input a weight: ";
        cin >> weight;
        this->arc[p1][p2] = weight;
        this->arc[p2][p1] = weight;
    }
}
//打印邻接矩阵
template<class Type>
void NetGraph<Type>::print()
{
    int i, j;
    cout << "***********************************" << endl;
    cout << "邻接矩阵:" << endl;
    for (i = 0; i < this->numVertexes; i++)
    {
        for (j = 0; j < this->numVertexes; j++)
            cout << this->arc[i][j] << "\t";
        cout << endl;
    }
}
//迪杰斯特拉(Dijkstra)算法
template<class Type>
void NetGraph<Type>::ShortestPath_Dijkstra(int v0, int *P, int *D)
{
    int v, w, k, min;
    int final[MAX];//final[w]=1表示求得顶点v0至vw的最短路径
    for (v = 0; v < this->numVertexes; v++)
    {
        final[v] = 0;//全部顶点初始化为未知最短路径状态
        D[v] = this->arc[v0][v];//将与v0点有连线的顶点上加上权值
        P[v] = 0;//初始化路径数组P为0
    }
    final[v0] = 1;//v0至v0不需要求路径
    D[v0] = 0;//v0至v0权值为0
    //开始主循环,每次求得v0到某个v顶点的最短路径
    for (v = 1; v < this->numVertexes; v++)
    {
        min = 65535;
        for (w = 0; w < this->numVertexes; w++)
        {
            if (!final[w] && D[w] < min)
            {
                k = w;//保存
                min = D[w];
            }
        }
        final[k] = 1;
        for (w = 0; w < this->numVertexes; w++)
        {
            if (!final[w] && (min + this->arc[k][w] < D[w]))
            {
                D[w] = min + this->arc[k][w];
                P[w] = k;
            }
        }
    }
}


int main()
{
    int i;
    int P[MAX], D[MAX];
    NetGraph<char> graph;
    graph.print();
    graph.ShortestPath_Dijkstra(0, P, D);
    cout << "*********************" << endl;
    for (i = 0; i < MAX; i++)
        cout << D[i] << " ";
    cout << endl;
    cout << "*********************" << endl;
    for (i = 0; i < MAX; i++)
        cout << P[i] << " ";
    cout << endl;
    system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值