A strange lift

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 up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?
Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
Sample Input
5 1 5
3 3 1 2 5
0
Sample Output
3

题目大意:

    从前有一栋楼,楼里面有一座电梯,电梯非常奇葩。

    一栋楼有N层(1-N),楼层i的电梯可以去i-k[i]和i+k[i]层,(k数组题目给定)(即按了一次按钮)

    求从A层到B层最少的按按钮次数。

解题思路:

    BFS可解,做过类似的题,所以这里使用dijkstra算法。

    最短路径dijkstra算法可解,begin为i,end为i+k[i]&&i-k[i],边的权值为1。求从A到B的最短路径就是答案。(注意为有向图)

Code:

 #include<stdio.h>
#include<limits.h>
#include<iostream>
#include<string.h>
#define MAXN 200
using namespace std;
int edge[MAXN+][MAXN+];
int dis[MAXN+];
bool vis[MAXN+];
int T,S,D,N,k;
void dijkstra(int begin)
{
memset(vis,,sizeof(vis));
for (int i=; i<=T; i++)
dis[i]=INT_MAX;
dis[begin]=;
for (int t=; t<=T; t++)
{
vis[begin]=;
for (int i=; i<=T; i++)
if (!vis[i]&&edge[begin][i]!=INT_MAX&&dis[begin]+edge[begin][i]<dis[i])
dis[i]=dis[begin]+edge[begin][i];
int min=INT_MAX;
for (int j=; j<=T; j++)
if (!vis[j]&&min>dis[j])
{
min=dis[j];
begin=j;
}
}
}
int main()
{
int begin,end;
while (cin>>T)
{
if (T==) break;
for (int i=;i<=MAXN;i++)
for (int j=;j<=MAXN;j++)
edge[i][j]=INT_MAX;
scanf("%d %d",&begin,&end);
int t;
for (int i=;i<=T;i++)
{
scanf("%d",&t); //注意是有向图!一开始因为这个WA了好几次。
if (i+t<=T) edge[i][i+t]=;
if (i-t>=) edge[i][i-t]=;
}
dijkstra(begin);
if (dis[end]!=INT_MAX) printf("%d\n",dis[end]);
else printf("-1\n");
}
return ;
}

HDU1548——A strange lift(最短路径:dijkstra算法)的更多相关文章

  1. 网络最短路径Dijkstra算法

    最近在学习算法,看到有人写过的这样一个算法,我决定摘抄过来作为我的学习笔记: <span style="font-size:18px;">/* * File: shor ...

  2. 单源最短路径Dijkstra算法,多源最短路径Floyd算法

    1.单源最短路径 (1)无权图的单源最短路径 /*无权单源最短路径*/ void UnWeighted(LGraph Graph, Vertex S) { std::queue<Vertex&g ...

  3. 最短路径-Dijkstra算法与Floyd算法

    一.最短路径 ①在非网图中,最短路径是指两顶点之间经历的边数最少的路径. AE:1    ADE:2   ADCE:3   ABCE:3 ②在网图中,最短路径是指两顶点之间经历的边上权值之和最短的路径 ...

  4. 数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )

    数据结构实验之图论七:驴友计划 Time Limit: 1000 ms           Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  5. 最短路径——Dijkstra算法以及二叉堆优化(含证明)

    一般最短路径算法习惯性的分为两种:单源最短路径算法和全顶点之间最短路径.前者是计算出从一个点出发,到达所有其余可到达顶点的距离.后者是计算出图中所有点之间的路径距离. 单源最短路径 Dijkstra算 ...

  6. 有向网络(带权的有向图)的最短路径Dijkstra算法

    什么是最短路径? 单源最短路径(所谓单源最短路径就是只指定一个顶点,最短路径是指其他顶点和这个顶点之间的路径的权值的最小值) 什么是最短路径问题? 给定一带权图,图中每条边的权值是非负的,代表着两顶点 ...

  7. Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例

    本文实例讲述了Python数据结构与算法之图的最短路径(Dijkstra算法).分享给大家供大家参考,具体如下: # coding:utf-8 # Dijkstra算法--通过边实现松弛 # 指定一个 ...

  8. 求两点之间最短路径-Dijkstra算法

     Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.D ...

  9. 最短路径—Dijkstra算法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

随机推荐

  1. ArcSDE for oracle10g安装后post的时候出现错误

    The Post Installation Setup can not locate required Oracle files in your path.Check your Oracle inst ...

  2. Android 基础(设备显示密度/图片自适应

    1. 设备的 显示密度 是由 设备的尺寸 和 设备的分辨率 两个因素决定的. 相同分辨率设备的尺寸越大显示密度越小, 相同尺寸的设备,分辨率越高显示密度越高. 2. 显示密度等级:  160 / 24 ...

  3. C# 与C++的数据转换

    一.类型转化 下面重点罗列下常用的类型转化. C++类型 C#类型 备注说明 Int Int16.Int32 没有悬念,直接转化 Uint UInt16.Uint32.int 在程序中,不太清楚是,就 ...

  4. javascript 面向对象技术

    面向对象术语 对象 ECMA-262 把对象(object)定义为“属性的无序集合,每个属性存放一个原始值.对象或函数”.严格来说,这意味着对象是无特定顺序的值的数组. 尽管 ECMAScript 如 ...

  5. laravel5.1关于lists函数的bug

    查询语句为: class DateAttrModel extends BaseModel{ -- static function getDays(--){ $days = self::lists('d ...

  6. $.ligerDialog 操作

    //关闭 $.ligerDialog.open 打开的弹窗 frameElement.dialog.close(); //关闭父窗口 parent.$.ligerDialog.close(); //关 ...

  7. API删除文件

    using System; using System.Runtime.InteropServices; namespace ConsoleApplication1 { class Program { ...

  8. Asp.net MVC 如何向webform一样在IIS里添加虚拟目录

    相信很多用webform的程序猿都习惯性的使用虚拟目录的形式来对一个程序添加新的功能,那么在mvc下该如何来弄呢? 首先得有一个项目基层的项目,然后我们在这个项目的基础上新增一个功能模块,例如信息发布 ...

  9. 然爸读书笔记(2013-4)----打造facebook

    扎克伯格的真实一面 (1)在公司内部知无不言,扎克伯格在公司内部问答时间.尽可能回答员工的任何问题,保持足够的透明度. (2)员工只有做到对外守口如瓶,我们才能做到对内知无不言. (3)faceboo ...

  10. office2010 office2013打开个别PPT时需要修复的解决方法

    写在前面的废话(请直接查看正文部分):一次意外之后,需要重装Microsoft office,于是屁颠屁颠就重装了一次MS office 2013,装好后发现,打开个别ppt/pptx时打不开,提示修 ...