zoukankan      html  css  js  c++  java
  • hdu 1548(最短路)

    A strange lift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 19516    Accepted Submission(s): 7224


    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
    题意:有一个升降机,可上可下,但是每一层都只能上升或者下降固定的层数,给定起点和终点,问从起点到终点最少需要按多少次电梯按钮,不能到输出-1
    题解:这题难点在于想到建图,但是相通之后就将每一层与其能够到达的层数连一条有向边就OK,然后进行最短路算法就可以得到结果了,实在想不到建图还是可以用搜索做的。
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <math.h>
    #include <queue>
    using namespace std;
    const int N =205;
    const int INF = 9999999;
    int n;
    int graph[N][N];
    int low[N];
    bool vis[N];
    void dijkstra(int s){
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=n;i++){
            low[i] = graph[s][i];
        }
        vis[s] = true;
        for(int i=1;i<n;i++){
            int Min = INF;
            for(int j=1;j<=n;j++){
                if(Min>low[j]&&!vis[j]){
                    s = j;
                    Min = low[j];
                }
            }
            vis[s] = true;
            for(int j=1;j<=n;j++){
                if(low[j]>low[s]+graph[s][j]&&!vis[j]){
                    low[j] = low[s]+graph[s][j];
                }
            }
        }
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF,n){
            int s,t;
            scanf("%d%d",&s,&t);
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(i==j) graph[i][j]=0;
                    else graph[i][j] = INF;
                }
            }
            for(int i=1;i<=n;i++){
                int num;
                scanf("%d",&num);
                if(i-num>=1) graph[i][i-num] = 1;
                if(i+num<=n) graph[i][i+num] = 1;
            }
            dijkstra(s);
            if(low[t]>=INF) printf("-1
    ");
            else printf("%d
    ",low[t]);
        }
        return 0;
    }
  • 相关阅读:
    laravel框架简易对接网易163邮件
    新版PHP7安装redis扩展并在laravel中运用
    make: as86: Command not found
    Ubuntu主题美化
    Ubuntu更换阿里源
    Ubuntu配置中文输入法
    JS内利用Ajax同后端异步交互数据
    更改网页内滚动条效果
    鼠标点击烟花特效
    内存交换分区创建&文件系统观察与操作
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5490086.html
Copyright © 2011-2022 走看看