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



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

相关文章推荐

hdu1548A strange lift——最短路(迪杰斯特拉,spfa)。bfs(队列。数组)

Dijkstra+spfa+数组模拟队列+bfs,

HDU 1548 A strange lift(最短路问题Dijkstra & bfs两种解法)

Problem Description There is a strange lift.The lift can stop can at every floor as you want, and t...

HDU 1548 最短路---A strange life

Problem Description There is a strange lift.The lift can stop can at every floor as you want, and t...

HDU1548 奇怪的电梯 最短路 spfa bfs

题意:电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点,要求出最少要按几次键。   分析:很不错的一道题。可以搜索,也可以spf...

hdu1548 A strange lift 不错的变相最短路考察

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

hdu1548 最短路/BFS

指定起点和终点,用dijskal求最短路 #include #include #include #include #include #include #include #include #includ...

HDU 1548 A strange lift(最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题面: A strange lift Time Limit: 2000/1...

HDU 1548 A strange lift 最短路变形

点击打开链接 A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java...

hdu 1548 A strange lift(最短路)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • acm_cxq
  • acm_cxq
  • 2016年05月24日 14:08
  • 171

HDU-1548 A strange lift(最短路[Spfa || BFS])

第一眼看就知道应该要搜索,要不是放在最短路专题,还真想不到用图论做,简单题,刚好练习一下Spfa(看到Spfa时间效率不是很稳定,还是多用Dijkstra的好) BFS占用内存少,运行快,代码少,如果...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1548 最短路
举报原因:
原因补充:

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