K 站中转内最便宜的航班-dfs与动态算法

news/2025/3/16 6:11:21/

K 站中转内最便宜的航班

有 n 个城市通过一些航班连接。给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 pricei 抵达 toi。

现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到出一条最多经过 k 站中转的路线,使得从 src 到 dst 的 价格最便宜 ,并返回该价格。 如果不存在这样的路线,则输出 -1。

示例 1:

输入:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
输出: 200
解释:
城市航班图如下
在这里插入图片描述

从城市 0 到城市 2 在 1 站中转以内的最便宜价格是 200,如图中红色所示。

示例 2:

输入:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
输出: 500
解释:
城市航班图如下

从城市 0 到城市 2 在 0 站中转以内的最便宜价格是 500,如图中蓝色所示。

在这里插入图片描述

dfs算法如下:

int findCheapestPrice(int n, int** flights, int flightsSize, int* flightsColSize, int src, int dst, int k) {int f[k + 2][n];memset(f, 0x3f, sizeof(f));f[0][src] = 0;for (int t = 1; t <= k + 1; ++t) {for (int k = 0; k < flightsSize; k++) {int j = flights[k][0], i = flights[k][1], cost = flights[k][2];f[t][i] = fmin(f[t][i], f[t - 1][j] + cost);}}int ans = 0x3f3f3f3f;for (int t = 1; t <= k + 1; ++t) {ans = fmin(ans, f[t][dst]);}return (ans == 0x3f3f3f3f ? -1 : ans);
}作者:LeetCode-Solution
链接:https://leetcode.cn/problems/cheapest-flights-within-k-stops/solution/k-zhan-zhong-zhuan-nei-zui-bian-yi-de-ha-abzi/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

动态规划算法如下:

struct node {int dst;int cost;struct node *next; };void dfs(int n,int k,struct node *N,int cp,int dst,int *s,int *num,int cost){if(n<=k){if(cp==dst){if(cost<*num){*num=cost;}}else{struct node *p=N+cp;p=p->next;while(p){if(s[p->dst]==0){printf("%d->",p->dst);s[p->dst]=1;dfs(n+1,k,N,p->dst,dst,s,num,cost+p->cost);s[p->dst]=0;}else{printf("cc");}p=p->next;}}}}
int findCheapestPrice(int n, int** flights, int flightsSize, int* flightsColSize, int src, int dst, int k){struct node *N=(struct node *)malloc(sizeof(struct node)*n);int *s=(int *)malloc(sizeof(int)*n);int *num=(int *)malloc(sizeof(int));int i;struct node *pl=N+src;*num=1000000;for(i=0;i<n;i++){(N+i)->next=NULL;s[i]=0;}s[src]=1;for(i=0;i<flightsSize;i++){struct node *p=(struct node *)malloc(sizeof(struct node)*flightsSize);p->dst=flights[i][1];p->cost=flights[i][2];p->next=(N+flights[i][0])->next;(N+flights[i][0])->next=p;}pl=pl->next;while(pl){printf("ds %d ",pl->dst);dfs(0,k,N,pl->dst,dst,s,num,pl->cost);//   printf("df3");pl=pl->next;}printf("df4");if((*num)==1000000)return -1;return *num;
}

http://www.ppmy.cn/news/568481.html

相关文章

双流机场国际航线将整体转场天府机场,双流机场广告和天府机场广告各有何优势

最新消息&#xff0c;成都双流机场T1航站楼将于2023年3月26号冬春航季结束后关闭改造&#xff0c;届时&#xff0c;双流机场全部国际及地区客运航线将整体转场天府国际机场运营。四川省内两座机场&#xff1b;双流机场和2021年通航的天府机场各自有什么优势呢&#xff1f;知行爱…

短途自驾游山西全1057

74短途自驾游山西全<br>最近刚刚有驴友自驾车前往山西&#xff0c;短短四天的山西之行也有不少收获&#xff0c;本期我们特别为大家推荐短期自驾游山西。 <br>  <br>   路线&#xff1a;北京→娘子关→太原榆次→祁县乔家大院→平遥古城 <br>  &…

DiscuzX 3.4 R20191201

安装说明&#xff1a;https://discuz.chat/pages/topic/index?id5797 本次release v3.4-20191201 距离上次0917版本75天&#xff0c;共合并了36个PR&#xff0c;感谢社区的努力与贡献。 打包文件下载地址&#xff1a; https://gitee.com/3dming/DiscuzL/attach_files 官方 …

discuzx3.4的php用什么版本号,Discuz! X3.4

全新安装教程 将 upload 目录下的所有文件使用 FTP 软件以二进制方式上传到空间 打开浏览器安装 Discuz! X3.4&#xff0c;在浏览器中运行 http://你的域名/install/ 开始全新安装&#xff0c;按提示一步一步操作 升级说明 从 X3.2、X3.3 升级 备份数据库 建立文件夹 old&#…

东北地区探空站

嫩江,50557,125.23,49.16,243.0 伊春,50774,128.90,47.71,232.0 哈尔滨,50953,126.76,45.73,143.0 通辽,54135,122.26,43.60,180.0 长春,54161,125.21,43.90,238.0 延吉,54292,129.46,42.88,178.0 临江,54374,126.91,41.71,333.0

【解决方案】智慧机场:基于视频智能融合平台EasyCVR让机场数字化转型高飞

一、行业背景 随着5G、AI、物联网、大数据等新技术的不断成熟&#xff0c;交通行业迎来数字化转型的快速发展期&#xff0c;航空业的数字化水平正由“高速”增长向“高质”增长转变&#xff0c;但机场在转型的过程中依然面临诸多困难和挑战&#xff1a; 机场运营效率无法满足…

BZOJ2843极地旅行社

2843: 极地旅行社 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 282 Solved: 193 Description 不久之前&#xff0c;Mirko建立了一个旅行社&#xff0c;名叫“极地之梦”。这家旅行社在北极附近购买了N座冰岛&#xff0c;并且提供观光服务。当地最受欢迎的当然是帝企鹅…

Discuz X2 文件目录功能详解

转自&#xff1a;http://www.discuz.net/thread-2188629-1-1.html 本人将对内容进行再次更新. 先从根目录开始&#xff0c;根目录文件一般都是入口&#xff0c;即执行具体功能的代码一般不在这些文件中&#xff0c;而是在其调用的文件中 admin.php 系统站点管理入口文件 api.…