dijkstra算法--寻找最短路径

转自https://blog.csdn.net/heroacool/article/details/51014824

基本思想

  1. 通过Dijkstra计算图G中的最短路径时,需要指定起点s(即从顶点s开始计算)。

  2. 此外,引进两个集合S和U。S的作用是记录已求出最短路径的顶点(以及相应的最短路径长度),而U则是记录还未求出最短路径的顶点(以及该顶点到起点s的距离)。

  3. 初始时,S中只有起点s;U中是除s之外的顶点,并且U中顶点的路径是”起点s到该顶点的路径”。然后,从U中找出路径最短的顶点,并将其加入到S中;接着,更新U中的顶点和顶点对应的路径。 然后,再从U中找出路径最短的顶点,并将其加入到S中;接着,更新U中的顶点和顶点对应的路径。 … 重复该操作,直到遍历完所有顶点。

操作步骤

  1. 初始时,S只包含起点s;U包含除s外的其他顶点,且U中顶点的距离为”起点s到该顶点的距离”[例如,U中顶点v的距离为(s,v)的长度,然后s和v不相邻,则v的距离为∞]。

  2. 从U中选出”距离最短的顶点k”,并将顶点k加入到S中;同时,从U中移除顶点k。

  3. 更新U中各个顶点到起点s的距离。之所以更新U中顶点的距离,是由于上一步中确定了k是求出最短路径的顶点,从而可以利用k来更新其它顶点的距离;例如,(s,v)的距离可能大于(s,k)+(k,v)的距离。

  4. 重复步骤(2)和(3),直到遍历完所有顶点。

 多谢博主的叙述,我其实一直对于dijkstra算法有疑惑  就是关于集合s,u的作用  搞清楚了就明白了 下面是我写的一个例子

#include<iostream>
using namespace std;
#define INF 100
//0:北区宿舍
//1:第二食堂
//2:2区
//3:3区
//4:4区
//5:5区
//6:6区
//7:7区
//8:8区
//9:图书馆

//图的邻接矩阵
int matrix[10][10] = {
    0,1,3,23,INF,15,6,7,28,12,
    0,0,12,2,13,3,14,6,INF,10,
    0,0,0,11,1,21,INF,24,5,38,
    0,0,0,0,11,13,4,25,6,38,
    0,0,0,0,0,22,12,14,INF,18,
    0,0,0,0,0,0,INF,12,4,48,
    0,0,0,0,0,0,0,22,24,18,
    0,0,0,0,0,0,0,0,21,26,
    0,0,0,0,0,0,0,0,0,26,
    0,0,0,0,0,0,0,0,0,0
};
//各个景点名字
char name[10][100] = {
    "北区宿舍",
    "第二食堂",
    "二区教学楼",
    "三区教学楼",
    "四区教学楼",
    "五区教学楼",
    "六区教学楼",
    "七区教学楼",
    "八区教学楼",
    "图书馆"
};
//景点的详细内容
char content[10][500] = {
    "提供住宿",
    "提供饮食",
    "教学区2",
    "教学区3",
    "教学区4",
    "教学区5",
    "教学区6",
    "教学区7",
    "教学区8",
    "提供阅读,书籍借阅与归还"
};

//S的作用是记录已求出最短路径的顶点(以及相应的最短路径长度),
//而U则是记录还未求出最短路径的顶点(以及该顶点到起点s的距离)。
int s[10], u[10];


bool flag[10] = { false };//用以标志已经存储在s[]中的点


//找到与i节点相连路径的最短节点   返回其下标
int min(int i) {
    int min = INF;
    int tag = 0;
    for (int j = 0; j < 10; j++)
    {
        if (j == i)  //不能返回自身  即权重为0的点
            continue;
        else if(!flag[j])  //如果没被标志
        {
            if (min > matrix[i][j])
            {
                min = matrix[i][j];
                tag = j;
            }
                
        }
    }
    return tag;
}
//介绍图景
void serveintroduce()
{
    int i;
    cout <<endl<< "please input your choice to get more information:";
    cin >> i;
    while (i > 9 || i < 0)
    {
        cout << "please input the true number!!!";
        cout << "please input your choice to get more information:";
        cin >> i;
    }

    cout << "the number you have input is  :" << i << endl;
    cout << "the building of the number is :" << name[i] << endl;
    cout << "this building's function is   :" << content[i] << endl;
}
//找到最短路径  输入出发点与目的地  输出最短路径长度
void findtrade()
{
    int st, en;   //开始节点st   结束节点en
    int pre[10];  //用以记录起始点到各点最短路径的前驱
    memset(s, 0, sizeof(s));
    memset(u, 0, sizeof(u));
    cout << endl << "please input the start point and destination:" << endl;
    cin >> st;
    cin >> en;
    while (st > 9 || st < 0 || en>9 || en < 0)
    {
        cout << "please input the true number!!!";
        cout << endl << "please input the start point and destination:" << endl;
        cin >> st;
        cin >> en;
    }
    if (st == en)
    {
        cout << "原地踏步";
        return;
    }

    for (int i = 0; i < 10; i++)
    {
        s[i] = u[i] = matrix[st][i];
        pre[i] = st;         //前驱
        flag[i] = false;     //刷新标志位  每次执行findtrade()函数都需要刷新标志
    }
    

    //Dijkstra算法
    int m = st;
    flag[st] = true;
    for (int i = 0; i < 10; i++)
    {
        if (m == en)
            break;
        if (s[min(m)] > s[m] + matrix[m][min(m)])
        {
            pre[min(m)] = m;
            s[min(m)] = s[m] + matrix[m][min(m)];
        }
        m = min(m);
        flag[m] = true;
    }
    cout << name[st] << "" << name[en] << "的最短距离为" << s[en] << endl;   //输出最短距离

    m = en;
    cout << name[en];
    while (pre[m] != st)   //打印最短路径
    {
        cout << "<---" << name[pre[m]];
        m = pre[m];
    }
    cout << "<---" << name[st] << endl;
}

int main()
{
    int i = 0;
    cout << "plaese read the review to choose what you want to know" << endl;
    for (i = 0; i < 10; i++)
    {
        cout << i << " :";
        cout << name[i] << " " << endl;
    }

    for (int i = 0; i < 10; i++)
    {
        for (int j = 9; j > i; j--)
            matrix[j][i] = matrix[i][j];
    }

    //for(int i = 0; i < 10; i++)
    //{
    //    for (int j = 0; j < 10; j++)
    //        cout << matrix[i][j]<<" ";
    //    cout << endl;
    //}

    while (i)
    {
        cout << endl<<"please input the num to choose the following choice:" << endl;
        cout << "1, service of introduce; 2, find the trade" << endl;
        cin >> i;
        while (i != 1 && i != 2)
        {
            cout << endl<<"please input the true number!!!" << endl;
            cout << "please input the num to choose the following choice:" << endl;
            cout << "1, service of introduce; 2, find the trade" << endl;
            cin >> i;
        }
        if (i == 1)
            serveintroduce();
        else
            findtrade();
    }

    return 0;
}
 
原文地址:https://www.cnblogs.com/zhengbao/p/9188741.html