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. 001-Spring Cloud Edgware.SR3 升级最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升级2.0.4.RELEASE注意问题点

    一.前提 升级前 => 升级后 Spring Boot 1.5.x => Spring Boot 2.0.4.RELEASE Spring Cloud Edgware SR3 => ...

  2. PHP开发接口使用RSA进行加密解密方法

    网络安全问题很重要,尤其是保证数据安全,遇到很多在写接口的程序员直接都是明文数据传输,在我看来这是很不专业的.本人提倡经过接口的数据都要进行加密解密之后进行使用. 这篇文章主要介绍使用PHP开发接口, ...

  3. OCR学习及tesseract的一些测试

    最近接触OCR,先收集一些资料,包括成熟软件.SDK.流行算法. 1. 一个对现有OCR软件及SDK的总结,比较全面,包括支持平台.编程语言.支持字体语言.输出格式.相关链接等 http://en.w ...

  4. Selenium - Xpath 使用方法

    由于最新版火狐不在支持FireBug等开发工具,可以通过https://ftp.mozilla.org/pub/firefox/releases/下载49版本以下的火狐就可以增加Firebug等扩展了 ...

  5. PKU2503_map应用

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  6. 74. Search a 2D Matrix(二分查找,剑指offer 1)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. vb6.0的安装

    vb6.0古老的编程软件

  8. cf249D

    这题说的是给了一个n*m的网格,然后每个格子的点事黑色的或者是白色的然后每个点如图所示    然后只能用白点和图中给出的边建立三角形然后询问三角形的个数有多少个,这样说每个三角形的边必须是图中有的边, ...

  9. Linux服务器配置---安装vsftpd

    安装vsftpd 大多数Linux系统都使用vsftpd,因此这里我们也安装vsftpd 1.安装vsftpd [root@localhost phpMyAdmin]# yum install -y ...

  10. 线程属性pthread_attr_t简介

    本文编辑整理自: http://hi.baidu.com/7828058/blog/item/256e16decd1a385e94ee3784.html http://www.ibm.com/deve ...