A strange lift

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

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 1242 1142 1217 2066 
 

这道题刚拿到的时候,我就觉得和最短路径没关系。不过,在后来的思考中我慢慢发现了它与最短路径间的
联系。这道题要求解的是最少的按键次数并要求能够到达目的楼层,而且每个楼层只能向上或向下移动该楼层
规定的层数,那么从a楼层到规定楼层只需按一次键即可。这里,我们可以将这一过程模拟成最短路径中等的
从a地道另一个指定位置的过程,而两地的距离即权值就是1,那么上下楼层的问题可以看成是从一个地方
到另一个地方,而两地的间距都是1的移动过程。最后,在通过dijkstra算法来统计出从起点到终点所需的
按键次数即可,若可到达就输出次数,不可到达的话次数为无穷大,输出-1。

AC代码:

#include<stdio.h>
#include<string.h>
#define max 0x3f3f3f3f
int map[201][201];
int dist[201];
int floor[201];
void dijkstra(int num,int v)
{
 bool vis[201];
 memset(vis,0,sizeof(vis));
 int i,j;
 for(i=1;i<=num;i++)
 {
  dist[i]=map[v][i];
 }
 dist[v]=0;
 vis[v]=1;
 for(i=2;i<=num;i++)
 {
  int tmp=max;
  int u=v;
  for(j=1;j<=num;j++)
   if((!vis[j])&&dist[j]<tmp)
   {
    u=j;
    tmp=dist[j];
   }
  vis[u]=1;
  for(j=1;j<=num;j++)
   if((!vis[j])&&map[u][j]<max)
   {
    int newdist=dist[u]+map[u][j];
    if(newdist<dist[j])
    {
     dist[j]=newdist;
    }
   }
 }

}
int main()
{
 int n;
 while(scanf("%d",&n)!=EOF&&n)
 {
  memset(map,max,sizeof(map));
  int start,end;
  scanf("%d%d",&start,&end);
  int i;
  for(i=1;i<=n;i++)
  {
   scanf("%d",&floor[i]);
   if(floor[i]+i<=n)
    map[i][floor[i]+i]=1;
   if(i-floor[i]>=1)
    map[i][i-floor[i]]=1;
  }
  dijkstra(n,start);
  if(dist[end]==max)
   printf("-1\n");
  else
   printf("%d\n",dist[end]);
 }
 return 0;
}

hdu 1548 (dijkstra解法)(一次AC就是爽)的更多相关文章

  1. cogs 364. [HDU 1548] 奇怪的电梯 Dijkstra

    364. [HDU 1548] 奇怪的电梯 ★   输入文件:lift.in   输出文件:lift.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 呵呵,有一天我做了 ...

  2. 最短路 HDU - 2544 (dijkstra算法或Floyd算法)

    Dijkstra解法: #include <stdio.h> #include <iostream> #include <cstring> #include < ...

  3. (重刷)HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,dijkstra解法。

    floyd解法 今天初看dijkstra,先拿这两题练手,其他变形题还是不是很懂. 模版题,纯练打字... HDU 1874: #include <cstdio> #define MAXN ...

  4. hdu 1548 A strange lift (dijkstra算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几 ...

  5. hdu 1548 楼梯 bfs或最短路 dijkstra

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...

  6. HDU 1548 A strange lift (Dijkstra)

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

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

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

  8. HDU 1548 A strange lift(Dijkstra,简单BFS)

    题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数, ...

  9. HDU 1548 A strange lift (Dijkstra)

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

随机推荐

  1. for循环语句示例应用

    age = 22 #优化前 ''' for i in range(10): guess_num = int(input('input your guess num:')) if guess_num = ...

  2. ROS的文件系统 (二)

    ROS的文件系统的基本概念, ROS文件系统中的两个最基本的概念:Package和Manifest,即包和清单文件. Package是组织ROS代码的最基本单位,每一个Package都可以包括库文件, ...

  3. Java 枚举&注解

    枚举类 如何自定义枚举类 JDK1.5之前需要自定义枚举类 JDK 1.5 新增的 enum 关键字用于定义枚举类 若枚举只有一个成员, 则可以作为一种单例模式的实现方式 //枚举类 class Se ...

  4. C- struct的使用

    数组是二等公民,不能进行整体赋值,或者参数传递,或者作为返回值. But!如果封装在struct内部,就完全不一样了 #include <iostream> using namespace ...

  5. Rhel6-mailsystem配置文档

    (postfix+dovecot+mysql+extmail) 理论基础:

  6. Dom事件初步了解

    1.事件流 事件流可以分为两种:事件冒泡和事件捕获 1. 事件冒泡就是从目标元素一直冒泡到根元素html(IE和DOM浏览器都有) 2. 事件捕获就是从根元素到目标元素(DOM浏览器支持) 2.事件处 ...

  7. == 和 isEqualToString的区别之备忘

    == 比较的是指针 isEqualToString 比较的是指针指向的内容 比如: NSString * strA = @"abc"; NSString * strB = @&qu ...

  8. julia解无忧公主的数学时间097.jl

    julia解无忧公主的数学时间097.jl #=""" julia解无忧公主的数学时间097.jl http://mp.weixin.qq.com/s?__biz=MzI ...

  9. JS学习第一课

    1.js 按照编写顺序执行 2.输出使用document.write. 3.申明数组 var array = [1,2,3,5] ;  var arrStr = ["sgsg",& ...

  10. acm 20140825

    为了自己的梦想,一次次的选择坚强.走上acm这条路,怎么也找不到让自己放弃的理由.我喜欢这种竞赛的氛围,我渴望在赛场上飞扬!想想过去的一个学习,自己并没有干点什么有意义的事.acm也没有好好的做!新的 ...