zoukankan      html  css  js  c++  java
  • HDU 1548 A strange lift (Dijkstra)

    A strange lift

    http://acm.hdu.edu.cn/showproblem.php?pid=1548

    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
     
    解题思路:注意A == B的情况,以及不能到达的情况就可以了,用Floyd会超时!
     

    解题代码: 

     1 // File Name: A strange lift 1548.cpp
     2 // Author: sheng
     3 // Created Time: 2013年07月19日 星期五 17时13分57秒
     4 
     5 #include <stdio.h>
     6 #include <string.h>
     7 #include <iostream>
     8 using namespace std;
     9 
    10 const int INF = 0x3fffffff;
    11 const int max_n = 202;
    12 int k[max_n];
    13 int f[max_n][max_n], vis[max_n];
    14 int dis[max_n];
    15 
    16 int n, A, B;
    17 
    18 void Dijstra()
    19 {
    20     memset(vis, 0, sizeof (vis));
    21     for (int i = 1; i <= n; i ++)
    22         dis[i] = f[A][i];
    23     vis[A] = 1;
    24     for (int i = 1; i <= n; i ++)
    25     {
    26         int min = INF;
    27         int k = -1;
    28         for (int j = 1; j <= n; j ++)
    29         {
    30             if (!vis[j] && min > dis[j])
    31             {
    32                 min = dis[j];
    33                 k = j;
    34             }
    35         }
    36         if (k == -1)
    37             return;
    38         vis[k] = 1;
    39         for (int j = 1; j <= n; j ++)
    40             if (!vis[j] && dis[j] > dis[k] + f[k][j])
    41                 dis[j] = dis[k] + f[k][j];
    42     }
    43 
    44 }
    45 
    46 int main ()
    47 {
    48     while (~scanf ("%d", &n) && n)
    49     {
    50         for (int i = 1; i <= n; i ++)
    51             for (int j = 1; j <= n; j ++)
    52                 f[i][j] = INF;
    53         scanf ("%d%d", &A, &B);
    54         for (int i = 1; i <= n; i ++)
    55             scanf ("%d", &k[i]);
    56         for (int i = 1; i <= n; i ++)
    57         {
    58             if (i - k[i] >= 1)
    59                 f[i][i - k[i]] = 1;
    60             if (i + k[i] <= n)
    61                 f[i][i + k[i]] = 1;
    62         }
    63         Dijstra();
    64     /*    for (int i = 1; i <= n; i ++)
    65             for (int j = 1; j <= n; j ++)
    66                 for (int k = 1; k <= n; k ++)
    67                     if (f[j][k] > f[j][i] + f[i][k])
    68                         f[j][k] = f[j][i] + f[i][k];
    69     */
    70         if (A == B)
    71             printf ("0
    ");
    72         else if (dis[B] == INF)
    73             printf("-1
    ");
    74         else printf ("%d
    ", dis[B]);
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    Kafka中的数据清理(logdeletion)
    genymotion虚拟器笔记
    layui hover显示图片
    vue 导出到excel
    el-tree知识积累
    js 含有对象的数组去重
    苏宁易购价格爬取(golang)
    vue+elementui+beego笔记
    vue笔记
    beego笔记
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3201343.html
Copyright © 2011-2022 走看看