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 ...
随机推荐
- mysql 导出表结构
mysql导出数据库各表结构,很简单只要一条命令即可: mysqldump -uxxx -d databasename [,table] > xxx.sql mysqldump中-d参数即为只导 ...
- 印象笔记无法同步问题解决 Unable to send HTTP request: 12029
问题 今天突然发现本地软件不能访问网络. 包括: 印象笔记无法同步, 搜狗输入法无法登陆. 但其它上网正常. 思路及解决过程 因为chrome上网 ,qq上网均正常. 且同事可以正常使用. 推测是本地 ...
- Linux C 程序指针和指针数组(NIGH)
指针和指针数组 #include<stdio.h> int main() { , b = ; int *p1 = &a , *p2 = &b ; printf(" ...
- jQuery插件开发 - 其实很简单
[前言] jQuery已经被广泛使用,凭借其简洁的API,对DOM强大的操控性,易扩展性越来越受到web开发人员的喜爱,我在社区也发布了很多的jQuery插件,经常有人询问一些技巧,因此干脆写这么一篇 ...
- asp.net runat="server" && hiddenfield
runat="server", c#可以直接获得client控件,并且赋值 hiddenfield 可以作为传值,或者界面存值,后台每次读取,并且再赋值到前台,这样前台就可以把上一 ...
- TCP三次握手原理与SYN攻击
本文内容包括以下几点 1.TCP三次握手四次挥手解析 2.迭代型服务器程序编写,并给出客户端,结合这一模式详细介绍Berkeley套接字的使用 3.介绍SYN攻击的原理 TCP连接建立,传输数据,连接 ...
- 在SAE上同步djanogo的mysql数据库
折腾了一个下午,终于搞掂了把djanogo应用的mysql数据库导入到SAE上了,归根到底麻烦的根源是SAE限制多多.下面简单记录一下过程以备日后参考使用. 首先还是修改settings.py,把数据 ...
- Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)
Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)
- 【IOCP】 IOCP模型属于一种通讯模型- 较难
http://baike.baidu.com/link?url=e9vXkKd2aHp8VDr1XTURdwQB4K85r28IYjeMwRIyuaXtsrCsXHY1eohiFgsDXRYRlj6x ...
- 【BZOJ 1013】 [JSOI2008]球形空间产生器sphere
Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁 ...