单源最短路问题(1)—朴素Dijkstra算法及其堆优化

常见的最短路问题分为两类:单源最短路(从一个点到其他所有点)、多源汇最短路(任意两点)

1、在单源最短路问题中,若所有的边都是非负数,使用Dijkstra算法;若存在负权边,那么可以使用Bellman-Ford算法,SPFA是对前者优化。关于算法原理的介绍有很多,这里不再详述。

(1)朴素Dijkstra算法,时间复杂度O(n ^ 2),通常在稠密图的时候使用(边的数量级大概为点的数量级的平方),使用邻接矩阵存图。可以根据题目要求的时间复杂度来选择使用哪种方法。

第一次循环:t = 1, dist[1] = 0, dist[2] = 2, dist[3] = 3, dist[4] = 0x3f3f3f3f, st[1] = true

第二次循环:t = 2, dist[1] = 0; dist[2] = 2, dist[3] = 3, dist[4] = 3, st[2] = true

第三次循环:t = 3, dist[1] = 0, dist[2] = 2, dist[3] = 3, dist[4] = 3, st[3] = true

结束

示例代码:提供n个点,m条边的有向边,存在自环和重边,计算一点到另一点的最短距离(题目出自AcWing)

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
        
const int N = 510;

int n, m;
int g[N][N];    //邻接矩阵存稠密图
int dist[N];    //存每一个点到起点的最短距离
bool st[N];     //记录每一个点是否被用来执行过松弛操作

int dijkstra()
{
    memset(dist, 0x3f, sizeof dist);   //将距离初始化为一个很大的值
    dist[1] = 0;        //取1为起点,起点可任取
    
    for(int i = 0; i < n - 1; i ++)     //执行n - 1次, 因为最后一个数字不需要用它来进行松弛操作了
    {
        int t = -1; //存st[]为false的点中,那个点的dist[]最小
        for(int j = 1; j <= n; j ++)
        {
            if(!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        }
        
        for(int j = 1; j <= n; j ++)    //用t这个点来更新st[]为false的所有点的dist[]
        {
            dist[j] = min(dist[j], dist[t] + g[t][j]);
        }
        
        st[t] = true;   //表示t这个点被用来执行过松弛操作了,也表示dist[t],已经确定最小值了
    }
    
    if(dist[n] == 0x3f3f3f3f) return -1;    //取n为终点,-1表示不能到达,终点可任取
    return dist[n];
}

int main()
{
    scanf("%d%d", &n, &m);
    
    memset(g, 0x3f, sizeof g);  //将每条边初始化为一个很大的值
    while(m --)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        g[a][b] = min(g[a][b], c);  //处理重边
    }
    
    printf("%d\n", dijkstra());
    
    return 0;
}

(2)堆优化版的Dijkstra算法,通常稀疏图使用(边与点的数量级大概相当),用邻接表存图,时间复杂度O(mlogn)

这里使用一个小根堆优化朴素方法里面,寻找最小的dist的点,使用了贪心的思想。

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>

using namespace std;

typedef pair<int, int> PII;

const int N = 1500010;

int n, m;
int h[N], w[N], e[N], ne[N], idx;       //邻接表存图
int dist[N];
bool st[N];

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}

int dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;        //取1为起点
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1});      //first 为距离, second 为点的编号
    
    while(heap.size())
    {
        auto t = heap.top();
        heap.pop();
        
        int point = t.second, distance = t.first;
        
        if(st[point]) continue;     //这个点被用过了
        st[point] = true;
        
        for(int i = h[point]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > distance + w[i])
            {
                dist[j] = distance + w[i];
                heap.push({dist[j], j});    //最小的距离会自动排到堆顶
            }
        }
    }
    
    if(dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main()
{
    scanf("%d%d", &n, &m);
    
    memset(h, -1, sizeof h);
    while(m --)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);
    }
    
    printf("%d\n", dijkstra());
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值