hdu 1548 A strange lift(Dijkstra+合理转换)
来源:互联网 发布:阿里云cdn加速 编辑:程序博客网 时间:2024/06/07 16:22
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"?
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.
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 53 3 1 2 50
Sample Output
3#include <iostream>#include <memory.h>#include <stdio.h>#define NUM 205#define INF 0x3f3f3f3fusing namespace std;int n;int g[NUM][NUM];int dis[NUM][NUM];int vis[NUM];int lowcost[NUM];int pre[NUM];void Dijkstra(int beg){ for(int i=1; i<=n; i++) { lowcost[i]=INF; vis[i]=false; pre[i]=-1; } lowcost[beg]=0; for(int j=1; j<=n; j++) { int k=-1; int Min=INF; for(int i=1; i<=n; i++) { if(!vis[i] && lowcost[i]<Min) { Min=lowcost[i]; k=i; } } if(k==-1)break; vis[k]=true; for(int i=1; i<=n; i++) { if(!vis[i] && lowcost[k]+g[k][i]<lowcost[i]) { lowcost[i]=lowcost[k]+g[k][i]; pre[i]=k; } } }}int k[NUM];int main(){ int a,b; while(cin>>n) { if(n==0) break; cin>>a>>b; for(int i=1;i<=n;i++) { cin>>k[i]; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { g[i][j]=INF; } g[i][i]=0; } for(int i=1;i<=n;i++) { if(i+k[i]<=n) g[i][i+k[i]]=1; //巧妙赋值 if(i-k[i]>=1) g[i][i-k[i]]=1; } Dijkstra(a); if(lowcost[b]==INF) cout<<"-1"<<endl; else cout<<lowcost[b]<<endl; } return 0;}
0 0
- hdu 1548 A strange lift(Dijkstra+合理转换)
- 【BFS/Dijkstra】hdu 1548 A Strange Lift
- hdu 1548 A strange lift (BFS、Dijkstra)
- 【Dijkstra】-HDU-1548-A strange lift
- HDU 1548 A strange lift(Dijkstra)
- hdu 1548 A strange lift (dijkstra算法)
- HDU - 1548 A strange lift(Dijkstra)
- HDU 1548 A strange lift (Dijkstra算法)
- hdu 1548-A strange lift-最短路-dijkstra
- [HDU 1548]A Strange Lift[Dijkstra最短路]
- hdu 1548 A strange lift Dijkstra+SPFA算法AC
- HDU 1548 A strange lift(Dijkstra、BFS、DP)
- HDU 1548 A strange lift(构造+最短路Dijkstra)
- A strange lift hdu 1548
- HDU 1548 A strange lift
- HDU 1548 A strange lift
- HDU 1548 A strange lift
- Hdu-1548 A strange lift
- Spring 事务机制详解
- debian ftp(vsftpd)服务器搭建
- 知乎 你有什么相见恨晚的知识想推荐给年轻人?-2
- Webrtc Jitter Buffer
- How to Change Login User in SharePoint2013?
- hdu 1548 A strange lift(Dijkstra+合理转换)
- 程序员需要知道的各类官方文档地址
- UIWebView无法用loadRequest加载页面 错误提示:App Transport Security has blocked a cleartext HTTP ...
- iOS学习笔记——文件操作(NSFileManager)
- 通过url读取XML
- Oracle12c中性能优化&功能增强新特性之临时undo
- css笔记重点
- Java的版本、特性和历史
- PackageManagerService(Android5.1)深入分析(一)构造函数