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. 如何搭建一个 MySQL 分布式集群

    1.准备集群搭建环境 使用6台虚拟机来搭建 MySQL分布式集群 ,相应的实验环境与对应的MySQL节点之间的对应关系如下图所示: 管理节点(MGM):这类节点的作用是管理MySQLCluster内的 ...

  2. Hibernate properties文件

    ###################### ### Query Language ### ###################### ## define query language consta ...

  3. A writer of dictionaries,a harmless druge.

    Nine Years for A and B By Christopher Ricks Dr. Johnson was the greatest man who made a dictionary.  ...

  4. opencv学习之路(12)、图像滤波

    一.图像滤波简介 二.方框滤波——boxFilter() #include<opencv2/opencv.hpp> using namespace cv; void main(){ Mat ...

  5. bzoj 1614 Telephone Lines架设电话线 - 二分答案 - 最短路

    Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...

  6. 学习Struts2的个人疑惑及问题解决

    刚开始学习SSH框架中Struts2时,个人疑惑以及一些问题总结一下. 1.package节点namespace属性值决定访问路径问题       namespace不写或写namespace=&qu ...

  7. Master of GCD 【线段树区间更新 || 差分】

    Master of GCD 时间限制: 1 Sec  内存限制: 128 MB 提交: 670  解决: 112 [提交] [状态] [命题人:admin] 题目描述 Hakase has n num ...

  8. IntelliJ IDEA 中SpringBoot对Run/Debug Configurations配置 SpringBoot热部署

    运行一个SpringBoot多模块应用 使用SpringBoot配置启动: Use classpath of module选中要运行的模块 VM options:内部配置参数 -Dserver.por ...

  9. Dubbo集群配置和官方文档

    集群配置: https://blog.csdn.net/zh520qx/article/details/63679908 https://www.cnblogs.com/hd3013779515/p/ ...

  10. MySQL的启动和关闭

    1.Windows下 启动服务 mysqld --console 或 net start mysql 关闭服务 mysqladmin -uroot shudown 或 net stop mysql 2 ...