[Leetcode 787]中转K站内最便宜机票
题目
n个城市,想求从src到dist的最廉价机票
有中转站数K的限制,即如果k=5,中转10次机票1000,中转5次机票2000,最后返回2000
There are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is a flight from city fromi to city toi with cost pricei.
You are also given three integers src, dst, and k, return the cheapest price from src to dst with at most k stops. If there is no such route, return -1.
Example 1:

Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
Output: 200
Explanation: The graph is shown.
The cheapest price from city0to city2with at most 1 stop costs 200, as marked red in the picture.
Example 2:

Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
Output: 500
Explanation: The graph is shown.
The cheapest price from city0to city2with at most 0 stop costs 500, as marked blue in the picture.
Constraints:
1 <= n <= 1000 <= flights.length <= (n * (n - 1) / 2)flights[i].length == 30 <= fromi, toi < nfromi != toi1 <= pricei <= 104- There will not be any multiple flights between two cities.
0 <= src, dst, k < nsrc != dst
思路
和https://www.cnblogs.com/inku/p/15622556.html类似
这题多了一个stepCheck,check中转的次数
dijsktra+优先队列
代码
class Solution {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
int[][] adj=new int[n][n];
for(int[] flight_line:flights){
//flighr_line: {from,to,money}
adj[flight_line[0]][flight_line[1]]=flight_line[2];
}
int[] cost=new int[n];
int[] stepCheck=new int[n];
Arrays.fill(cost,Integer.MAX_VALUE);
Arrays.fill(stepCheck,Integer.MAX_VALUE);
cost[src] = 0;
stepCheck[src] = 0;
//int[] cur position,cost,stops
PriorityQueue<int[]> pq=new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1]-o2[1];
}
});
//pq.add(new int[]{0,0,1}); 错误!
pq.offer(new int[]{src, 0, 0});//起点不一定是0,本身不算中转
while (!pq.isEmpty()){
int[] curPos=pq.poll();
int curNode=curPos[0];
int curCost=curPos[1];
int curStop=curPos[2];
if(curNode==dst)
return curCost;
if(curStop==k+1)
continue;
for(int i=0;i<n;i++){
//两地有机票
if(adj[curNode][i]>0){
int cost_try=adj[curNode][i]+curCost;
if(cost_try<cost[i]){
cost[i]=cost_try;
pq.offer(new int[]{i,cost_try,curStop+1});//便宜的情况,加入
}
else if(curStop<stepCheck[i]){
pq.offer(new int[]{i,cost_try,curStop+1});//没便宜,但站数更少的情况,也加入
}
stepCheck[i]=curStop;//本身不算stop
}
}
}
return cost[dst]==Integer.MAX_VALUE?-1:cost[dst];
}
}
疑问
else if(curStop<stepCheck[i]){
pq.offer(new int[]{i,cost_try,curStop+1});//没便宜,但站数更少的情况,也加入
}
没便宜(cost更多),但站数更少的情况下虽然加入了,但因优先队列是按cost排序,那岂不是永远取不到?它的意义?
A:取得到,先加进去再说。虽然当前cost多,但后面的cost可能极小,成为最终解的一部分

[Leetcode 787]中转K站内最便宜机票的更多相关文章
- LeetCode——787. K 站中转内最便宜的航班
有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst 最多经过 ...
- Java实现 LeetCode 787 K 站中转内最便宜的航班(两种DP)
787. K 站中转内最便宜的航班 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是 ...
- 【力扣leetcode】-787. K站中转内最便宜的航班
题目描述: 有 n 个城市通过一些航班连接.给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 p ...
- leetcode 787. K 站中转内最便宜的航班
问题描述 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst ...
- 【力扣】787. K 站中转内最便宜的航班加权——有向图最短路径
前言 我感觉这题比较有代表性,所以记录一下,这题是加权有向图中求最短路径的问题. 题目 787. K 站中转内最便宜的航班 动态规划 假设有一条路径是[src, i, ..., j, dst],解法一 ...
- [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 ...
- leetcode_787【K 站中转内最便宜的航班】
有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst 最多经过 ...
- Lucene.net站内搜索—4、搜索引擎第一版技术储备(简单介绍Log4Net、生产者消费者模式)
目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...
- Lucene.net站内搜索—1、SEO优化
目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...
- 1.PHP站内搜索 分类: PHP开发实例 2015-07-31 22:48 4人阅读 评论(0) 收藏
PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...
随机推荐
- redis-RedisTemplate.opsForValue 常用方法
16.multiSetIfAbsent(Map<? extends K,? extends V> map) 如果对应的map集合名称不存在,则添加,如果存在则不做修改. Map value ...
- vue element 可编辑表格行内验证
<template> <div class="page-layout rataMdel"> <el-button type="primary ...
- nginx ip段限制
deny 123.0.0.0/8; // 封 123.0.0.1~123.255.255.254 这个段的ip deny 123.1.0.0/16; // 封 123.1.0.1~123.1.255. ...
- python func_timeout 设置函数超时退出
使用func_timeout设置函数超时退出,使用func_set_timeout装饰器和func_timeout方法 from func_timeout import func_set_timeou ...
- Flutter中的路由 路由替换 返回到根路由
一.Flutter 中返回到上一级页面 Navigator.of(context).pop(); 二.Flutter 中替换路由 比如我们从用户中心页面跳转到了 registerFirst 页面,然后 ...
- Netty Reactor模型
1.netty抽象出两个线程池:BossGroup负责监听和建立连接 :WorkerGroup 负责网络IO的读写 2.BossGroup 和 WorkerGroup 类型都是NioEventLoop ...
- 清理缓存tc
/$SYNC 今天修改自建表的字段,换了参考字段但是SE16N显示一直没有改变,删字段,删表都尝试依旧无果,实际上只是没有清理缓存,扑街,留存
- HTML5代码大全
一.HTML各种命令的代码: 1.文本标签(命令) <pre></pre> 创建预格式化文本 <h1></h1> 创建 ...
- HTTP对应状态码
服务器–响应–客户端 1. 响应体 Accept:告诉浏览器所支持的数据类型Accept-Encoding:表示浏览器支持的编码格式 GBK UTF-8 GB2312 ISO8859-1Accept- ...
- Queries Gym - 100741A - 树状数组
给定 \(n\) 和 \(m\),对于 \(n\) 个数字 \(a_i\),进行下列三种操作: (1) + p r: 将 p 位置的元素加上 r, 输出此时 p 位置的值: (2) - p r : 将 ...