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. modelform和modelserializer

    modelform modelform比form强悍很多

  2. [py]python面向对象的str getattr特殊方法

    本文旨在说清楚 类中的 def init def str def getattr 这三个方法怎么用的. 定制输入实例名时输出内容 def __str__会定制输出实例名时候的输出 class Chai ...

  3. phpmyadmin-配合nginx与php安装

    1. 概况 phpMyAdmin是用来在网页端图形化操作MySQL数据库的工具,使用起来非常直观,目前最新版本是4.8.3.在搭建web集群架构时可能有这样的需求,数据库安装在专门的一台机器上,但是希 ...

  4. winform dataGridView DataGridViewComboBoxColumn 下拉框事件代码

    有一个dataGridView ,有一列是DataGridViewComboBoxColumn .我用动态绑定,在绑定数据的时候.我们也给这一列绑定数据 在dataGridView的RowsAdded ...

  5. 自实现jQuery版分页插件

    本篇博客的分页插件是在2017-11-10 的一篇博客的基础上改造的(原博客地址:原生js版分页插件),主要是优化了分页按钮的排列和显示样式,取消首页和末页的箭头按钮,改为数字按钮,并始终把它们分别固 ...

  6. rpm服务的独立服务管理

    /etc/init.d  启动脚本的位置 /etc/sysconfig/ 初始化环境配置文件 /etc/   配置文件位置 /etc/xinetd.conf  xinetd配置文件 /etc/xine ...

  7. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  8. React组件,React和生命周期

    笔记,具体可以看看这个博客: https://segmentfault.com/a/1190000004168886?utm_source=tag-newest react 的jsx document ...

  9. 25最短路径之Dijkstra算法

    图的最优化问题:最小生成树.最短路径 典型的图应用问题 无向连通加权图的最小生成树 有向/无向加权图的最短路径 四个经典算法 Kruskal算法.Prim算法---------------最小生成树 ...

  10. [转载]LinkButton跳转页面及传递参数

    在DataList中使用LinkButton按钮(LinkButtonDelete),该按钮用于链接跳转到删除页面.在模板中双击该按钮,跳转到.cs页面.问题是我们如何获得该条信息的ID,如果不知道I ...