描述

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 fights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.

The number of nodes n will be in range [1, 100], with nodes labeled from 0 to n - 1.
The size of flights will be in range [0, n * (n - 1) / 2].
The format of each flight will be (src, dst, price).
The price of each flight will be in the range [1, 10000].
k is in the range of [0, n - 1].
There will not be any duplicated flights or self cycles.

样例

Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200

Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500

思路

见到这道题的第一反应是最短路算法。按照dfs,记录每个节点的深度,建立堆每次选择最短的边搜索,遇到dst就结束算法。(这里可能和Prim有点搞混)。考虑到建立了边的堆集,时间复杂度会有O( E log E)。

后来发现不能直接结束算法,因为可能存在负环,所以必须要搜索所有的点。

正确的思路是dp。 f( i , j )表示到达第i个点用了至多j步的时候的距离。

转移方程是: f( i , j ) = min{ f( pre(i), j-1 )+weight( pre(i), i ), f( i, j-1 ) }

也就是说到达第i个点用了至多 j 步的距离相当于到达 i 的前序节点用了至多 j-1 步,或者(没有多走),用了至多 j-1 步就到了 i 。

完成需要填满  max { O(NK), O(E) }

代码

    struct edge{int src;int weight;edge( int d, int w ){this->src = d;this->weight = w;}edge(){}};int findCheapestPrice(int n, vector<vector<int>> &flights, int src, int dst, int K) {// write your code herevector<edge> empty;vector<vector<edge>> pre(n, empty);int **f = new int*[n];int k = K+1;for(int i=0; i<n; i++){f[i] = new int[k];}for(auto i:flights) //记录前件{edge tmp1(i[0], i[2]);pre[i[1]].emplace_back(tmp1);}int sizeOfedge = flights.size();int infty = 1 << 30;f[src][0] = 0; for(int i=1; i<n; i++){if( i == src ){continue;}f[i][0] = infty;}for(int j=1; j<=k; j++){for(int i=0; i<n; i++){int min = infty;for(auto q:pre[i]){if( f[q.src][j-1]+ q.weight < min ){min = f[q.src][j-1] + q.weight;}}if(min > f[i][j-1]){min = f[i][j-1];}f[i][j] = min;}}int dstMin = infty;for(int j=0; j<=k; j++){if( f[dst][j] < dstMin ){dstMin = f[dst][j];}}if( dstMin == infty ){return -1;}else{return dstMin;}
}

[lintcode] 1029. Cheapest Flights Within K stops [Medium]相关推荐

  1. 【Lintcode】1029. Cheapest Flights Within K Stops

    题目地址: https://www.lintcode.com/problem/cheapest-flights-within-k-stops/description 给定一个非负权有向图,再给定一个源 ...

  2. Cheapest Flights Within K Stops

    [leetcode]Cheapest Flights Within K Stops 链接:https://leetcode.com/problems/cheapest-flights-within-k ...

  3. LeetCode——787. K 站中转内最便宜的航班(Cheapest Flights Within K Stops)[中等]——分析及代码(Java)

    LeetCode--787. K 站中转内最便宜的航班[Cheapest Flights Within K Stops][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 ( ...

  4. leetcode 787. Cheapest Flights Within K Stops | 787. K 站中转内最便宜的航班(BFS)

    题目 https://leetcode.com/problems/cheapest-flights-within-k-stops/ 题解 这题挺坑的实际上.DFS 超时了,因为涉及到步数限制 k,所以 ...

  5. LeetCode 787. Cheapest Flights Within K Stops

    题解 这题看似最短路径,但是暗藏变化. 首先想到dfs,搜索到所有由始至终的可行路径,记录最小cost即可. 但是这题编程实现有很多小花招,怎么剪枝,一个是k(步进的次数), 二是cost,都可以用来 ...

  6. LeetCode 从零单刷个人笔记整理(持续更新)

    更新至2020.2.23 github:https://github.com/ChopinXBP/LeetCode-Babel 本人博客用于个人对知识点的记录和巩固. 用几乎所有可行的方法进行了实现和 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  8. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

  9. 使用广度优先搜索找到最短路径

    by Sachin Malhotra 由Sachin Malhotra 使用广度优先搜索找到最短路径 (Finding Shortest Paths using Breadth First Searc ...

最新文章

  1. 某单位招聘考试需要考核数学英语计算机,2010年10月自学考试电子商务网站设计原理试题...
  2. BSTestRunner——一个丑在路上的python unnitest HTML报告生成Runner
  3. maven 编译mybatis项目时xml文件无法编译到target目录下的解决方法
  4. 深入研究Java类加载机制
  5. hash 数据类型的应用场景
  6. Maven+Spring+CXF+Tomcat7 简单例子实现webservice
  7. 探究Python源码,终于弄懂了字符串驻留技术
  8. 和显卡驱动要配套吗_显卡有必要更新驱动程序吗?老玩家的建议请收好
  9. redis基础_NOSQL介绍
  10. Atitit. c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0 attilax总结 1. 版本历史 1 1.1. C# 1.0-纯粹的面向对象 2 1.2. C# 2.0
  11. java认证考试例题_2016年Java认证考试题(3)
  12. 基于扩频信号的水声信道数据传输系统仿真,研究满足了WSSUS假设的瑞利信道模型,采用相干BPSK调制,联合多普勒Rake接收机
  13. CLO3D.Modelist.V2.2.134 X64 三维服装设计软件
  14. R_leaflet包_最易上手地图教程(二)
  15. 剑指Offer-46:把数字翻译成字符串
  16. 数据分析八大模型:详解RFM模型
  17. 2017多校第4场 HDU 6078 Wavel Sequence DP
  18. 自研数据分析工具——yandas系列一:分析泰坦尼克号沉船事件中的乘客信息表
  19. Java学习笔记(九)抽象类
  20. 递归、迭代和分治(1):递归

热门文章

  1. 汇编语言复习题及详细答案1(老师给的题 自己写的答案)
  2. 软件测试————测试级别
  3. 设计太单调?快用肌理降服甲方,给你的设计提提分
  4. 【科普】WebSocket
  5. 天勤数据结构笔记——第六章 树与二叉树(大题)
  6. 【线性代数】矩阵初等变换与线性方程组公式定理总结
  7. Java写银联支付C扫B经验分享--框架SSM
  8. Python---列表
  9. Apollo:参考线ReferenceLine是如何定义的
  10. Java读写excel方法大全,对特定的数据给上特定的背景颜色,加生备注信息等等。