https://vjudge.net/problem/HDU-1548

题意:

电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点,要求出最少要按几次键。

思路:

可以用BFS,也可以用迪杰斯特拉算法。

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; #define INF 100000000 int n, A, B;
int map[][];
int vis[];
int d[];
int num[]; void Dijkstra()
{
memset(vis, , sizeof(vis));
for (int i = ; i <= n; i++)
{
num[i] = map[A][i];
}
num[A] = ;
vis[A] = ;
for (int i = ; i < n; i++)
{
int min = INF;
int pos;
for (int j = ; j <= n; j++)
{
if (num[j] < min && !vis[j])
{
pos = j;
min = num[j];
}
}
if (min == INF) break;
vis[pos] = ;
for (int j = ; j <= n; j++)
{
if (num[pos] + map[pos][j] < num[j] && !vis[j])
num[j] = num[pos] + map[pos][j];
}
}
if (num[B] == INF) printf("-1\n");
else printf("%d\n", num[B]);
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int a;
while (scanf("%d", &n) && n)
{
scanf("%d%d", &A, &B);
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
map[i][j] = INF;
for (int i = ; i <= n; i++)
{
scanf("%d", &a);
if (i + a <= n)
map[i][i + a] = ;
if (i - a >= )
map[i][i - a] = ;
}
Dijkstra();
}
return ;
}

HDU 1548 A strange lift (Dijkstra)的更多相关文章

  1. HDU 1548 A strange lift(BFS)

    Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...

  2. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  3. 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 ...

  4. HDU 1548 A strange lift (广搜)

    题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...

  5. hdu 1548 A strange lift(迪杰斯特拉,邻接表)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. HDU 1548 A strange lift(dij+邻接矩阵)

    ( ̄▽ ̄)" //dijkstra算法, //只是有效边(即能从i楼到j楼)的边权都为1(代表次数1): //关于能否到达目标楼层b,只需判断最终lowtime[b]是否等于INF即可. # ...

  7. 杭电 1548 A strange lift(广搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Others) ...

  8. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

  9. HDU 1548 A strange lift (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

随机推荐

  1. orm之路由层

    一.简单配置 1.参数 第一个参数是正则表达式(如果要精准匹配:‘^publish/$’),或者加斜杠('^publish/') 第二个参数是视图函数(不要加括号) urlpatterns = [ u ...

  2. 深入理解Fabric环境搭建的详细过程(转)

    前面的准备工作我就不用多说了,也就是各种软件和开发环境的安装,安装好以后,我们git clone下来最新的代码,并切换到v1.0.0,并且下载好我们需要使用的docker镜像,也就是到步骤6,接下来我 ...

  3. pem转pfx

    openssl req -new -key privkey.pem -out root.csr openssl x509 -req -days -sha1 -extensions v3_ca -sig ...

  4. [LeetCode] 675. Cut Off Trees for Golf Event_Hard tag: BFS

    You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...

  5. 在Keras模型中one-hot编码,Embedding层,使用预训练的词向量/处理图片

    最近看了吴恩达老师的深度学习课程,又看了python深度学习这本书,对深度学习有了大概的了解,但是在实战的时候, 还是会有一些细枝末节没有完全弄懂,这篇文章就用来总结一下用keras实现深度学习算法的 ...

  6. Winsock网络编程

    Winsock是Windows下网络编程的标准接口.使用Winsock编程的步骤一般是比较固定的. 首先要包含头文件#include <WinSock2.h>,同时要添加WS2_32.li ...

  7. STL学习笔记--特殊容器

    容器配接器 (1) stack 栈 后进先出(LIFO), 头文件#include<stack> template<class _Ty, class _Container = deq ...

  8. Hive 常用函数汇总

    Hive内部提供了很多函数给开发者使用,包括数学函数,类型转换函数,条件函数,字符函数,聚合函数,表生成函数等等,这些函数都统称为内置函数. 目录 数学函数 集合函数 类型转换函数 日期函数 条件函数 ...

  9. Linux基础命令---ln

    ln 为指定的目录或者文件创建链接,如果没有指定链接名,那么会创建一个和源文件名字一样的链接. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fed ...

  10. python3.4学习笔记(十) 常用操作符,条件分支和循环实例

    python3.4学习笔记(十) 常用操作符,条件分支和循环实例 #Pyhon常用操作符 c = d = 10 d /= 8 #3.x真正的除法 print(d) #1.25 c //= 8 #用两个 ...