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. 学习Spark2.0中的Structured Streaming(一)

    转载自:http://lxw1234.com/archives/2016/10/772.htm Spark2.0新增了Structured Streaming,它是基于SparkSQL构建的可扩展和容 ...

  2. 实习培训——Servlet(7)

    实习培训——Servlet(7) 1  Servlet 异常处理 当一个 Servlet 抛出一个异常时,Web 容器在使用了 exception-type 元素的 web.xml 中搜索与抛出异常类 ...

  3. HTTP 教程

    HTTP 简介 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准. HTTP是一个基于 ...

  4. ComBSTR的使用

    用 CComBSTR 进行编程 Visual Studio .NET 2003   3(共 3)对本文的评价是有帮助 - 评价此主题   ATL 类 CComBSTR 提供对 BSTR 数据类型的包装 ...

  5. Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class

    转自http://www.anylogic.com/anylogic/help/index.jsp?topic=/com.xj.anylogic.help/html/code/Arrays_Colle ...

  6. VS2013发布网站详细步骤

    以下是我发布网站的时候,搜索到的可以使用的办法,同样适用于vs2013(已经尝试). 1.打开你的VS2012网站项目,右键点击项目>菜单中 重新生成一下网站项目:再次点击右键>发布: V ...

  7. Javascript-涨工资案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. C# 查看所有的隐藏文件

    方法1 通过 位与 static void Main(string[] Args)        { //假设扫描C:\Test中—— string[] hiddenfiles = Directory ...

  9. STA分析(三) cmos模型

    CMOS集成电路的基本结构是以P型材料作为衬底(p-substrate),直接生成NMOS, 同时增加N肼(n-well),在其上制造PMOS. 增加两个bulk(P+,N+)防止非MOS管内的PN结 ...

  10. mongodb权限

    1.在无密码模式下添加账号 db.createUser( { user: "user", pwd: "pwd", roles: [ { role: " ...