Dijkstra最短路(迪克斯特拉算法)(C++实现)(邻接矩阵)

Dijkstra最短路(迪克斯特拉算法)(C++实现)(邻接矩阵)

实现代码

/*
author : eclipse
email  : eclipsecs@qq.com
time   : Sun Apr 19 22:11:03 2020
*/
#include<bits/stdc++.h>
using namespace std;

const int INF = 0xFFFF;

vector<int> path;
vector< vector<int> > arcs;
vector<int> dist;

void dijkstra(int vexNum, int source) {
    source--;
    vector<bool> tag;
    tag.resize(vexNum);
    for (int i = 0; i < vexNum; i++) {
        tag[i] = false;
    }
    path[source] = source + 1;
    dist[source] = 0;
    int v = source;
    for (int i = 0; i < vexNum; i++) {
        int minDist = INF;
        for (int w = 0; w < vexNum; w++) {
            if (!tag[w]) {
                if (dist[w] < minDist) {
                    v = w;
                    minDist = dist[w];
                }
            }
        }
        tag[v] = true;
        for (int w = 0; w < vexNum; w++) {
            if (!tag[w] && (dist[w] > minDist + arcs[v][w])) {
                dist[w] = minDist + arcs[v][w];
                path[w] = v + 1;
            }
        }
    }
}

void print() {
    printf("Dijkstra Single Source Shortest Path edges weight\n");
    for (int i = 0; i < dist.size(); i++) {
        printf("%d ", dist[i]);
    }
    printf("\nDijkstra Single Source Shortest Path\n");
    for (int i = 0; i < path.size(); i++) {
        printf("%d ", path[i]);
    }
    printf("\n");
}

int main(int argc, char const *argv[])
{
    int vexNum, edgeNum, source;
    while (~scanf("%d%d%d", &vexNum, &edgeNum, &source)) {
        path.resize(vexNum);
        arcs.resize(vexNum);
        dist.resize(vexNum);
        for (int i = 0; i < vexNum; i++) {
            dist[i] = INF;
            arcs[i].resize(vexNum);
            for (int j = 0; j < vexNum; j++) {
                arcs[i][j] = INF;
            }
        }
        for (int i = 0; i < edgeNum; i++) {
            int from, to, weight;
            scanf("%d%d%d", &from, &to, &weight);
            arcs[from - 1][to - 1] = weight;
            arcs[to - 1][from - 1] = weight;
        }
        dijkstra(vexNum, source);
        print();
        path.clear();
        for (int i = 0; i < vexNum; i++) {
            arcs[i].clear();
        }
        arcs.clear();
        dist.clear();
    }
    return 0;
}

算法思路

  • 思路
    上述算法思路与堆优化的Dijkstra算法类似,只是将堆优化得到最小代价边部分改成了for循环遍历搜索
  • 复杂度
    上述算法时间复杂度为O(|V| ^ |V|)
  • 注释
    主要代码及注释与堆优化的Dijkstra最短路算法相似,在此不再赘述
  • 样例
    样例及图解与堆优化的Dijkstra最短路算法相似,在此不再赘述
  • 堆优化Dijkstra算法

测试数据

5 6 1
1 2 2
1 4 8
2 3 3
2 5 5
3 4 2
3 5 6

5 6 2
1 2 2
1 4 8
2 3 3
2 5 5
3 4 2
3 5 6

5 6 3
1 2 2
1 4 8
2 3 3
2 5 5
3 4 2
3 5 6

5 6 4
1 2 2
1 4 8
2 3 3
2 5 5
3 4 2
3 5 6

5 6 5
1 2 2
1 4 8
2 3 3
2 5 5
3 4 2
3 5 6

输出结果

Dijkstra Single Source Shortest Path edges weight
0 2 5 7 7
Dijkstra Single Source Shortest Path
1 1 2 3 2
Dijkstra Single Source Shortest Path edges weight
2 0 3 5 5
Dijkstra Single Source Shortest Path
2 2 2 3 2
Dijkstra Single Source Shortest Path edges weight
5 3 0 2 6
Dijkstra Single Source Shortest Path
2 3 3 3 3
Dijkstra Single Source Shortest Path edges weight
7 5 2 0 8
Dijkstra Single Source Shortest Path
2 3 4 4 3
Dijkstra Single Source Shortest Path edges weight
7 5 6 8 0
Dijkstra Single Source Shortest Path
2 5 5 3 5

鸣谢

最后

  • 对Dijkstra算法进行堆优化可以将算法时间复杂度降低至O(|V| ^ 2 * lg|E|)
  • 堆优化Dijkstra算法
  • 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值