HDU 1548 A strange lift

来源:互联网 发布:网络连接线的接头 编辑:程序博客网 时间:2024/06/06 04:58

Problem Description

There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0KiN) 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 iKi 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(1N,A,B200) 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 53 3 1 2 50

Sample Output

3

Recommend

8600


题目大意

一幢大楼有 n 层,有个电梯,第 i 层的电梯能让人上升 k[i] 或者下降 k[i],但不能超过 n 或者低于 1
求至少按几次电梯,才能从 a 走到 b

多组数据以一个单独的0结束,无解输出-1

solution

  • 按照题目中的描述建图,然后跑最短路就好了

  • n 很小,Floyd和dijkstra应该都可以,但我好长时间没写spfa了,于是就写的spfa…

  • 因为边权都是1,bfs应该也可以.

code

#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;template<typename T>void input(T &x) {    x=0; T a=1;    register char c=getchar();    for(;c<'0'||c>'9';c=getchar())        if(c=='-') a=-1;    for(;c>='0'&&c<='9';c=getchar())        x=x*10+c-'0';    x*=a;    return;}#define MAXN 210#define MAXM 510struct Edge {    int u,v,w,next;    Edge(int u=0,int v=0,int w=0,int next=0):        u(u),v(v),w(w),next(next) {}};Edge edge[MAXM];int head[MAXN],cnt;void addedge(int u,int v,int w) {    edge[++cnt]=Edge(u,v,w,head[u]);    head[u]=cnt;    return;}#define inf 2147483647int n,m;int dis[MAXN];bool inq[MAXN];queue<int> q;int spfa(int s,int t) {    dis[s]=0;    q.push(s);    inq[s]=true;    while(!q.empty()) {        int u=q.front();        q.pop();        inq[u]=false;        for(int i=head[u];i;i=edge[i].next) {            int v=edge[i].v;            if(dis[u]+edge[i].w<dis[v]) {                dis[v]=dis[u]+edge[i].w;                if(!inq[v]) {                    q.push(v);                    inq[v]=true;                }            }        }    }    return dis[t];}int main() {    int A,B;    while(scanf("%d",&n)==1&&n!=0) {        for(int i=1;i<=n;i++)            dis[i]=inf,head[i]=0;        cnt=0;        input(A),input(B);        for(int i=1;i<=n;i++) {            int k;            input(k);            if(i-k>=1) addedge(i,i-k,1);            if(i+k<=n) addedge(i,i+k,1);        }        int ans=spfa(A,B);        if(ans==inf) ans=-1;        printf("%d\n",ans);    }    return 0;}
原创粉丝点击