A strange lift (关键构图,电梯升降)
2013-01-21 21:22
155 查看
点击打开链接
1.题目:
二、代码:
1.题目:
A strange lift Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 15 Accepted Submission(s) : 4 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 53 3 1 2 50 Sample Output 3
二、代码:
#include<stdio.h> #include<string.h> #define max 0x7fffffff int dist[205]; int map[205][205]; int visit[205]; void dijkstra(int v,int n) { memset(dist,-1,sizeof(dist)); for(int i=1; i<=n; i++) { dist[i]=map[v][i]; visit[i]=0; } visit[v]=1; dist[v]=0; for(int j=0; j<n; j++) { int min=max; int u; for(int i=1; i<=n; i++) { if(dist[i]<min&&visit[i]==0) { min=dist[i]; u=i; } } visit[u]=1; for(int i=1; i<=n; i++) { if(map[u][i]!=max&&visit[i]==0) { int newdist=dist[u]+map[u][i]; if(newdist<dist[i]) { dist[i]=newdist; } } } } } int main() { int n,s,e,tmp; while(scanf("%d",&n)!=EOF) { if(n==0) break; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) map[i][j]=max; } scanf("%d%d",&s,&e); for(int i=1; i<=n; i++) { scanf("%d",&tmp); if(i-tmp>0) map[i][i-tmp]=1; if(i+tmp<=n) map[i][i+tmp]=1; } dijkstra(s,n); if(dist[e]!=max) printf("%d\n",dist[e]); else printf("-1\n"); } return 0; } /* 5 1 5 3 3 1 2 5 0 */
相关文章推荐
- HDU 1548 A strange lift
- Catch That Cow && A strange lift
- A strange lift宽度搜索优先队列
- HDU 1548 A strange lift 奇怪的电梯(BFS,水)
- poj1149-------网络流的题目,关键是构图,好题!!!
- 1020:电梯升降
- WPF界面设计技巧(7)—模拟电梯升降的缓动动画
- 电梯升降
- 1020:电梯升降
- SDJZU 电梯升降
- hdu1584 A strange lift (电梯最短路径问题)
- 电梯升降
- 【Codeforces Round 274 (Div 2)E】【DP 成段更新 打标记法 滚动数组】Riding in a Lift 乘坐k次电梯避免到达b层的方案数
- poj 3281 最大流 拆点构图,关键在于构图
- hdu1584 A strange lift (电梯最短路径问题)
- poj 3020(匈牙利算法的简单应用,关键是构图)
- 电梯(lift)
- 浙江大学ACM俱乐部 1020:电梯升降
- WPF界面设计技巧(7)—模拟电梯升降的缓动动画
- 1020:电梯升降