hdu 1548 A strange lift(迪杰斯特拉,邻接表)
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
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"?
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.
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".
题意:一个特别的电梯,按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,,sizeof(vis));
memset(dis,,sizeof(dis));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
if(i==j)
map[i][j]=; //同一个地方距离为0
else
map[i][j]=MAX;
}
for(i=; i<=n; i++)
{
scanf("%d",&s);
if(i+s<=n) //标记这一层电梯可以去的楼层
map[i][s+i]=;
if(i-s>=)
map[i][i-s]=;
}
vis[a]=; //起点已走过
for(i=; i<=n; i++)
dis[i]=map[a][i]; //初始化距离为每个点到起点的距离
int min,k,t;
for(i=; i<=n; i++)
{
min=MAX;
for(j=; j<=n; j++)
if(!vis[j]&&dis[j]<min) //每次都找离终点最近的点
{
min=dis[j];
t=j;
}
vis[t]=; //标记为已经找过此点
for(j=; 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 ;
}
邻接表代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
int from,to,val,next;
}edge[];
int tol,s,t,n;
int dis[];
bool vis[];
int head[]; void init()
{
tol=;
memset(head,-,sizeof(head));
} void addEdge(int u,int v)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].val=;
edge[tol].next=head[u];
head[u]=tol++;
} void getmap()
{
int x;
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(i-x>=) 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]=;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-;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();
}
}
hdu 1548 A strange lift(迪杰斯特拉,邻接表)的更多相关文章
- HDU 1548 A strange lift (bfs / 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 3339 In Action(迪杰斯特拉+01背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- HDU 2544最短路 (迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Me ...
- HDU 1548 A strange lift (Dijkstra)
https://vjudge.net/problem/HDU-1548 题意: 电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点 ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- HDU 1548 A strange lift 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- Leetcode48. Rotate Image旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- 七.RBM受限玻尔兹曼机
1.受限玻尔兹曼机 玻尔兹曼机是一大类的神经网络模型,但是在实际应用中使用最多的则是受限玻尔兹曼机(RBM). 受限玻尔兹曼机(RBM)是一个随机神经网络(即当网络的神经元节点被激活时会有随机行为 ...
- mysql错误日志目录
在windows下,一般是mysql安装目录下的data目录下 ,扩展名是.err的文件.
- Ubuntu 12.04 安装 IQQ
1. 安装 IQQ 首先应安装jdk包 2. 百度网盘下载: http://pan.baidu.com/share/home?uk=3071047022 3. 运行 (1) Linux用户给IQQ-1 ...
- 记CRenderTarget:DrawText()绘制中文乱码的BUG及解决办法
原文:记CRenderTarget:DrawText()绘制中文乱码的BUG及解决办法 转载请注明出处:http://www.cnblogs.com/Ray1024 一.问题描述 在MFC中使用Dir ...
- NOIP模拟 17.8.17
NOIP模拟17.8.17 A 小 G 的字符串文件名 输入文件 输出文件 时间限制 空间限制str.pas/c/cpp str.in str.out 1s 128MB[题目描述]有一天,小 L 给小 ...
- 2019-8-31-C#-循环的判断会进来几次
title author date CreateTime categories C# 循环的判断会进来几次 lindexi 2019-08-31 16:55:58 +0800 2018-06-14 0 ...
- 好玩又实用,阿里巴巴开源混沌工程工具 ChaosBlade
减少故障的最好方法就是让问题经常性的发生.在可控范围或环境下,通过不断重复失败过程,持续提升系统的容错和弹性能力. 那么,实施一次高效的混沌工程实验,需要几步呢? 答案:2 步. ① 登陆 Chaos ...
- 【JZOJ4845】【NOIP2016提高A组集训第5场11.2】寻找
题目描述 "我有个愿望,我希望穿越一切找到你." 这是个二维平面世界,平面上有n个特殊的果实,我从(0,0)点出发,希望得到尽量多的果实,但是出于某种特殊的原因,我的运动方式只有三 ...
- WebSocket简述
WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在 W ...