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,然后进行最短路算法就可以得到结果了,实在想不到建图还是可以用搜索做的。
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <queue>
  6. using namespace std;
  7. const int N =;
  8. const int INF = ;
  9. int n;
  10. int graph[N][N];
  11. int low[N];
  12. bool vis[N];
  13. void dijkstra(int s){
  14. memset(vis,false,sizeof(vis));
  15. for(int i=;i<=n;i++){
  16. low[i] = graph[s][i];
  17. }
  18. vis[s] = true;
  19. for(int i=;i<n;i++){
  20. int Min = INF;
  21. for(int j=;j<=n;j++){
  22. if(Min>low[j]&&!vis[j]){
  23. s = j;
  24. Min = low[j];
  25. }
  26. }
  27. vis[s] = true;
  28. for(int j=;j<=n;j++){
  29. if(low[j]>low[s]+graph[s][j]&&!vis[j]){
  30. low[j] = low[s]+graph[s][j];
  31. }
  32. }
  33. }
  34. }
  35. int main()
  36. {
  37. while(scanf("%d",&n)!=EOF,n){
  38. int s,t;
  39. scanf("%d%d",&s,&t);
  40. for(int i=;i<=n;i++){
  41. for(int j=;j<=n;j++){
  42. if(i==j) graph[i][j]=;
  43. else graph[i][j] = INF;
  44. }
  45. }
  46. for(int i=;i<=n;i++){
  47. int num;
  48. scanf("%d",&num);
  49. if(i-num>=) graph[i][i-num] = ;
  50. if(i+num<=n) graph[i][i+num] = ;
  51. }
  52. dijkstra(s);
  53. if(low[t]>=INF) printf("-1\n");
  54. else printf("%d\n",low[t]);
  55. }
  56. return ;
  57. }

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. 经典dfs(depth-first search)

    DFS主要在于参数的改变; 样例输入: n=4                //给定n个数字 a={1,2,4,7}    //输入n个数据 k=15              //目标数字 样例输 ...

  2. Charles Babbage【查尔斯·巴贝奇】

    Charles Babbage When Babbage was working at Cambridge, a new idea occurred to him. 巴贝奇在剑桥工作的时候,脑海中有了 ...

  3. 笔记-python-selenium,phantomjs

    笔记-python-selenium,phantomjs 1.      简介 1.1.    selenium selenium是一款自动化测试工具,支持多种语言 为什么爬虫要使用selenium呢 ...

  4. 笔记-http-header

    笔记-http-header 1.      Requests部分 Host:请求的web服务器域名地址 User-Agent:HTTP客户端运行的浏览器类型的详细信息.通过该头部信息,web服务器可 ...

  5. Android资源限定符

    Android系统会根据设备参数,自动选择最佳资源配置方案. Android中常见的资源限定符: 屏幕特征 限定符 描述 大小 small 提供给小屏幕设备的资源 normal 提供给中等屏幕设备的资 ...

  6. xss games20关小游戏附源代码

    1. get方式的的值直接输出来了. ?name=<script>alert(1)</script> 2. 同样没有过滤,不过需要闭合前边的双引号和>. "&g ...

  7. PyInstaller打包python脚本

    用python写的工具写好了,想打包然后发给测试同事使用,最后选择了PyInstaller,支持Windows.Linux.OS X,支持打包成一个文件夹或单个EXE文件.   我是直接在线安装的,在 ...

  8. Halcon17 windows 下载

    Halcon17 windows 下载地址:http://www.211xun.com/download_page_9.html HALCON 17 是一套机器视觉图像处理库,由一千多个算子以及底层的 ...

  9. rpm包管理 命令

    rpm -ivh package.rpmrpm -ivh --force  package_name.rpm # ...conflict with...rpm -ivh --nodeps packag ...

  10. 管理nuget程序包中搜索不到任何程序包

    之前一直使用vs2012开发,管理nuget程序包没有出现过什么问题.因为开发需要,需要使用vs2015,安装vs2015后再使用vs2012时,发现管理nuget程序包中搜索不到任何资源,晕死. 想 ...