hdu 1548 A strange lift(迪杰斯特拉,邻接表)
2016-04-07 21:14
267 查看
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18723 Accepted Submission(s):
6926
[align=left]Problem Description[/align]
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"?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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".
[align=left]Sample Input[/align]
5 1 5
3 3 1 2 5
0
[align=left]Sample Output[/align]
3
[align=left]Recommend[/align]
8600 | We have carefully selected several similar
problems for you: 1385 1142 1372 1072 1690
最短路的模板题,代码还是很好理解的。
题意:一个特别的电梯,按up可升上k[i]层,到大i+k[i]层,down则到达i-k[i]层,最高不能超过n,最低不能小于1,给你一个起点和终点,问最少可以按几次到达目的地。在一个N层高的楼有一个奇怪的电梯,在每一层只能上升或下降一个特定的层数,中间不会停止,在给定的条件下,问能不能到达指定楼层,可以到达的话返回转操作次数,不可以的话返回-1.
附上代码:
#include <iostream> #include <cstdio> #include <cstring> #define M 205 #define MAX 0x3f3f3f3f using namespace std; int map[M][M],vis[M],dis[M]; int main() { int n,a,b,i,j,s; while(~scanf("%d",&n)&&n) { scanf("%d%d",&a,&b); memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); for(i=1; i<=n; i++) for(j=1; j<=n; j++) { if(i==j) map[i][j]=0; //同一个地方距离为0 else map[i][j]=MAX; } for(i=1; i<=n; i++) { scanf("%d",&s); if(i+s<=n) //标记这一层电梯可以去的楼层 map[i][s+i]=1; if(i-s>=1) map[i][i-s]=1; } vis[a]=1; //起点已走过 for(i=1; i<=n; i++) dis[i]=map[a][i]; //初始化距离为每个点到起点的距离 int min,k,t; for(i=1; i<=n; i++) { min=MAX; for(j=1; j<=n; j++) if(!vis[j]&&dis[j]<min) //每次都找离终点最近的点 { min=dis[j]; t=j; } vis[t]=1; //标记为已经找过此点 for(j=1; j<=n; j++) if(!vis[j]&&map[t][j]<MAX) //从最近的点到下一个点的距离与初始距离进行比较 if(dis[j]>dis[t]+map[t][j]) dis[j]=dis[t]+map[t][j]; } if(dis[b]<MAX) printf("%d\n",dis[b]); else //不能到 则输出-1 printf("-1\n"); } return 0; }
邻接表代码:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define inf 0x3f3f3f3f using namespace std; struct Edge { int from,to,val,next; }edge[40500]; int tol,s,t,n; int dis[205]; bool vis[205]; int head[40500]; void init() { tol=0; memset(head,-1,sizeof(head)); } void addEdge(int u,int v) { edge[tol].from=u; edge[tol].to=v; edge[tol].val=1; edge[tol].next=head[u]; head[u]=tol++; } void getmap() { int x; for(int i=1;i<=n;i++) { scanf("%d",&x); if(i-x>=1) addEdge(i,i-x); if(i+x<=n) addEdge(i,i+x); } memset(vis,false,sizeof(vis)); memset(dis,inf,sizeof(dis)); } void spfa() { queue<int>q; q.push(s); vis[s]=true; dis[s]=0; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(dis[v]>dis[u]+edge[i].val) { dis[v]=dis[u]+edge[i].val; if(!vis[v]) { vis[v]=true; q.push(v); } } } } if(dis[t]<inf) printf("%d\n",dis[t]); else printf("-1\n"); return; } int main() { int i,j; while(~scanf("%d",&n)&&n) { scanf("%d%d",&s,&t); init(); getmap(); spfa(); } }
相关文章推荐
- 4.0-ansible playbook handlers
- 树形dp-CodeForces 581F
- 第五届计算机职业资格认证考试CCF (第一题)
- Fliter2D
- POJ 1730 Perfect Pth Powers(素数筛选法)
- 【SSM-MyBatis框架】MyBatis入门
- Aliyun(阿里云)搭建WordPress
- Java_SE08-文本数据IO操作,异常处理
- 第六周博客技术发表 C语言代码
- Ubuntu 12.04下C语言连接Mysql数据库实现增删改查
- 机器学习技法-GBDT算法
- 属性选择器
- Linux系统中头文件的使用
- 3.9-ansible playbook判断
- Atitit.web ui 组件化 vs mvc
- 选择排序
- Jquery学习第一步
- 3.8-ansible playbook循环
- 拉普拉斯算子
- 杭电5540 Secrete Master Plan