迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径。
它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想),直到扩展到终点为止。
通过Dijkstra计算图G中的最短路径时,需要指定起点s(即从顶点s开始计算)。
Dijkstra算法算是贪心思想实现的,首先把起点到所有点的距离存下来找个最短的,然后松弛一次再找出最短的,所谓的松弛操作就是,遍历一遍看通过刚刚找到的距离最短的点作为中转站会不会更近,如果更近了就更新距离,这样把所有的点找遍之后就存下了起点到其他所有点的最短距离。
https://blog.youkuaiyun.com/lbperfect123/article/details/84281300
https://blog.youkuaiyun.com/heroacool/article/details/51014824
以下代码出自: https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/greedy_algorithms/Dijkstra.cpp
#include <iostream>
#include <limits.h>
using namespace std;
//Wrapper class for storing a graph
class Graph
{
public:
int vertexNum;
int **edges;
//Constructs a graph with V vertices and E edges
Graph(const int V)
{
// initializes the array edges.
this->edges = new int *[V];
for (int i = 0; i < V; i++)
{
edges[i] = new int[V];
}
// fills the array with zeros.