1007 最短路
来源:互联网 发布:map生成json 编辑:程序博客网 时间:2024/06/05 12:32
A strange lift
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 8
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
题意:神奇的电梯 只能按上或者按下 第二行输入的是每次向上或者向下的楼层、、、、
没想出来的题,后来看了代码将每次上升或者下降的步长计数为1 然后求最短路
代码:
#include<iostream>#include<cstring>using namespace std;const int manx=99999;bool vis[manx];int map[300][300];int dir[300];int n;void dijksta(int v){ int u; int temp; // cout<<v<<" 12312313"<<endl; memset(vis,0,sizeof(vis)); for (int i=1;i<=n;i++) { dir[i]=map[v][i]; } vis[v]=1; dir[v]=0; for (int i=1;i<=n;i++) { temp=manx; for (int j=1;j<=n;j++) { if (!vis[j]&&dir[j]<temp) { temp=dir[j]; u=j; } } vis[u]=1; for (int j=1;j<=n;j++) { if (!vis[j]&&dir[j]>dir[u]+map[u][j]) dir[j]=dir[u]+map[u][j]; } }}int main(){int k;int a,b;while(cin>>n){if(n==0) break; cin>>a>>b;for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { map[i][j]=map[j][i]=manx; } for(int i=1;i<=n;i++) { cin>>k; if((i+k)<=n) map[i][i+k]=1; if((i-k)>=1) map[i][i-k]=1; }/* for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cout<<map[i][j]<<" "; } cout<<endl; }*/ dijksta(a); if(dir[b]==manx) cout<<"-1"<<endl; else// cout<<b<<endl; cout<<dir[b]<<endl;} return 0;}
- 最短路 1007
- 最短路—1007
- 1007 最短路
- 最短路 & 次短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- C#贪吃蛇游戏之--游戏信息存取(四)
- Google需要从Android得到什么?
- VMWare中ubuntu打开终端方法
- C#贪吃蛇游戏之--界面层(五)
- Uva 10534 波浪子序列(二次快速LIS)
- 1007 最短路
- 界面编程之基本界面组件(7)ImageView(图像视图)
- 搭建 struts2 架构
- URAL 1018 Binary Apple Tree
- 深入分析Java对象的建构顺序
- dwz+thinkphp 我的视图测试
- 普通 56K猫拔号上网工具 wvdial 介绍
- Matrix详解
- VS2010快捷键