题目链接: 传送门

A strange lift

Time Limit: 1000MS     Memory Limit: 32768 K

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

解题思路:

将问题转化为最短路,电梯可到达的楼层权值设为1,否则设置为INF,跑一下Dijkstra就完了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = 205;
int edge[MAX][MAX],dis[MAX];
bool vis[MAX];
int N,A,B;

void Dijkstra()
{
    int tmp,pos;
    memset(vis,false,sizeof(vis));
    for (int i = 1;i <= N;i++)
    {
        dis[i] = edge[A][i];
    }
    dis[A] = 0;
    vis[A] = true;
    for (int i = 2;i <= N;i++)
    {
        tmp = INF;
        for (int j = 1;j <= N;j++)
        {
            if (!vis[j] && dis[j] < tmp)
            {
                tmp = dis[j];
                pos = j;
            }
        }
        if (tmp == INF) break;
        vis[pos] = true;
        for (int j = 1;j <= N;j++)
        {
            if (dis[pos] + edge[pos][j] < dis[j])
            {
                dis[j] = dis[pos] + edge[pos][j];
            }
        }
    }
    printf("%d\n",dis[B] == INF?-1:dis[B]);
}

int main()
{
    while (~scanf("%d",&N) && N)
    {
        int tmp;
        memset(edge,INF,sizeof(edge));
        for (int i = 0;i <= N;i++)
        {
            for (int j = 0;j <= i;j++)
            {
                if (i == j) edge[i][j] = edge[j][i] = 0;
                else    edge[i][j] = edge[j][i] = INF;
            }
        }
        scanf("%d%d",&A,&B);
        for (int i = 1;i <= N;i++)
        {
            scanf("%d",&tmp);
            if (i - tmp > 0)
            {
                edge[i][i-tmp] = 1;
            }
            if (i + tmp <= N)
            {
                edge[i][i+tmp] = 1;
            }
        }
        Dijkstra();
    }
    return 0;
}

HDU 1548 A strange lift (最短路/Dijkstra)的更多相关文章

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

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

  2. hdu 1548 A strange lift

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

  3. 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 ...

  4. HDU 1548 A strange lift (Dijkstra)

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

  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 宽搜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(迪杰斯特拉,邻接表)

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

  8. HDU 1548 A strange lift 搜索

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

  9. hdu 1548 A strange lift (bfs)

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

随机推荐

  1. PRML读书会第五章 Neural Networks(神经网络、BP误差后向传播链式求导法则、正则化、卷积网络)

    主讲人 网神 (新浪微博:@豆角茄子麻酱凉面) 网神(66707180) 18:55:06 那我们开始了啊,前面第3,4章讲了回归和分类问题,他们应用的主要限制是维度灾难问题.今天的第5章神经网络的内 ...

  2. 简单高效的nodejs爬虫模型

    这篇文章讲解一下yunshare项目的爬虫模型. 使用nodejs开发爬虫很简单,不需要类似python的scrapy这样的爬虫框架,只需要用request或者superagent这样的http库就能 ...

  3. bootstrap的popover插件在focus模式时在Safari浏览器无法使用的bug解决方案

    前言 最近在使用bootstrap的popover插件,效果如下: popover插件的focus模式时表现为当点击按钮时弹出浮动层,在点击浮动层外的任何一处,都隐藏浮动层. 但是在mac下的Safa ...

  4. Koa框架实践与中间件原理剖析

     最近尝试用了一下Koa,并在此记录一下使用心得. 注意:本文是以读者已经了解Generator和Promise为前提在写的,因为单单Generator和Promise都能够写一篇博文来讲解介绍了,所 ...

  5. (六)观察者模式详解(包含观察者模式JDK的漏洞以及事件驱动模型)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 本章我们讨论一个除前面的单例 ...

  6. C# Rotating Oval

    This program is used to show how to generate an oval. The moon's orbit around the sun is an oval two ...

  7. SQL-Server下载地址

    有同学费尽心思的找SQL server数据库各版本的下载地址,看到别人的求助贴就不自觉的想去帮助他们,但是一个一个去帮助又不太现实,毕竟个人精力有限,既然大家有需求,那么艾薇百科今天就本着乐于分享和奉 ...

  8. Spring + SpringMVC + MyBatis

    1.需求说明实现用户通过数据库验证登录需求,采用Myeclipse+Tomcat 6.0+Mysql 5.0+JDK 1.6 2.数据库表开发所用是Mysql数据库,只建立单张用户表T_USER,表结 ...

  9. jqMobile中pageinit,pagecreate,pageshow等函数的执行顺序

    常见的共有5个page函数,刚开始有点迷糊的是到底谁先谁后执行. 实验告诉我们结果: var temp = ''; $('body').live('pagechange', function () { ...

  10. 使用D3绘制图表(3)--添加坐标轴和文本标签

    上一篇是曲线的绘制,这样仅仅只是有一条线,完全先是不出数据想要表现的内容,于是我们要添加坐标系,添加坐标系和画线类似. 1.还是没有变化的html页面 <!DOCTYPE html> &l ...