前言

我感觉这题比较有代表性,所以记录一下,这题是加权有向图中求最短路径的问题。

题目

787. K 站中转内最便宜的航班

动态规划

假设有一条路径是[src, i, ..., j, dst],解法一子问题的定义是[src, i, ..., j],解法二子问题的定义是[i, ..., j, dst]

解法一需要知道哪些节点指向dst,需要求入度。
解法二需要知道src指向哪些节点,需要求出度。

解法一

如下图所示,想要求srcdst的最短路径,如果知道了srcs1srcs2的最短路径,那么问题就好解决了。

加上s1s2到dst的花费取最小值即可,伪代码如下

minPrice(dst, k) =
min(minPrice(s1, k - 1) + w1,
minPrice(s2, k - 1) + w2)

最终代码

class Solution {
int n, src, dst;
int[][] flights;
int[][] memo;
HashMap<Integer, List<int[]>> indegree = new HashMap<>(); public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
this.n = n;
this.flights = flights;
this.src = src;
this.dst = dst;
// 求入度
for(int[] flight : flights){
int from = flight[0], to = flight[1], price = flight[2];
indegree.putIfAbsent(to, new ArrayList<>());
indegree.get(to).add(new int[]{from, price});
}
memo = new int[n][k + 1];
for(int[] arr : memo){
Arrays.fill(arr, -2);
} return dp(dst, k);
} int dp(int dst, int k){
if(src == dst){
return 0;
}
if(k < 0){
return -1;
}
if(memo[dst][k] != -2){
return memo[dst][k];
} int res = Integer.MAX_VALUE;
if(indegree.containsKey(dst)){
for(int[] v : indegree.get(dst)){
int subProblem = dp(v[0], k - 1);
if(subProblem == -1) continue;
res = Math.min(res, subProblem + v[1]);
}
} memo[dst][k] = res == Integer.MAX_VALUE ? -1 : res;
return memo[dst][k];
}
}

解法二

如下图所示,想要求srcdst的最短路径,如果知道了s1dsts2dst的最短路径,那么问题就好解决了。


加上srcs1s2的花费取最小值即可,伪代码如下

minPrice(src, k) =
min(minPrice(s1, k - 1) + w1,
minPrice(s2, k - 1) + w2)

最终代码

class Solution {
int n, src, dst;
int[][] flights;
int[][] memo;
HashMap<Integer, List<int[]>> outdegree = new HashMap<>(); public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
this.n = n;
this.flights = flights;
this.src = src;
this.dst = dst;
// 求出度
for(int[] flight : flights){
int from = flight[0], to = flight[1], price = flight[2];
outdegree.putIfAbsent(from, new ArrayList<>());
outdegree.get(from).add(new int[]{to, price});
}
memo = new int[n][k + 1];
for(int[] arr : memo){
Arrays.fill(arr, -2);
} return dp(src, k);
} int dp(int src, int k){
if(src == dst){
return 0;
}
if(k < 0){
return -1;
}
if(memo[src][k] != -2){
return memo[src][k];
} int res = Integer.MAX_VALUE;
if(outdegree.containsKey(src)){
for(int[] v : outdegree.get(src)){
int subProblem = dp(v[0], k - 1);
if(subProblem == -1) continue;
res = Math.min(res, subProblem + v[1]);
}
} memo[src][k] = res == Integer.MAX_VALUE ? -1 : res;
return memo[src][k];
}
}

小结

两种解法代码非常相似,具有对称性。对于有向图最短路径问题,常规思路都是 Dijkstra 等图论经典算法,没想到动态规划也可以,很奇妙。这也是我想记录这道题的原因吧。

BFS 算法思路

Dijkstra 算法

