HDU 1548 A Strange lift DIJKSTRA+SPFA algorithm AC

Source: Internet
Author: User

A Strange LiftTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 13777 Accepted Submission (s): 5254


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
Simple question. SPFA Code:
#include <cstdio> #include <deque> #define MAX #define INF 1000000000using namespace std; int Graph[max][ma X]; int T[max]; bool Visited[max]; int SPFA (int s, int d, int n) {int Dis[max], c[max];for (int i = 1; I <= n; ++i) { Dis[i] = INF; C[i] = 0; Visited[i] = false;}  Dis[s] = 0;d eque<int> que; Que.push_front (s); while (!que.empty ()) {int k = Que.front (); Que.pop_front (); Visited[k] = false; c[k]++; if (c[k]>n) {return-1;} for (int i = 1; I <= n; ++i) {if (Dis[i]>dis[k]+graph[k][i]) {dis[i] = Dis[k]+graph[k][i]; Visited[i] = true; if (!que. Empty ()) {if (Que.front () <=dis[i]) que.push_back (i); Elseque.push_front (i);} Else{que.push_front (i);}}} return dis[d];}  int main () {int n, a, B, while (~SCANF ("%d", &n) && N) {scanf ("%d%d", &a,&b); for (int i = 1; I <= N; ++i) {scanf ("%d", &t[i]);} for (int i = 0, I <= N; ++i) {for (int j = 0; J <= N; ++j) {graph[i][j] = INF;}} for (int i = 1; I <= n; ++i) {if (i+t[i]<=n) {Graph[i][i+t[i]] = 1;} if (i-t[i]>0) {Graph[i][i-t[i]] = 1;}} if (b<1|b>n) {puts ("-1"); continue;} int ans = SPFA (a,b,n), if (ans >= INF) puts ("1"); elseprintf ("%d\n", ans);} return 0;}

Dijkstra Code:
#include <stdio.h> #define MAX #define INF 1000000000int Graph[max][max]; int T[max], Dis[max];bool Visited[max] void Dijkstra (int s, int n) {for (int i = 1; I <= n; ++i) {dis[i] = Graph[s][i]; Visited[i] = false;} Dis[s] = 0;//forget to write this line of code. Let me wrong into sb. Visited[s] = true; for (int i = 1; i < n; ++i) {int index =-1, min = inf;for (int j = 1; j <= N; ++j) {if (!visited[j] && dis[j]<min) {index = j; min = Dis[j];}} if (index = =-1) {return;} Visited[index] = true; for (int j = 1; j <= N; ++j) {if (!visited[j] && dis[j]>dis[index]+graph[index][j]) {di S[J] = Dis[index]+graph[index][j];}}}  int main () {int n, a, B, while (~SCANF ("%d", &n) && N) {scanf ("%d%d", &a,&b); for (int i = 1; I <= N; ++i) {scanf ("%d", &t[i]);} for (int i = 0, I <= N; ++i) {for (int j = 0; J <= N; ++j) {graph[i][j] = INF;}} for (int i = 1; I <= n; ++i) {if (i+t[i]<=n) {Graph[i][i+t[i]] = 1;} if (i-t[i]>0) {Graph[i][i-t[i]] = 1;}} if (b<1|b>n) {pUTS ("-1"); continue;} Dijkstra (A,n), if (Dis[b] >= INF) puts ("1"); elseprintf ("%d\n", Dis[b]);} return 0;}

with June

HDU 1548 A Strange lift DIJKSTRA+SPFA algorithm AC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.