A strange lift hdu1548 Dijkstra算法

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

http://acm.hdu.edu.cn/showproblem.php?pid=1548

Dijkstra算法:单源最短路径问题

——包括确定起点的最短路径问题,确定终点的最短路径问题(与确定起点的问题相反,该问题是已知终结结点,求最短路径的问题。在无向图中该问题与确定起点的问题完全等同,在有向图中该问题等同于把所有路径方向反转的确定起点的问题。)

确定起点终点的最短路径问题
——即已知起点和终点,求两结点之间的最短路径。

Floyd算法:全局最短路径问题

——求图中所有的最短路径。

#include <iostream>
using namespace std;
#define N 205
#define INF 0x7ffffff
int lift[N][N],dir[N];
bool visited[N];

int main(){
#ifndef ONLINE_JUDGE
	freopen("1548in.txt","r",stdin);
#endif
	int n,a,b,i,j,ki,low,t;
	while (scanf("%d",&n)!=EOF&&n){
		scanf("%d%d",&a,&b);
		for (i=1;i<=n;i++)
			for (j=1;j<=i;j++)
				lift[i][j]=lift[j][i]=INF;
		for (i=1;i<=n;i++){
			scanf("%d",&ki);
			if (i-ki>=1)
				lift[i][i-ki]=1;
			if (i+ki<=n)
				lift[i][i+ki]=1;
		}
		for (i=1;i<=n;i++){
			lift[i][i]=0;
			dir[i]=lift[a][i];
		}
		memset(visited,false,sizeof(visited));
		dir[a]=0;
		visited[a]=true;
		for (ki=1;ki<n;ki++){
			low=INF+1;
			for (i=1;i<=n;i++)
				if (!visited[i]&&dir[i]<low){
					low=dir[i];
					t=i;
				}
			visited[t]=true;
			for (i=1;i<=n;i++)
				if (!visited[i]&&dir[t]+lift[t][i]<dir[i])
					dir[i]=dir[t]+lift[t][i];
		}
		if (dir[b]<INF)
			printf("%d\n",dir[b]);
		else printf("-1\n");
	}
	return 0;
}


HDU1548Dijkstra和BFS】 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1548 【广搜BFS分析】有N层楼,每层只能上或者下Ni阶,但是不能低于一层或高于N层,所以,每一次往两个方向搜索。 #include&lt;iostream&gt; #include&lt;queue&gt; #include&lt;algorithm&gt; #include&lt;cstdio... 阅读详情

相关推荐

HDU1548:A strange lift(Dijkstra && BFS)

Problem Description There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When yo

ACM!荣耀之路! 1万+

hdu 1548 最小生成树dijkstra,关键建图

http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:一个特别的电梯,按up可升上k[i]层,到大i+k[i]层,down则到达i-k[i]层,最高不能超过n,最低不能小于1,给你一个起点和终点,问最少可以按几次到达目的地。 分析:把n转换为一个n*n初始化为0的数组,后对每一层可到达的楼层加上一条边为1, 这样就转换为求两点之间的最短...

weixin_38168838的博客 172

HDU1548-A strange lift-最短路(Dijkstra模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 这个题目很容易让人用广搜。。。无语。。。 #include #include #include #include #include #include #include #include #include #include #include #define LL long long #defi

wlxsq的专栏 1286

hdu1548

题意:一个人要从某一层电梯到另一层电梯 ,每层电梯都只能上或者下特定层数,问你从某一层到另一层最少需要按多少次电梯按钮 如果你看了数据范围的话你就会想到用BFS广搜 用广搜就会想到队列   我先说说队列的几个基本语句吧 include quequeq 入队q.push(x)将x接到对位 q.pop(x)弹出队列的第一个元素 q.front(x)访问数组的第一个元素 q.back(

授人以鱼不如授人以渔 1586

hdu 1548 A strange lift Dijkstra+SPFA算法AC

Problem Description There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go u

Lionel 967

HDU 1548 A strange lift (Dijkstra算法)

Choose the best route Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 62   Accepted Submission(s) : 27 Font: Times New Roman | Verdana |

超越自我就是成功 438

hdu 1548 A strange lift (dijkstra算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间。 注意的几点 (1)每次按一下,只能表示上或者是下,然后根据输入的看是上几层或者是下几层。 (2)注意不能到底不存在的楼层。 详见代码。 1 #include <iostream>...

weixin_30593443的博客 81

最短路算法 Dijkstra算法 HDU HDOJ 1548 A strange lift ACM

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目描述: 题目下! //算法分析 最短路 //Made by syx //Time 2010年9月1日 09:25:35 /* Dijkstra算法的基本思路是: 假设每个点都有一对标号 (dj, pj),其中dj是从起源点s到点j的最短...

weixin_30478757的博客 120

最短路径-Dijkstra算法总结(附题:HDU-1548 A strange lift

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17554    Accepted Submission(s): 6559 Problem Description There is a strange l...

Apple_hzc的博客 520

HDU1548——A strange lift(最短路径:dijkstra算法)

A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and ...

weixin_30901729的博客 136

HDU 1548 --- A strange lift】BFS||最短路问题(floyd,dijkstra都可以)

HDU 1548 --- A strange lift】BFS||最短路问题(floyd,dijkstra都可以) Description There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every f...

来日浅谈的博客 575

hdu 1548 A strange lift(bfs||dijkstra

hdu 1548 A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 ProblemDescription Thereisastrangelift.Theliftcanstopcanateveryfloorasyouwant,andthereisanum...

diansitao5461的博客 161

Dijkstra】-HDU-1548-A strange lift

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目描述: 有一个电梯,上面只有UP 和DOWN 两种按钮,每层楼梯有一个值,按下UP按钮就会上升这么多层,按下DOWN按钮就会下降这么多层。问对于给定的从A层到B层,至少需要按几次按钮? 解题思路: 还是基础的Dijkstra求最短路,注意判断边界条件,每条路径的花费都是1。 AC代

邂逅黛玛姑娘 955

hdu-1548 A strange lift

http://acm.hdu.edu.cn/showproblem.php?pid=1548

有一种力量叫乐观 683

HDU1548 A strange lift Dijkstra BFS

HDU1548 A strange lift Dijkstra BFS

htrdchh的博客 143

Hdu-1548 A strange lift

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意: 有一个建筑有N层,里面有一架电梯。给你一个起点层数和一个终点层数。there is a number Ki(0 ,在每一层都有一个按钮,按钮上面有一个数字,表示

飘过的小牛 1845
上一篇: Dark roads hdu2988 Kruskal
下一篇: 最大报销额 hdu1864 01背包
花酱_
博客等级 码龄14年 30粉丝 100原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值