HDU1548——A strange lift(最短路径:dijkstra算法)
A strange lift
Description
There is a strange lift.The lift can stop can at every 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"?
Input
The input consists of several test cases.,Each test 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.
Output
For each case of the input output a interger, the least 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".
Sample Input
5 1 5
3 3 1 2 5
0
Sample Output
3
题目大意:
从前有一栋楼,楼里面有一座电梯,电梯非常奇葩。
一栋楼有N层(1-N),楼层i的电梯可以去i-k[i]和i+k[i]层,(k数组题目给定)(即按了一次按钮)
求从A层到B层最少的按按钮次数。
解题思路:
BFS可解,做过类似的题,所以这里使用dijkstra算法。
最短路径dijkstra算法可解,begin为i,end为i+k[i]&&i-k[i],边的权值为1。求从A到B的最短路径就是答案。(注意为有向图)
Code:
#include<stdio.h>
#include<limits.h>
#include<iostream>
#include<string.h>
#define MAXN 200
using namespace std;
int edge[MAXN+][MAXN+];
int dis[MAXN+];
bool vis[MAXN+];
int T,S,D,N,k;
void dijkstra(int begin)
{
memset(vis,,sizeof(vis));
for (int i=; i<=T; i++)
dis[i]=INT_MAX;
dis[begin]=;
for (int t=; t<=T; t++)
{
vis[begin]=;
for (int i=; i<=T; i++)
if (!vis[i]&&edge[begin][i]!=INT_MAX&&dis[begin]+edge[begin][i]<dis[i])
dis[i]=dis[begin]+edge[begin][i];
int min=INT_MAX;
for (int j=; j<=T; j++)
if (!vis[j]&&min>dis[j])
{
min=dis[j];
begin=j;
}
}
}
int main()
{
int begin,end;
while (cin>>T)
{
if (T==) break;
for (int i=;i<=MAXN;i++)
for (int j=;j<=MAXN;j++)
edge[i][j]=INT_MAX;
scanf("%d %d",&begin,&end);
int t;
for (int i=;i<=T;i++)
{
scanf("%d",&t); //注意是有向图!一开始因为这个WA了好几次。
if (i+t<=T) edge[i][i+t]=;
if (i-t>=) edge[i][i-t]=;
}
dijkstra(begin);
if (dis[end]!=INT_MAX) printf("%d\n",dis[end]);
else printf("-1\n");
}
return ;
}
HDU1548——A strange lift(最短路径:dijkstra算法)的更多相关文章
- 网络最短路径Dijkstra算法
最近在学习算法,看到有人写过的这样一个算法,我决定摘抄过来作为我的学习笔记: <span style="font-size:18px;">/* * File: shor ...
- 单源最短路径Dijkstra算法,多源最短路径Floyd算法
1.单源最短路径 (1)无权图的单源最短路径 /*无权单源最短路径*/ void UnWeighted(LGraph Graph, Vertex S) { std::queue<Vertex&g ...
- 最短路径-Dijkstra算法与Floyd算法
一.最短路径 ①在非网图中,最短路径是指两顶点之间经历的边数最少的路径. AE:1 ADE:2 ADCE:3 ABCE:3 ②在网图中,最短路径是指两顶点之间经历的边上权值之和最短的路径 ...
- 数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )
数据结构实验之图论七:驴友计划 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- 最短路径——Dijkstra算法以及二叉堆优化(含证明)
一般最短路径算法习惯性的分为两种:单源最短路径算法和全顶点之间最短路径.前者是计算出从一个点出发,到达所有其余可到达顶点的距离.后者是计算出图中所有点之间的路径距离. 单源最短路径 Dijkstra算 ...
- 有向网络(带权的有向图)的最短路径Dijkstra算法
什么是最短路径? 单源最短路径(所谓单源最短路径就是只指定一个顶点,最短路径是指其他顶点和这个顶点之间的路径的权值的最小值) 什么是最短路径问题? 给定一带权图,图中每条边的权值是非负的,代表着两顶点 ...
- Python数据结构与算法之图的最短路径(Dijkstra算法)完整实例
本文实例讲述了Python数据结构与算法之图的最短路径(Dijkstra算法).分享给大家供大家参考,具体如下: # coding:utf-8 # Dijkstra算法--通过边实现松弛 # 指定一个 ...
- 求两点之间最短路径-Dijkstra算法
Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.D ...
- 最短路径—Dijkstra算法
Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...
随机推荐
- SMB/CIFS协议解析一概述
一.SMB/CIFS协议的区别 在NetBIOS出现之后,Microsoft就使用NetBIOS实现了一个网络文件/打印服务系统,这个系统基于NetBIOS设定了一套文件共享协 议,Microsoft ...
- HTML5跨文档消息传递
HTML5定义了一些javascript API,其中有一个就是跨文档消息传递(cross-document-messaging简称XDM). 现在XDM已经作为一个规范独立了出来,名字为:Web M ...
- “Cache-control”常见的取值有private、no-cache、max-age、must-revalidate等
网页的缓存由HTTP消息头中的"Cache-Control" 来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为pri ...
- 51nod1265四点共面
1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共线也算共面).如 ...
- YII千万级PV架构经验分享--俯瞰篇--性能介绍
一张图,啥也不说了.直接上图,大图真难画. 呃,非得写满二百个字,其实本来想画均衡负债,一些服务器假设列子的,突然发现,没有业务要求,画不出来.写了这么久了,天天熬夜,得休息几天再继续.其实还有非常重 ...
- UTF8后MD5
function ToUTF8Encode(str: string): string; //将字符串转UTF8编码 var b: Byte; begin for b in BytesOf(UTF8En ...
- 实现Linux select IO复用C/S服务器代码
已在ubuntu 下验证可用 服务器端 #include<stdio.h>#include<unistd.h>#include<stdlib.h>#include& ...
- JQuery的鼠标滚动事件
jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用. 注意当浏览器窗口大小改变时(如最 ...
- gc overhead limit exceeded
eclipse-- gc overhead limit exceeded 修改内存不足的方法如下: Eclipse报错:gc overhead limit exceeded eclipse 原因是Ec ...
- 创建dblink 同义词
database link dblink的主要作用是两个数据库间的数据访问 create database link my_test connect to testdbname identified ...