###### hdu 1548 最短路

A strange lift

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:65536/32768 K (Java/Others)
Total Submission(s): 19482    Accepted Submission(s): 7214

Problem Description

There is a strange lift.The lift canstop 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 floori,if you press the button "UP" , you will go up Ki floor,i.e,you willgo 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, thelift can't go up high than N,and can't go down lower than 1. For example, thereis a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Beginingfrom the 1 st floor,you can press the button "UP", and you'll go upto the 4 th floor,and if you press the button "DOWN", the lift can'tdo it, because it can't go down to the -2 th floor,as you know ,the -2 th floorisn't exist.
Here comes the problem: when you are on floor A,and you want to go to floorB,how many times at least he has to press the button "UP" or"DOWN"?

Input

The input consists of several testcases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) whichdescribe 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 ainterger, the least times you have to press the button when you on floor A,andyou want to go to floor B.If you can't reach floor B,printf "-1".

Sample Input

51 5

33 1 2 5

0

Sample Output

3

在n个点都有与之对应的上升或者下降多少层  那么我们可以根据这个条件来构造一个有向图
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&a);
            if(i + a <= n)
                map[i][i+a] = 1;
            if(i - a >= 1)
                map[i][i-a] = 1;
        }

AC代码：

#include <iostream>
#include <cstring>
#include <cstdio>
#define INF 0x3f3f3f3f
#define MAX 1000
using namespace std;
int n;
int map[MAX][MAX],dist[MAX],s[MAX];
void dij(int v)
{
memset(s,0,sizeof(s));
for(int i = 1; i <= n; i++)
{
dist[i] = map[v][i];
s[i] = 0;
}
dist[v] = 0;
s[v] = 1;
int u = 0,min;
for(int i = 2; i <= n; i++)
{
min = INF;
for(int j = 1; j <= n; j++)
{
if(s[j]==0&&dist[j]<min)
{
min = dist[j];
u = j;
}
}
if(min == INF)
break;
s[u] = 1;
for(int j = 1; j <= n; j++)
{
if(s[j]==0&&map[u][j] + dist[u] < dist[j])
dist[j] = map[u][j] + dist[u];
}
}
}
int main()
{
while(~scanf("%d",&n))
{
if(n==0)
break;
int A,B,a;
memset(map,INF,sizeof(map));
scanf("%d%d",&A,&B);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a);
if(i + a <= n)
map[i][i+a] = 1;
if(i - a >= 1)
map[i][i-a] = 1;
}
dij(A);
if(dist[B] < INF)
printf("%d\n",dist[B]);
else
printf("-1\n");
}
return 0;
}



#### hdu1548(BFS广搜入门)

2016-03-01 23:32:39

#### hdu1548(BFS)

2015-03-08 22:46:31

#### HDU-2544 最短路【最短路】

2012-02-25 09:31:02

#### hdu oj 2544 最短路(最短路径）

2014-08-10 15:16:25

#### 杭电 HDU ACM 1548 A strange lift（简单广搜）

2015-07-31 10:00:09

#### 最短路习题集（hdu）

2016-08-05 10:22:07

#### HDU-2433 Travel（最短路[Dijkstra]）

2015-08-14 16:14:09

#### 最短路径 贪心实现代码

2009年09月06日 2KB 下载

#### ACM-最短路（SPFA,Dijkstra,Floyd）之最短路——hdu2544

2014-05-27 11:59:54

#### 2017多校训练赛第四场 HDU 6071（最短路）

2017-08-03 22:44:40

hdu 1548 最短路