hdu 1548 (dijkstra解法)(一次AC就是爽)


恭喜福州大学杨楠获得[BestCoder Round #4]冠军(iPad Mini一部)
《BestCoder用户手册》下载

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就是爽),布布扣,bubuko.com

时间: 2024-10-24 20:08:25

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

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

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st

hdu 1548 A strange lift (dijkstra算法)

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

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

364. [HDU 1548] 奇怪的电梯 ★   输入文件:lift.in   输出文件:lift.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第 i 层楼 (1<=i<=N) 上有一个数字 Ki(0<=Ki<=N) .电梯只有四个按钮:开,关,上,下.上下的层数等于当前楼层上的那个数字.当然,如果不能满足要求,相应的按钮就会失灵.例如: 3 3 1 2 5 代表

HDU 3065 病毒侵袭持续中 AC自动机题解

其实本题比HDU的病毒侵袭1还简单,不过有一个陷阱卡到我了:就是搜索text的时候,当遇到的字母不是大写字母的时候,那么就要重新从根节点开始搜索,否则就会答案错误. 那么一点陷阱,居然没想到啊. 教训啊:看来对不太平常的地方,需要更加深入的思考,才能发现其中的陷阱,否则就WA了. #include <stdio.h> #include <string.h> #include <queue> using std::queue; const int MAX_N = 1001

hdu 2680 (Dijkstra)

快放暑假了,训练又要开始了.先从熟悉的图论开始做吧. 题意:一张有向图中有若干起点一个终点,让你算最短路,方法很简单只需人为加一个起点指向所有起点让后距离为0即可. 代码如下: 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <set> 5 #include <iostream> 6 #include <queue> 7 #inc

HDU 2112 HDU Today (Dijkstra算法)

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13952    Accepted Submission(s): 3264 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

HDU 1548 (最基础的BFS了) A strange lift

这是一维的BFS,而且没有什么变形,应该是最基础的BFS了吧 题意: 有这样一个奇葩的电梯,你在第i层的时候你只能选择上或者下Ki层,也就是你只能从第i层到达i+Ki或者i-Ki层.当然电梯最低只能在1层最高只能在n层. 给出起点和终点问最少需要多少次才能到达终点,如果不能到达输出-1 没有什么好解释的了,如此单纯的一道题,只要不是太粗心就能A过去 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #incl

HDU 3341 Lost&#39;s revenge(AC自动机+状压DP)

Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4548    Accepted Submission(s): 1274 Problem Description Lost and AekdyCoin are friends. They always play "number game"(A bor

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): 11341    Accepted Submission(s): 4289 Problem Description There is a strange lift.The lift can stop can at every floor as you want