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 ...
随机推荐
- Codefroces 213E. Two Permutations
E. Two Permutations time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 851D Arpa and a list of numbers
D. Arpa and a list of numbers time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 关于Ajax提交后无法刷新iframe页面的问题
原因及解决方法: 要把刷新代码写进ajax的代码块里面,而不是放在后面
- C# 详解反射
原博客:http://www.cnblogs.com/Stephenchao/p/4481995.html 两个现实中的例子: 1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的 ...
- Vue 实现展开折叠效果
Vue 实现展开折叠效果 效果参见:https://segmentfault.com/q/1010000011359250/a-1020000011360185 上述链接中,大佬给除了解决方法,再次进 ...
- Linux进程管理(一、 基本概念和数据结构)
被问到两个问题, 后来想了下如果要讲明白还不太容易,需要对进程的概念,进程管理有清晰的认识: 1. 父进程打开了一个文件,然后通过fork创建一个子进程, 子进程是否共享父进程的文件描述符? 2. 在 ...
- Kubernetes1.4即将发布
(一)发布历史 Kubernetes 1.0 - 2015年7月发布 Kubernetes 1.1 - 2015年11月发布 Kubernetes 1.2 - 2016年3月发布 Kubernetes ...
- hdu1527 威佐夫博奕
有2堆石子,有2个人,每个人可以从一堆取或从2堆取一样的个数的石子,至少取1个.问先手的是胜或输.设(ak,bk)我么成为局势. (0,0)(1,2)(3,5)(4,7)..这种先手必输的叫奇异局势. ...
- oracle函数的分类
v 单行函数:对每一行输入值进行计算,得到相应的计算结果,返回给用户,也就是说,每行作为一个输入参数,经过函数计算得到每行的计算结果. 比如select length(ename) from emp ...
- 重磅课程|《CNCF x Alibaba 云原生技术公开课》正式开讲!
到底什么是“云原生”?云原生与 CNCF.Kubernetes 是什么关系?作为云计算时代的开发者和从业者,我们该如何在“云原生”的技术浪潮中站稳脚跟,将云原生落地.实现个人的自我升级呢? 201 ...