zoukankan      html  css  js  c++  java
  • hdu 1548 A strange lift (dijkstra)

    A strange lift
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 32529    Accepted Submission(s): 11664

    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
     

    C/C++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <string>
     5 #include <cstdio>
     6 #include <cstring>
     7 #include <climits>
     8 #include <algorithm>
     9 #define INF 0xffffff
    10 using namespace std;
    11 
    12 int n, my_begin, my_end, my_map[210][210];
    13 
    14 int my_dijkstra()
    15 {
    16     int my_book[210] = {0}, my_dis[210];
    17     my_book[my_begin] = 1;
    18     for (int i = 1; i <= n; ++ i)
    19         my_dis[i] = my_map[my_begin][i];
    20     while (1)
    21     {
    22         int my_pos = -1, my_min = INF;
    23         for (int i = 1; i <= n; ++ i)
    24         {
    25             if (!my_book[i] && my_dis[i] < my_min)
    26             {
    27                 my_min = my_dis[i];
    28                 my_pos = i;
    29             }
    30         }
    31         if (my_pos == -1) break;
    32         my_book[my_pos] = 1;
    33         for (int i = 1; i <= n; ++ i)
    34             if (!my_book[i])
    35                 my_dis[i] = min(my_dis[i], my_dis[my_pos] + my_map[my_pos][i]);
    36     }
    37     if (my_dis[my_end] == INF) return -1;
    38     return my_dis[my_end];
    39 }
    40 
    41 int main()
    42 {
    43     while (~scanf("%d", &n), n)
    44     {
    45         for (int i = 1; i <= n; ++ i)
    46             for (int j = 1; j <= n; ++ j)
    47                 my_map[i][j] = i == j ? 0 : INF;
    48 
    49         scanf("%d%d", &my_begin, &my_end);
    50         for (int i = 1; i <= n; ++ i)
    51         {
    52             int temp;
    53             scanf("%d", &temp);
    54             if (i - temp >= 1) my_map[i][i-temp] = 1;
    55             if (i + temp <= n) my_map[i][i+temp] = 1;
    56         }
    57         printf("%d
    ", my_dijkstra());
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    让程序用自定义的菜单自定义菜单AVKON_VIEW,CBA,MENU_BAR,MENU_PANE
    symbian 菜单不显示的原因
    子类中调用父类的带参数的构造函数|子类构造函数调用父类构造函数 的说明
    symbian 设置 透明背景
    IOS App资源路径
    Nonblock I/O 及其使用
    CEikStatusPane MakeVisible kernexec 3错误
    把mapinfo图层的经纬度信息导出来的办法
    解决安装macports,不能更新的问题
    jpg结构解析
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9438542.html
Copyright © 2011-2022 走看看