public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
List<int[]>[] graph = new LinkedList[n];
for (int i = 0; i < n; i++) {
graph[i] = new LinkedList<>();
}
for (int[] edge : flights) {
int from = edge[0];
int to = edge[1];
int price = edge[2];
graph[from].add(new int[]{to, price});
} // 启动 dijkstra 算法
// 计算以 src 为起点在 k 次中转到达 dst 的最短路径
K++;
return dijkstra(graph, src, K, dst);
} class State {
// 图节点的 id
int id;
// 从 src 节点到当前节点的花费
int costFromSrc;
// 从 src 节点到当前节点经过的节点个数
int nodeNumFromSrc; State(int id, int costFromSrc, int nodeNumFromSrc) {
this.id = id;
this.costFromSrc = costFromSrc;
this.nodeNumFromSrc = nodeNumFromSrc;
}
} // 输入一个起点 src,计算从 src 到其他节点的最短距离
int dijkstra(List<int[]>[] graph, int src, int k, int dst) {
// 定义:从起点 src 到达节点 i 的最短路径权重为 distTo[i]
int[] distTo = new int[graph.length];
// 定义:从起点 src 到达节点 i 的最小权重路径至少要经过 nodeNumTo[i] 个节点
int[] nodeNumTo = new int[graph.length];
Arrays.fill(distTo, Integer.MAX_VALUE);
Arrays.fill(nodeNumTo, Integer.MAX_VALUE);
// base case
distTo[src] = 0;
nodeNumTo[src] = 0; // 优先级队列,costFromSrc 较小的排在前面
Queue<State> pq = new PriorityQueue<>((a, b) -> {
return a.costFromSrc - b.costFromSrc;
});
// 从起点 src 开始进行 BFS
pq.offer(new State(src, 0, 0)); while (!pq.isEmpty()) {
State curState = pq.poll();
int curNodeID = curState.id;
int costFromSrc = curState.costFromSrc;
int curNodeNumFromSrc = curState.nodeNumFromSrc; if (curNodeID == dst) {
// 找到最短路径
return costFromSrc;
}
if (curNodeNumFromSrc == k) {
// 中转次数耗尽
continue;
} // 将 curNode 的相邻节点装入队列
for (int[] neighbor : graph[curNodeID]) {
int nextNodeID = neighbor[0];
int costToNextNode = costFromSrc + neighbor[1];
// 中转次数消耗 1
int nextNodeNumFromSrc = curNodeNumFromSrc + 1; // 更新 dp table
if (distTo[nextNodeID] > costToNextNode) {
distTo[nextNodeID] = costToNextNode;
nodeNumTo[nextNodeID] = nextNodeNumFromSrc;
}
// 剪枝,如果中转次数更多,花费还更大,那必然不会是最短路径
if (costToNextNode > distTo[nextNodeID]
&& nextNodeNumFromSrc > nodeNumTo[nextNodeID]) {
continue;
} pq.offer(new State(nextNodeID, costToNextNode, nextNodeNumFromSrc));
}
}
return -1;
}

参考资料

旅游省钱大法:加权最短路径

【力扣】787. K 站中转内最便宜的航班加权——有向图最短路径的更多相关文章

  1. Java实现 LeetCode 787 K 站中转内最便宜的航班(两种DP)

    787. K 站中转内最便宜的航班 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是 ...

  2. 【力扣leetcode】-787. K站中转内最便宜的航班

    题目描述: 有 n 个城市通过一些航班连接.给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 p ...

  3. LeetCode——787. K 站中转内最便宜的航班

    有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst 最多经过 ...

  4. leetcode 787. K 站中转内最便宜的航班

    问题描述 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst ...

  5. [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 pri ...

  6. leetcode_787【K 站中转内最便宜的航班】

    有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst 最多经过 ...

  7. 刷题-力扣-1011. 在 D 天内送达包裹的能力

    1011. 在 D 天内送达包裹的能力 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/capacity-to-ship-packag ...

  8. 力扣992.K个不同整数的子数组-C语言实现

    题目 原题链接 给定一个正整数数组 A,如果 A 的某个子数组中不同整数的个数恰好为 K,则称 A 的这个连续.不一定独立的子数组为好子数组. (例如,[1,2,3,1,2] 中有 3 个不同的整数: ...

  9. 力扣——Reverse Nodes in k-Group(K 个一组翻转链表) python实现

    题目描述: 中文: 给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度. 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序 ...

随机推荐

  1. MVVM视图模型

  2. Java实现7种常见密码算法

    原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介 前面在密码学入门一文中讲解了各种常见的密码学概念.算法与运用场景,但没有介绍过代码,因此,为作补充,这一篇将会介绍 ...

  3. VS使用正则表达式删除程序中的空行

    Ctrl+H; 需要替换的正则表达式 ^(?([^\r\n])\s)*\r?$\r?\n

  4. Sql Server性能排查和优化懒人攻略

    转载自作者zhang502219048的微信公众号[SQL数据库编程]:Sql Server性能排查和优化懒人攻略 很多年前,笔者那时刚从广东技术师范学院(现为广东技术师范大学,以前为广东民族学院)的 ...

  5. redis的几个优化点

    1. redis读写速度慢 可以将redis单实例改为redis集群 2. redis报OOM redis内存溢出,调大redis内存:增加redis.conf中的maxmemory 的值.如果red ...

  6. python——os模块学习

    import os #1.获取当前使用的操作系统 #返回操作系统类型,nt是windows,posix是linux print(os.name) #print是一个函数,函数里面进行条件判断'posi ...

  7. 安卓APP和小程序渗透测试技巧总结

    安卓APP和小程序渗透测试技巧总结 免责声明: 安卓7以上抓取https流量包 证书信任 首先安装OpenSSL,此步骤不再赘述,可以参考百度. 然后安装模拟器(我使用的是夜神模拟器). 导出需要的证 ...

  8. mindxdl--common--web_cert_utils.go

    // Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.// Package common this file ...

  9. [Android开发学iOS系列] TableView展现一个list

    TableView 基础 本文讲讲TableView的基本使用. 顺便介绍一下delegation. TableView用来做什么 TableView用来展示一个很长的list. 和Android中的 ...

  10. i春秋时间

    打开题目就是一段php代码 大致的意思是 ------------------------------------------------------------------------------- ...