Dijkstra’s Shortest Path Algorithm / LeetCode 787. Cheapest Flights Within K Stops

Dijkstra’s Shortest Path Algorithm

实现详见:https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-using-priority_queue-stl/

需要注意的是,priority_queue并无法更新内部的元素,因此我们更新dist的同时,直接把新的距离加入pq即可。pq里虽然有outdated的dist,但是由于距离过长,他们并不会更新dist。

//  If there is shorted path to v through u.
if (dist[v] > dist[u] + weight) {
    // Updating distance of v
    dist[v] = dist[u] + weight;
    pq.push(make_pair(dist[v], v));
} 

时间复杂度 O(ElogV)

787. Cheapest Flights Within K Stops

本题的实质是 Dijkstra’s Shortest Path Algorithm,只不过追加了一个约束条件step。

class Solution {
public:
    typedef tuple<int,int,int> ti; // (dist,u,step)

    struct edge{
        int end;
        int weight;
    };

    int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K) {
        vector<vector<edge>> graph(n,vector<edge>());
        for (auto flight:flights){
            graph[flight[0]].push_back({flight[1],flight[2]});
        }
        priority_queue<ti,vector<ti>,greater<ti>> q;
        q.emplace(0,src,K+1);

        while (!q.empty()){
            auto [dist,u,step]=q.top(); q.pop();
            if (u==dst) return dist;
            if (step==0) continue;
            for (auto [v,w]:graph[u]){
                q.emplace(dist+w,v,step-1);
            }
        }
        return -1;
    }
};

原文地址:https://www.cnblogs.com/hankunyan/p/11518287.html

时间: 2024-11-07 00:33:38

Dijkstra’s Shortest Path Algorithm / LeetCode 787. Cheapest Flights Within K Stops的相关文章

787. Cheapest Flights Within K Stops

https://leetcode.com/problems/cheapest-flights-within-k-stops/description/ DFS (slow) class Solution { public: vector<vector<pair<int,int>>> v; // city: <connected city, price> vector<bool> visited; int res = INT_MAX; int fin

[Swift]LeetCode787. K 站中转内最便宜的航班 | Cheapest Flights Within K Stops

There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w. Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src 

Within K stops 最短路径 Cheapest Flights Within K Stops

2018-09-19 22:34:28 问题描述: 问题求解: 本题是典型的最短路径的扩展题,可以使用Bellman Ford算法进行求解,需要注意的是在Bellman Ford算法的时候需要额外申请一个数组来保存变量. public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) { int[] dist = new int[n]; for (int i = 0; i < n; i++) dist[i

Method for finding shortest path to destination in traffic network using Dijkstra algorithm or Floyd-warshall algorithm

A method is presented for finding a shortest path from a starting place to a destination place in a traffic network including one or more turn restrictions, one or more U-turns and one or more P-turns using a Dijkstra algorithm. The method as sets a

[LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径

An undirected, connected graph of N nodes (labeled?0, 1, 2, ..., N-1) is given as?graph. graph.length = N, and?j != i?is in the list?graph[i]?exactly once, if and only if nodes?i?and?j?are connected. Return the length of the shortest path that visits

BFS 基础写法 —— 以 LeetCode #1091 Shortest Path in Binary Matrix 为例

Question In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that: Adjacent cells C_i and C_{i+1} are connected

LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination

原题链接在这里:https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/ 题目: Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can move up, down, left or right from and to an empty cell. Return

The Shortest Path in Nya Graph HDU - 4725

Problem Description This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.The Nya graph is an un

HDU 4725 The Shortest Path in Nya Graph(构图)

The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13445    Accepted Submission(s): 2856 Problem Description This is a very easy problem, your task is just calculate