• Dijkstra求最短路


    [Acwing 849]Dijkstra求最短路 I

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

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

    输入格式

    第一行包含整数 n n n m m m

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

    输出格式

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

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

    数据范围

    $ 1≤n≤500$,
    1 ≤ m ≤ 105 1≤m≤105 1m105,
    图中涉及边长均不超过10000。

    输入样例:

    3 3
    1 2 2
    2 3 1
    1 3 4
    
    • 1
    • 2
    • 3
    • 4

    输出样例:

    3
    
    • 1

    最短路

    使用Dijkstra算法,邻接矩阵存储

    朴素代码

    #include 
    #include 
    #include 
    using namespace std;
    
    const int N = 505;
    const int INF = INT_MAX;
    struct Edge {
        int from, to, weight;
    };
    
    vector<Edge> edges;
    int dist[N];
    
    void bellmanFord(int start, int n, int m) {
        fill(dist, dist + n + 1, INF);
        dist[start] = 0;
    
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int u = edges[j].from;
                int v = edges[j].to;
                int w = edges[j].weight;
                if (dist[u] != INF && dist[v] > dist[u] + w) {
                    dist[v] = dist[u] + w;
                }
            }
        }
    
        // 检查是否存在负权回路
        for (int i = 0; i < m; i++) {
            int u = edges[i].from;
            int v = edges[i].to;
            int w = edges[i].weight;
            if (dist[u] != INF && dist[v] > dist[u] + w) {
                cout << -1 << endl;
                return;
            }
        }
    
        if (dist[n] == INF) {
            cout << -1 << endl;
        } else {
            cout << dist[n] << endl;
        }
    }
    
    int main() {
        int n, m;
        cin >> n >> m;
    
        for (int i = 0; i < m; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            edges.push_back({x, y, z});
        }
    
        bellmanFord(1, n, m);
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    优先队列优化代码

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    const int N = 505;
    const int INF = INT_MAX;
    struct Edge {
        int to, weight;
    };
    
    vector<Edge> graph[N];
    int dist[N];
    
    void dijkstra(int start, int n) {
        fill(dist, dist + n + 1, INF);
        dist[start] = 0;
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        pq.push({0, start});
    
        while (!pq.empty()) {
            int d = pq.top().first;
            int u = pq.top().second;
            pq.pop();
    
            if (d > dist[u]) {
                continue;
            }
    
            for (Edge& e : graph[u]) {
                int v = e.to;
                int w = e.weight;
                if (dist[v] > dist[u] + w) {
                    dist[v] = dist[u] + w;
                    pq.push({dist[v], v});
                }
            }
        }
    }
    
    int main() {
        int n, m;
        cin >> n >> m;
    
        for (int i = 0; i < m; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            graph[x].push_back({y, z});
        }
    
        dijkstra(1, n);
    
        if (dist[n] == INF) {
            cout << -1 << endl;
        } else {
            cout << dist[n] << endl;
        }
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
  • 相关阅读:
    【LeetCode】9. 回文数
    嵌入式Qt-实现两个窗口的切换
    武汉洪山区申请ITSS认证和能力评估CS认证的好处
    TCMonodepth:Enforcing Temporal Consistency in Video Depth Estimation-论文阅读
    FullGC 过多 为什么会让CPU飙升100%
    MyBatis篇---第四篇
    【小笔记】fasttext文本分类问题分析
    Unity进阶提升-2D游戏跳跃手感优化(跳起下落)
    【深度学习】PyTorch深度学习笔记02-线性模型
    Git常用命令diff和mv
  • 原文地址:https://blog.csdn.net/2201_75859030/article/details/137440438