A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19516    Accepted Submission(s): 7224

Problem 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
题意:有一个升降机,可上可下,但是每一层都只能上升或者下降固定的层数,给定起点和终点,问从起点到终点最少需要按多少次电梯按钮,不能到输出-1
题解:这题难点在于想到建图,但是相通之后就将每一层与其能够到达的层数连一条有向边就OK,然后进行最短路算法就可以得到结果了,实在想不到建图还是可以用搜索做的。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
const int N =;
const int INF = ;
int n;
int graph[N][N];
int low[N];
bool vis[N];
void dijkstra(int s){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = graph[s][i];
}
vis[s] = true;
for(int i=;i<n;i++){
int Min = INF;
for(int j=;j<=n;j++){
if(Min>low[j]&&!vis[j]){
s = j;
Min = low[j];
}
}
vis[s] = true;
for(int j=;j<=n;j++){
if(low[j]>low[s]+graph[s][j]&&!vis[j]){
low[j] = low[s]+graph[s][j];
}
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF,n){
int s,t;
scanf("%d%d",&s,&t);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j) graph[i][j]=;
else graph[i][j] = INF;
}
}
for(int i=;i<=n;i++){
int num;
scanf("%d",&num);
if(i-num>=) graph[i][i-num] = ;
if(i+num<=n) graph[i][i+num] = ;
}
dijkstra(s);
if(low[t]>=INF) printf("-1\n");
else printf("%d\n",low[t]);
}
return ;
}

hdu 1548(最短路)的更多相关文章

  1. cogs 364. [HDU 1548] 奇怪的电梯 Dijkstra

    364. [HDU 1548] 奇怪的电梯 ★   输入文件:lift.in   输出文件:lift.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 呵呵,有一天我做了 ...

  2. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  3. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

  6. hdu 1548 楼梯 bfs或最短路 dijkstra

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...

  7. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  8. 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 ...

  9. HDU 1548 A strange lift(最短路&&bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. Python猫荐书系列之七:Python入门书籍有哪些?

    本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/ArN-6mLPzPT8Zoq0Na_tsg 最近,猫哥的 Python ...

  2. LOJ #6010. 「网络流 24 题」数字梯形

    #6010. 「网络流 24 题」数字梯形   题目描述 给定一个由 n nn 行数字组成的数字梯形如下图所示.梯形的第一行有 m mm 个数字.从梯形的顶部的 m mm 个数字开始,在每个数字处可以 ...

  3. dp专练

    dp练习. codevs 1048 石子归并 区间dp #include<cstdio> #include<algorithm> #include<cstring> ...

  4. P2615 神奇的幻方

    P2615 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首 ...

  5. 自己用C语言写RH850 F1KM serial bootloader

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 手上有块Renesas ...

  6. C语言编程题002

    给出两个整数,L和R,其中L<=A<=B<=R,然后求出A^B值最大的数.其中1<=L<=R<=1000. 比如说L = 1;R = 3; L 0001 R 001 ...

  7. MFC深入浅出读书笔记第一部分

    最近看侯捷的MFC深入浅出,简单总结一下. 第一章首先就是先了解一下windows程序设计的基础知识,包括win32程序开发基础,什么*.lib,*.h,*.cpp的,程序入口点WinMain函数,窗 ...

  8. Python subprocess.Popen中communicate()和wait()区别

    刚开始我是使用的wait(),但是当adb命令返回太多时,程序就会卡死,查询得知原因后,才使用了communicate(),communicate()返回一个元组:(stdoutdata, stder ...

  9. Python2 HTMLTestRunner自动化测试报告美化

    python2 的测试报告美化,需要的同学直接用 #coding=utf-8 """ A TestRunner for use with the Python unit ...

  10. python的重重之器(生成器、迭代器、装饰器)

    一.装饰器 1.定义:本质是函数,装饰其他函数就是为其他函数添加附件功能. 2.原则: a.不能修改被装饰的函数的源代码: b.不能修改被装饰的函数的调用方式: 实例: #!/usr/bin/env ...