850. Dijkstra求最短路 II

题目

给定一个 n 个点 m 条边的有向图,图中可能存在重边和自环,所有边权均为非负值。

请你求出 1 号点到 n 号点的最短距离,如果无法从 1 号点走到 n 号点,则输出 −1。

输入格式
第一行包含整数 n 和 m。

接下来 m 行每行包含三个整数 x,y,z,表示存在一条从点 x 到点 y 的有向边,边长为 z。

输出格式
输出一个整数,表示 1 号点到 n 号点的最短距离。

如果路径不存在,则输出 −1。

数据范围
1≤n,m≤1.5×105,
图中涉及边长均不小于 0,且不超过 10000。
数据保证:如果最短路存在,则最短路的长度不超过 109。

输入样例:
3 3
1 2 2
2 3 1
1 3 4
输出样例:
3

思路

这题给的数据中点和边的数量差不多,是稀疏图,用邻接表进行存储,由于点的数量过多,会导致遍历未处理的最短距离节点的时候超时

TimeOut代码

#include <iostream>
#include <cstring>

using namespace std;
const int N = 200010;
int n, m;
int h[N], e[N], ne[N], idx, len[N];
int dist[N];
bool st[N];

void add(int a, int b, int c){
    if(a == b) return;//解决自环
    // 解决重边
    for(int i = h[a]; i != -1; i = ne[i]){
        int j = e[i];
        if(b == j){//有重边
            len[i] = min(len[i], c);//取重边的较短边
            return;//结束
        }
    }
    len[idx] = c, e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

int dijkstra(){
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    
    for(int i = 0; i < n-1; i++){
        int t = -1;
        for(int j = 1; j <= n; j++){
            if(!st[j] && (t == -1 || dist[t] > dist[j])) t = j;
        }
        st[t] = true;
        
        for(int j = h[t]; j != -1; j = ne[j]){
            int k = e[j];
            dist[k] = min(dist[k], dist[t] + len[j]);
        }
    }
    if(dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main()
{
    cin >> n >> m;
    
    memset(h, -1, sizeof h);
    
    while(m --){
        int a, b, c;
        cin >> a >> b >> c;
        add(a,b,c);
    }
    
    cout << dijkstra() << endl;
    
    return 0;
}

堆优化查找未处理最短距离的节点代码:

时间消耗最多的是遍历查找未处理的最短距离的节点,因此维护一个小根堆,查找的时间复杂度为O(1),一次插入并维护的时间复杂度为logN,总共插入m次,因此时间复杂度为O(mlogn)

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

using namespace std;

typedef pair<int, int> PII;

const int N = 200010;
int n, m;
int h[N], e[N], ne[N], idx, w[N];
int dist[N];
bool st[N];


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

int dijkstra(){
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    
    priority_queue<PII, vector<PII>, greater<PII>> heap;//优先队列,小根堆,根据第一个值进行排序,即距离
    
    heap.push({0,1});//距离1号点的距离为0
    while(heap.size()){
        auto t = heap.top();
        heap.pop();
        
        int d = t.first, id = t.second;//距离和编号
        
        // 特判,如果当前点已经处理过,则继续查找下一个没有处理过的点
        if(st[id]) continue;
        st[id] = true;//标记当前点已经处理过
        
        // 查找当前点所连接的相邻点
        for(int i = h[id]; i != -1; i = ne[i]){
            int j = e[i];//相邻点
            int weight = w[i];//距离当前相邻点的边权
            if(dist[j] > dist[id] + weight){
                dist[j] = dist[id] + weight;
                heap.push({dist[j], j});//把更新后的最短距离和节点编号放入堆中
            }
        }
    }
    
    // for(int i = 0; i < n-1; i++){//总共n-1次循环
    // // 查找未处理的最短距离的节点这里可以进行小根堆优化
    //     int t = -1;
    //     for(int j = 1; j <= n; j++){
    //         if(!st[j] && (t == -1 || dist[t] > dist[j])) t = j;
    //     }
    //     st[t] = true;//O(1)
        
    //     //每次遍历当前点连接的所有边,n-1次循环中总共遍历m条边,更新到当前节点的最短距离
    //     for(int j = h[t]; j != -1; j = ne[j]){
    //         int k = e[j];
    //         dist[k] = min(dist[k], dist[t] + w[j]);
    //     }
    // }
    

    if(dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main()
{
    cin >> n >> m;
    
    memset(h, -1, sizeof h);
    
    while(m --){
        int a, b, c;
        cin >> a >> b >> c;
        add(a,b,c);
    }
    
    cout << dijkstra() << endl;
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值