HDU 1548 A strange lift(构造+最短路Dijkstra)
2016-07-14 16:23
260 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1548
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20811 Accepted Submission(s): 7601
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”?
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.
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 5
3 3 1 2 5
0
Sample Output
3
之前用搜索写了一下,搞掉了,感觉还可以用最短路来做,所以就试了一下。
其实很简单,把一维的模型构造为二维的就好了,这样就可以DJ了,下面是AC代码:
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20811 Accepted Submission(s): 7601
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”?
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.
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 5
3 3 1 2 5
0
Sample Output
3
之前用搜索写了一下,搞掉了,感觉还可以用最短路来做,所以就试了一下。
其实很简单,把一维的模型构造为二维的就好了,这样就可以DJ了,下面是AC代码:
#include<cstdio> #include<cstring> #include<algorithm> #define INF 0x3fffffff using namespace std; int e[205][205],n,st,en,a[205],dis[205],book[205]; void init() { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(i==j) { e[i][j]=0; } else e[i][j]=INF; } } } int main() { while(~scanf("%d",&n)) { if(n==0) { break; } init(); scanf("%d%d",&st,&en); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); } for(int i=1; i<=n; i++) { if(a[i]!=0) { if(i+a[i]<=n) { e[i][i+a[i]]=1; } if(i-a[i]>=1) { e[i][i-a[i]]=1; } } } memset(book,0,sizeof(book)); dis[st]=0; for(int i=1; i<=n; i++) { dis[i]=e[st][i]; } int u; for(int i=1; i<=n-1; i++) { int minn=INF; for(int j=1; j<=n; j++) { if(book[j]==0&&dis[j]<minn) { minn=dis[j]; u=j; } } book[u]=1; for(int v=1; v<=n; v++) { if(e[u][v]<INF) { if(dis[v]>e[u][v]+dis[u]) { dis[v]=e[u][v]+dis[u]; } } } } if(dis[en]==INF) { printf("-1\n"); } else { printf("%d\n",dis[en]); } } return 0; }
相关文章推荐
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- HDU 1016 Prime Ring Problem
- HDU 1017 A Mathematical Curiosity