A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18723    Accepted Submission(s):
6926

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 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
 
Recommend
8600   |   We have carefully selected several similar
problems for you:  1385 1142 1372 1072 1690 
 
最短路的模板题,代码还是很好理解的。
 

题意:一个特别的电梯,按up可升上k[i]层,到大i+k[i]层,down则到达i-k[i]层,最高不能超过n,最低不能小于1,给你一个起点和终点,问最少可以按几次到达目的地。在一个N层高的楼有一个奇怪的电梯,在每一层只能上升或下降一个特定的层数,中间不会停止,在给定的条件下,问能不能到达指定楼层,可以到达的话返回转操作次数,不可以的话返回-1.

附上代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#define M 205
#define MAX 0x3f3f3f3f
using namespace std;
int map[M][M],vis[M],dis[M];
int main()
{
int n,a,b,i,j,s;
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
if(i==j)
map[i][j]=; //同一个地方距离为0
else
map[i][j]=MAX;
}
for(i=; i<=n; i++)
{
scanf("%d",&s);
if(i+s<=n) //标记这一层电梯可以去的楼层
map[i][s+i]=;
if(i-s>=)
map[i][i-s]=;
}
vis[a]=; //起点已走过
for(i=; i<=n; i++)
dis[i]=map[a][i]; //初始化距离为每个点到起点的距离
int min,k,t;
for(i=; i<=n; i++)
{
min=MAX;
for(j=; j<=n; j++)
if(!vis[j]&&dis[j]<min) //每次都找离终点最近的点
{
min=dis[j];
t=j;
}
vis[t]=; //标记为已经找过此点
for(j=; j<=n; j++)
if(!vis[j]&&map[t][j]<MAX) //从最近的点到下一个点的距离与初始距离进行比较
if(dis[j]>dis[t]+map[t][j])
dis[j]=dis[t]+map[t][j];
}
if(dis[b]<MAX)
printf("%d\n",dis[b]);
else //不能到 则输出-1
printf("-1\n");
}
return ;
}

邻接表代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
int from,to,val,next;
}edge[];
int tol,s,t,n;
int dis[];
bool vis[];
int head[]; void init()
{
tol=;
memset(head,-,sizeof(head));
} void addEdge(int u,int v)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].val=;
edge[tol].next=head[u];
head[u]=tol++;
} void getmap()
{
int x;
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(i-x>=) addEdge(i,i-x);
if(i+x<=n) addEdge(i,i+x);
}
memset(vis,false,sizeof(vis));
memset(dis,inf,sizeof(dis));
} void spfa()
{
queue<int>q;
q.push(s);
vis[s]=true;
dis[s]=;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].val)
{
dis[v]=dis[u]+edge[i].val;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
if(dis[t]<inf)
printf("%d\n",dis[t]);
else
printf("-1\n");
return;
} int main()
{
int i,j;
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&s,&t);
init();
getmap();
spfa();
}
}

hdu 1548 A strange lift(迪杰斯特拉,邻接表)的更多相关文章

  1. HDU 1548 A strange lift (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

  2. HDU 3339 In Action(迪杰斯特拉+01背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...

  3. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

  4. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  5. HDU 1548 A strange lift (Dijkstra)

    https://vjudge.net/problem/HDU-1548 题意: 电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点 ...

  6. hdu 1548 A strange lift 宽搜bfs+优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...

  7. HDU 1548 A strange lift (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

  8. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  9. HDU 1548 A strange lift 搜索

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. Uva11384 Help is needed for Dexter

    Dexter is tired of Dee Dee. So he decided to keep Dee Dee busy in a game. The game he planned for he ...

  2. python正则表达式应用 定义一个函数,求字符串中出现的所有整数之和

  3. JQuery--动画队列以及清空队列.stop()方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. JQuery--mouseover()与moseout()的区别

    mouseover()与mouseout()区别 普通鼠标移入移出事件 语法: mouseover()/mouseout() 功能: 当鼠标移入/移出到添加事件的元素或其子元素的时候,都会被触发!!m ...

  5. centOS7 安装vsftp服务器

    一.目的:有许多时候我们需要从自己机器上,上传文件到Linux服务器上,想要上传文件就必须要通过FTP 协议(File Transfer Protocol(文件传输协议)).所以要在服务器上配置FTP ...

  6. 搭建直播服务器,使用nginx与nginx-rtmp-module搭建流媒体服务器;

    现在,一起学习一下如何自己搭建一个流媒体服务器吧! 本次搭建流媒体使用的环境是centos 7.0+nginx: 让我们一起开始奇妙的流媒体之旅吧! 1.下载nginx-rtmp-module: ng ...

  7. R语言-组间差异的非参数检验

    R语言-组间差异的非参数检验 7.5 组间差异的非参数检验 如果数据无法满足t检验或ANOVA的参数假设,可以转而使用非参数方法.举例来说,若结果变量在本质上就严重偏倚或呈现有序关系,那么你可能会希望 ...

  8. python中的open函数

    open函数用于文件处理 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 1 文件句柄 = open('文件路径', '模式') 打开文件时,需要指定文件路径和以何等方式打开文件 ...

  9. @codechef - KILLER@ Painting Tree

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 N 个点的有根树,标号 1 到 N,以 1 为根.定义 ...

  10. Apache CarbonData1.3简介

    CarbonData是一种高性能大数据存储方案,支持快速过滤查找和即席OLAP分析,已在20+企业生产环境上部署应用,其中最大的单一集群数据规模达到几万亿.针对当前大数据领域分析场景需求各异而导致的存 ...