A strange lift(Dijistra优化)

A strange lift

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

这题一开始用基本的dijistra老是时间超限,就试了优化了的,运行时间就变15ms了,快多了。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <vector>
 5 #include <queue>
 6 using namespace std;
 7 const int N = 220;
 8 const int INF = 1e7;
 9 int d[N], n, a, b;
10
11 struct edge{
12     int to, cost;
13 };
14 vector<edge> g[N];
15 typedef pair<int,int> P;
16 void dijistra(int s){
17     priority_queue<P,vector<P>,greater<P> > que;
18     d[a] = 0;
19     que.push(P(0,s));
20     while(!que.empty()){
21         P p = que.top();que.pop();
22         int v = p.second;
23         if(d[v] < p.first) continue;
24         for(int i = 0; i < g[v].size(); i ++){
25             edge e = g[v][i];
26             if(d[e.to] > d[v] + e.cost){
27                 d[e.to] = d[v] + e.cost;
28                 que.push(P(d[e.to],e.to));
29             }
30         }
31     }
32 }
33
34
35 int main(){
36     while(~scanf("%d",&n)&&n){
37         for(int i = 0; i < N; i ++){
38             d[i] = INF;
39             g[i].clear();
40         }
41         scanf("%d %d",&a,&b);
42         for(int i = 1; i <= n; i ++){
43             int tmp;
44             edge e;
45             scanf("%d",&tmp);
46             if(i-tmp > 0){
47                 e.to = i-tmp;
48                 e.cost = 1;
49                 g[i].push_back(e);
50             }
51             if(i+tmp <= n){
52                 e.to = i + tmp;
53                 e.cost = 1;
54                 g[i].push_back(e);
55             }
56         }
57         dijistra(a);
58         printf("%d\n",(d[b]==INF)?-1:d[b]);
59     }
60     return 0;
61 }
时间: 2024-07-30 22:02:51

A strange lift(Dijistra优化)的相关文章

A strange lift

点击打开链接 题意:有n层楼层,现在在每一层有两个按钮,分别为up和down,按动按钮时,可以向上或向下跳动num[ i ]层:问能否以最少的次数从A到B层,不能输出-1: 解析:构图,将从i层到按动按钮后跳转的楼层,看作连通状态,赋值为1,这样就转换成单源最短路问题: #include<iostream> #include<cstring> #include<cstdio> using namespace std; const int maxn = 500; cons

HDU 1548 A strange lift【不错的最短路,spfa】

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21281    Accepted Submission(s): 7786 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548.A strange lift

A strange lift Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1548 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)

hdu1548——A strange lift

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12230    Accepted Submission(s): 4656 Problem Description There is a strange lift.The lift can stop can at every floor as you want

E - A strange lift 【BFS】

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 K

(hdu step 4.2.4)A strange lift(求从起点到终点的最小步数,限制条件是:在一维的情况下)

题目: A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 709 Accepted Submission(s): 348   Problem Description There is a strange lift.The lift can stop can at every floor as you want, a

杭电 1548 A strange lift(广搜)

http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11341    Accepted Submission(s): 4289 Problem Description There is a strange lift.T

hdu1584 A strange lift (电梯最短路径问题)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15570    Accepted Submission(s): 5832 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548 A strange lift(Dijkstra,简单BFS)

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st