场景
- 给定带权网络G,远点s 对于所有的其他顶点v, s到v的最短通路是多少?该通路由哪些边构成
性质
- 单调性
- 最短路径树上的任一顶点v到必定是源点s到v的最短路径
- 歧义性
- 无环性
和最小支撑树的区别
- 最小支撑树求的是整个拓扑图的所有路径之和最小,不能保证任意两点之间的路径最小应用场景: 快递车将霍送到城市的每一个快递点,不走重复的路而且时间最短
- 最短路径是保证源点到任一一点的路径最短应用场景: 地图寻径,怎么确保可以最短时间到达想要去的地方
代码实现
-
-
- template <typename Tv, typename Te> struct DijkstraPU{
-
- virtual void operator()(Graph<Tv, Te>* graph, int v int u) {
-
- if (graph->status(u) != UNDISCOVER) {
- return;
- }
-
-
- if (graph->priority(u) > graph->priority(v) + graph->weight(v, u)) {
- graph->priority(u) = graph->priority(v) + graph->weight(v, u)
- graph->parent(u) = v;
- }
- }
- };
-
-
- template <typename PU> void pfs(int v, PU prioUpdater){
-
- reset();
-
-
- int clock = 0;
- int s = v;
-
-
- do {
-
- if (status(v) == UNDISCOVERED) {
- PFS(v, prioUpdater);
- }
-
-
- v = ++v%n;
- } while (s != v);
- }
-
-
- template <typename PU> void PFS(int v, PU prioUpdater) {
-
- priority(v) = 0;
- status(v) = VISITED;
-
-
- parent(s) = -1;
-
-
- while(true) {
-
- for (int w = firstNbr(s); w > -1 ; w = nextNbr(s, w)) {
- prioUpdater(this,s, w);
- }
-
-
- int shortest = INT_MAX;
- for (int w =0; w < n ; w++) {
- if (status(w) == UNDISCOVERED && priority(w) < shortest) {
- shortest = priority(w);
- s = w;
- }
- }
-
-
-
-
- if (status(s) == VISITED) {
- break;
- }
-
-
- status(s) = VISITED;
- type(parent(s), s) = TREE;
- }
- }
-
-