hdu 1548 最短路

原创 2016年05月31日 14:43:11

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

 

分析:

在要求得范围内构建一个图  然后dijkstra算法求最短路

主要是如何构造图

在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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 1548 最短路

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 就是Dijkstra算法的应用。。。。不过被坑了好久,原来电梯的up、down是并列的,初始化...
  • zjsyhjh
  • zjsyhjh
  • 2014年05月26日 17:51
  • 261

hdu 1548 最短路

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • sdjzli
  • sdjzli
  • 2013年04月09日 20:23
  • 1325

hdu1317 负权最短路(Floyd+bellman)

首先要读懂题,做这个题时读题看样例琢磨了半天才明白= =; 题目的意思是,一个人从一号房间走到最后一个房间,每个房间有相应的能量值(可正可负),当走到这个房间的时候可以获取这个房间的能量值,如果走到...
  • zs120197
  • zs120197
  • 2016年07月24日 12:45
  • 263

2017多校训练赛第四场 HDU 6071(最短路)

比赛的时候就是蠢……         当时只剩下十几分钟的时候看到这题的……其实很简单的一道最短路,前提是想到了……         题目要求是只有四个点,然后连边成正方形,问从2号点出发,再回到2号...
  • u013534123
  • u013534123
  • 2017年08月03日 22:44
  • 1018

ACM-最短路(SPFA,Dijkstra,Floyd)之最短路——hdu2544

ACM 最短路(SPFA,Dijkstra,Floyd) 最短路 hdu2544
  • lx417147512
  • lx417147512
  • 2014年05月27日 11:59
  • 3405

HDU-1584-蜘蛛牌

HDU-1584-蜘蛛牌
  • loy_184548
  • loy_184548
  • 2016年04月01日 12:22
  • 321

hdu 3790 最短路径问题(多权值的最短路)

#include #include #include using namespace std; const int inf=100000000; struct In { int x, y; }...
  • u013365671
  • u013365671
  • 2014年08月13日 19:55
  • 528

HDU:最短路(单源最短路径Dijistra)

最短路Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis...
  • zugofn
  • zugofn
  • 2016年04月07日 17:52
  • 387

hdu oj 2544 最短路(最短路径)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm...
  • u014253173
  • u014253173
  • 2014年08月10日 15:16
  • 1771

HDU 3790 最短路问题(最短路)

【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=3790题目意思如题,就多问了个花费而已解题思路创个结构体跑模板就可以了代码部分#include ...
  • pashuihou
  • pashuihou
  • 2017年12月16日 21:37
  • 26
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1548 最短路
举报原因:
原因补充:

(最多只允许输入30个字)