HDU 1548 A Strange Lift "good short circuit, SPFA"

Source: Internet
Author: User


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


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
original title link: http://acm.hdu.edu.cn/showproblem.php?pid=1548

Main topic:

n represents the number of stairs, a for the starting position, b for the terminating position, and then the number of N, after the arrival of each layer, you can choose to press the button "up", "down", on behalf of the elevator can rise or fall after the number of layers (note: Is the change of layer, and legal), ask you at least How many times do I need to press the button?

Ideas:

The problem seems a little complicated, but it's easier to transform it. With each layer and its reachable floors, the graph is built and the weight is set to 1, and the problem can be converted to the shortest path from the starting point to the end point.

I use Dijkstra algorithm WA, have negative right? Some people use BFS to solve this problem.

Sofa Algorithm AC code:

#include <iostream> #include <cstdio> #include <queue>using namespace std;const int inf=0x3f3f3f3f;        int a[205][205];int dis[205];bool vis[205];int s,t,n;void SPFA () {for (int i=1;i<=n;i++) {dis[i]=inf;    Vis[i]=false;    } dis[s]=0;    Vis[s]=true;    queue<int>q;    Q.push (s);        while (!q.empty ()) {int P=q.front ();        Q.pop ();        Vis[p]=true; for (int i=1;i<=n;i++) {if (Dis[i]>dis[p]+a[p][i]) {dis[i]=dis[p]+a[p][i                ];                    if (!vis[i]) {vis[i]=true;                Q.push (i);        }}}}}int main () {while (cin>>n,n) {cin>>s>>t;                for (int i=1, i<=n; i++) {for (int j=1; j<=n; J + +) if (i==j) a[i][j]=0;        else A[i][j]=inf;        } int x; for (int i=1;i<=n;i++) {scanf ("%d", &AMP;X);            if (i-x>=1) a[i][i-x]=1;        if (i+x<=n) a[i][i+x]=1;        } SPFA ();        if (dis[t]>=inf) cout<< "-1" <<endl;    else cout<<dis[t]<<endl; } return 0;}



HDU 1548 A Strange Lift "good short circuit, SPFA"

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.