A Strange LiftTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 15570 Accepted Submission (s): 5832
Problem Descriptionthere is a strange lift. The lift can stop can at every floor as you want, and there are a number ki (0 <= ki <= N) on every floor. The lift has just, buttons:up, and down. When you are at floor i,if Press the button "up", you'll go up Ki floor,i.e,you'll go to the i+ki th floor,as the SAM E, if you press the button ' down ', you'll go down Ki floor,i.e,you'll 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 are a buliding with 5 floors, and K1 = 3, K2 = 3,K3 = 1,k4 = 2, K5 = 5.Begining from the 1st Floor,you Can press the button "up", and you'll go up to the 4th Floor,and if your press the button "down", the lift can ' t does it, be Cause it can ' t go down to the-2nd floor,as you know, the-2nd floor isn ' t exist.
Here comes the problem:when is on floor a,and you want to go to floor b,how many times at least he have to press the Button ' up ' or ' down '?
Inputthe input consists of several test Cases.,each test case contains both lines.
The first line contains three integers n, a, B (1 <= n,a,b <=) which describe above,the second line consist N int Egers k1,k2,.... kn.
A single 0 Indicate the end of the input.
Outputfor each case of the input output a interger, the least times you had to press the button when you were on floor A,and y ou want to go to floor b.if your can ' t reach floor b,printf "-1".
Sample Input
5 1 53 3 1 2 50
Sample Output
3
Recommend8600 | We have carefully selected several similar problems for you:1385 1242 1142 1217 1253
Statistic | Submit | Discuss | Note
Elevators are only two directions, up or down. Since we are seeking the shortest path, we also use the Dijkstra algorithm (no negative weights).
As long as you can think of how to construct the algorithm. If your own algorithm, but do not know how to solve problems, it is useless. Here I would like to say to you,
In the process of doing the problem later, do not just look at other people's code, to see the thought, why others write. Then write the code according to the thought of your imagination and write the process without
Look at someone else's code. Even if it doesn't matter, this impression is the deepest, and later on the tap,
Specific or code inside see:
#include <stdio.h> #include <string.h> #include <queue>using namespace std;struct node{int pos,t; friend bool operator< (node A,node b) {return a.t>b.t;}}; Priority_queue<node>s;int lift[205],vis[205],n;int Dijkstra (int st,int ed) {Node Temp,temp1;int flag=0;temp.pos =st,temp.t=0;s.push (temp), while (!s.empty ()) {temp1=temp=s.top (), S.pop (), vis[temp.pos]=1;if (temp.pos==ed) {flag=1 ; break;} Temp.pos=temp1.pos-lift[temp1.pos];//temp.t=temp1.t+1;if (temp.pos>=1&&temp.pos<=n&&!vis[ Temp.pos]) S.push (temp); Temp.pos=temp1.pos+lift[temp1.pos];temp.t=temp1.t+1;if (Temp.pos>=1&&temp.pos <=n&&!vis[temp.pos]) S.push (temp);//the same place as the previous code. It was either a matrix or a tree, and now it's in two directions. Judging the elevator
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu1584 A Strange Lift (Elevator shortest path problem)