2018-09-19 22:34:28

问题描述:

问题求解:

本题是典型的最短路径的扩展题,可以使用Bellman Ford算法进行求解,需要注意的是在Bellman Ford算法的时候需要额外申请一个数组来保存变量。

    int inf = (int)1e9;
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
// write your code here
int[] dist = new int[n];
Arrays.fill(dist, inf);
dist[src] = 0;
for (int i = 0; i <= K; i++) {
int[] prev = Arrays.copyOf(dist, n);
for (int[] e : flights) {
int from = e[0];
int to = e[1];
int w = e[2];
if (prev[to] > prev[from] + w) {
dist[to] = prev[from] + w;
}
}
}
return dist[dst] == inf ? -1 : dist[dst];
}

  

Within K stops 最短路径 Cheapest Flights Within K Stops的更多相关文章

  1. [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 ...

  2. [LeetCode] Cheapest Flights Within K Stops K次转机内的最便宜的航班

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

  3. [LeetCode] 787. Cheapest Flights Within K Stops K次转机内的最便宜航班

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

  4. 787. 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 ...

  5. LeetCode 787. Cheapest Flights Within K Stops

    原题链接在这里:https://leetcode.com/problems/cheapest-flights-within-k-stops/ 题目: There are n cities connec ...

  6. 【LeetCode】787. Cheapest Flights Within K Stops 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 参考资料 日期 题目 ...

  7. [LeetCode] 787. Cheapest Flights Within K Stops_Medium tag: Dynamic Programming, BFS, Heap

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

  8. K条最短路径算法(KSP, k-shortest pathes):Yen's Algorithm

    参考: K最短路径算法之Yen's Algorithm Yen's algorithm 基于网络流量的SDN最短路径转发应用 K条最短路径算法:Yen's Algorithm 算法背景 K 最短路径问 ...

  9. 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除。

    题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出: 一个整数. ...

随机推荐

  1. Angular4.x 安装|创建项目|目录结构|创建组件

    Angular4.x 安装|创建项目|目录结构|创建组件 安装最新版本的 nodejs node.js 官网:https://nodejs.org/zh-cn/ 去官网下载 node.js,下一步下一 ...

  2. 理解Linux文件系统之 inode

    一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做”扇区”(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统读取硬盘的时候,不会 ...

  3. Java基础——javaMail:使用心得

    想要做一个java发送邮件小功能. 使用的maven搭建的项目. <!-- https://mvnrepository.com/artifact/javax.mail/mail -->&l ...

  4. Python3 tkinter基础 Listbox delete 删除单个、所有元素

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  5. A>B等CSS选择器

    这些是CSS3特有的选择器,A>B 表示选择A元素的所有子B元素.与A B的区别在于,A B选择所有后代元素,而A>B只选择一代.另外:没有<的用法. A+B表示HTML中紧随A的B ...

  6. 解决跨域(CORS)问题

    为什么会有跨域问题 是因为浏览器的同源策略是对ajax请求进行阻拦了,但是不是所有的请求都给做跨域,像是一般的href属性,a标签什么的都不拦截 解决跨域问题的两种方式 JSONP   推荐参考 CO ...

  7. HDU1285 确定比赛问题【拓扑排序+优先队列】

    题目 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩, ...

  8. sql server查看用户权限

    System.ServiceModel.FaultException: Server error. Detail: The EXECUTE permission was denied on the o ...

  9. Android 的 ListView 的CheckBox标题栏显示文本之后显示单选框

    https://blog.csdn.net/u013790519/article/details/50036223 2.CheckBox的android:button=”@null”属性代码设置如下: ...

  10. Unity3D学习笔记(二十七):MVC框架下的背包系统(2)

    Tools FileTools using System.Collections; using System.Collections.Generic; using UnityEngine; using ...