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. numpy 学习笔记

    numpy 学习笔记 导入 numpy 包 import numpy as np 声明 ndarray 的几种方法 方法一,从list中创建 l = [[1,2,3], [4,5,6], [7,8,9 ...

  2. 异步任务利器Celery(二)在django项目中使用Celery

    Celery 4.0支持django1.8及以上的版本,低于1.8的项目使用Celery 3.1. 一个django项目的组织如下: - proj/ - manage.py - proj/ - __i ...

  3. MongoDB的C#驱动报错Server instance 127.0.0.1:27017 is no longer connected的解决方案

    使用C#的MondoDB驱动,一直没问题.结果最近,MongoCursor的ToList方法,取列表,总是报错 Server instance 127.0.0.1:27017 is no longer ...

  4. 建立TCP连接过程

    1.服务器实例化一个ServerSocket 对象, 表示通过服务器上的端口通信. ServerSocket serverSocket = new ServerSocket(port); 2.服务器调 ...

  5. sqlitestudio

    SQLite数据库的特性 特点: 1.轻量级2.独立性,没有依赖,无需安装3.隔离性 全部在一个文件夹系统4.跨平台 支持众多操作系统5.多语言接口 支持众多编程语言6.安全性 事物,通过独占性和共享 ...

  6. linux如何管理物理内存?

    Linux kernel version: 5.0.1 arm64 1.将物理内存划分为若干页,每页的大小为4KiB(可以为8KiB或16KiB),那么如何知道每个页当前是什么情况呢? 那就需要一个结 ...

  7. cat查看文件以及sed查看指定行数

    https://unix.stackexchange.com/questions/288521/with-the-linux-cat-command-how-do-i-show-only-certai ...

  8. centos6.5下安装jdk并配置环境变量

    链接: https://blog.csdn.net/wawawawawawaa/article/details/81158943 以下链接供参考: https://blog.csdn.net/Bugg ...

  9. Linux/shell: remove adjacent similar patterns

    cat > temp004AA1abcAA2AA3abcAA4abcAA5AA6 awk 'BEGIN {pre=0; str="";} { if(NR==1){     i ...

  10. python 之 运算符

    Python 运算符   Python 运算符 什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4和5被称为操作数,"+"号为运算 ...