题目大意:有一个升降机,它有两个按钮UP和DOWN,给你一些数i表示层数,并且每层对应的Ki,如果按UP按钮,会从第i层升到第i+Ki层;如果按了DOWN则会从第i层降到第i-Ki层;并规定能到的层数为1到N,现在的要求就是给你N,A,B和一串数K1到Kn,问你从A到B,至少按几下按钮。

构造一个图,边的权值为1

Sample Input
5 1 5 //层数 起点 终点
3 3 1 2 5
0

Sample Output
3

Dijkstra:(O(n^2))

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8
 9 const int MAXN=300;
10 const int INF=0x3f3f3f3f;
11 int n ;
12 bool vis[MAXN];
13 int cost[MAXN][MAXN] ;
14 int lowcost[MAXN] ;
15 int pre[MAXN];
16 void Dijkstra(int beg)
17 {
18     for(int i=0;i<n;i++)
19     {
20         lowcost[i]=INF;vis[i]=false;pre[i]=-1;
21     }
22     lowcost[beg]=0;
23     for(int j=0;j<n;j++)
24     {
25         int k=-1;
26         int Min=INF;
27         for(int i=0;i<n;i++)
28             if(!vis[i]&&lowcost[i]<Min)
29             {
30                 Min=lowcost[i];
31                 k=i;
32             }
33             if(k==-1)
34                 break ;
35             vis[k]=true;
36             for(int i=0;i<n;i++)
37                 if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
38                 {
39                     lowcost[i]=lowcost[k]+cost[k][i];
40                         pre[i]=k;
41                 }
42     }
43
44 }
45
46
47
48 int main ()
49 {
50    // freopen("in.txt","r",stdin) ;
51
52     while (scanf("%d" , &n ) !=EOF)
53     {
54         if (n==0)
55             break ;
56         int i , j ;
57         for (i = 0 ; i < n ; i++)
58             for (j = 0 ; j < n ; j++)
59                cost[i][j] = INF ;
60         int s , e , t;
61         scanf("%d %d" , &s , &e) ;
62         for (i = 0 ; i < n ; i++)
63         {
64             scanf("%d" , &t) ;
65             if (i + t <= n-1)
66                 cost[i][i+t] = 1 ;
67             if (i - t >= 0)
68                 cost[i][i-t] = 1 ;
69         }
70         Dijkstra(s-1) ;
71         if (lowcost[e-1] != INF)
72             printf("%d\n" , lowcost[e-1]) ;
73         else
74             printf("-1\n") ;
75     }
76
77     return 0 ;
78 }

View Code

堆优化:

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # include <queue>
 7 # define LL long long
 8 using namespace std ;
 9
10 const int INF=0x3f3f3f3f;
11 const int MAXN=300;
12 struct qnode
13 {
14     int v;
15     int c;
16     qnode(int _v=0,int _c=0):v(_v),c(_c){}
17     bool operator <(const qnode &r)const
18     {
19         return c>r.c;
20     }
21 };
22 struct Edge
23 {
24     int v,cost;
25     Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
26 };
27 vector<Edge>E[MAXN];
28 bool vis[MAXN];
29 int dist[MAXN];
30 int n ;
31 void Dijkstra(int start)//点的编号从1开始
32 {
33     memset(vis,false,sizeof(vis));
34     for(int i=1;i<=n;i++)dist[i]=INF;
35     priority_queue<qnode>que;
36     while(!que.empty())que.pop();
37     dist[start]=0;
38     que.push(qnode(start,0));
39     qnode tmp;
40     while(!que.empty())
41     {
42         tmp=que.top();
43         que.pop();
44         int u=tmp.v;
45         if(vis[u])continue;
46         vis[u]=true;
47         for(int i=0;i<E[u].size();i++)
48         {
49             int v=E[tmp.v][i].v;
50             int cost=E[u][i].cost;
51             if(!vis[v]&&dist[v]>dist[u]+cost)
52             {
53                 dist[v]=dist[u]+cost;
54                 que.push(qnode(v,dist[v]));
55             }
56         }
57     }
58 }
59 void addedge(int u,int v,int w)
60 {
61     E[u].push_back(Edge(v,w));
62 }
63
64 int main ()
65 {
66   //  freopen("in.txt","r",stdin) ;
67     int m ;
68     while (scanf("%d" , &n) !=EOF)
69     {
70         if (n==0 )
71             break ;
72         int u , v , w ;
73         int i , j ;
74         for(i=1;i<=n;i++)
75             E[i].clear();
76
77         int s , e , t;
78         scanf("%d %d" , &s , &e) ;
79         for (i = 1 ; i <= n ; i++)
80         {
81             scanf("%d" , &t) ;
82             if (i + t <= n)
83                 addedge(i,i+t,1) ;
84             if (i - t >= 1)
85                 addedge(i,i-t,1) ;
86         }
87         Dijkstra(s) ;
88         if (dist[e] != INF)
89             printf("%d\n" , dist[e]) ;
90         else
91             printf("-1\n") ;
92     }
93
94     return 0 ;
95 }

View Code

转载于:https://www.cnblogs.com/mengchunchen/p/4587102.html

hdu 1548 升降梯相关推荐

  1. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  2. hdu 1548 A strange lift

    A strange lift Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

  3. A strange lift HDU - 1548(基础广搜)

    There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...

  4. hdu 1548 A strange lift (BFS)

    解题思路:BFS 注意点:每次查询结束后,要清空队列 #include <iostream> #include <queue> #include <cstring> ...

  5. 最短路径问题经典题目汇总

    50道数据结构最短路径问题 HDU 1142 http://acm.hdu.edu.cn/showproblem.php?pid=1142 HDU 1217 http://acm.hdu.edu.cn ...

  6. 杭电OJ分类题目(4)-Graph

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...

  7. ACM—最短路—8月14日

    ACM-最短路-8月14日 训练网址: http://acm.hdu.edu.cn/diy/contest_show.php?cid=24386 学习博客 最小生成树与最短路径的区别以及实现方法 - ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. [转] HDU 题目分类

    转载来自:http://www.cppblog.com/acronix/archive/2010/09/24/127536.aspx 分类一: 基础题:1000.1001.1004.1005.1008 ...

最新文章

  1. ubuntu 压缩率最高的软件_不用写采集规则也可以轻松采集网站文章,揭秘一款明泽文章采集软件的工作原理...
  2. 2018 CVPR GAN 相关论文调研
  3. Eclipse Collections随Java版本的演变
  4. html js坐标图,javascript – HTML5 Canvas沿着带坐标的路径拖动图像
  5. windows守护进程_在Linux的Windows子系统上(WSL)使用Docker(Ubuntu)
  6. tensorflow 人面关键点识别_人工智能算法-图像识别项目
  7. 电子双缝干涉,可以在穿过缝前进行探测
  8. 苹果耳机无线真假测试软件,辨认苹果耳机真假,就这六步。
  9. eclipse汉化小教程
  10. PHP设置谷歌验证器(Google Authenticator)实现操作二步验证
  11. 一种基于自动机的快速分词方法
  12. html css animate,animate.css的使用方法
  13. 区块链溯源是如何实现的?
  14. 如何使用Echarts设计专业的K线图
  15. 文件隐藏服务器版本信息,隐藏版本信息
  16. 一 STM32时钟系统
  17. Java 中 Integer 源码学习之缓存池了解
  18. 《Java SE实战指南》01-09:常见问题及其解决方案
  19. mongodb代码之复制集合(pymongo)
  20. curl检查端口_curl命令(测试连接命令)

热门文章

  1. C语言之变量和数据类型
  2. 用于工作的 Linux 桌面——Ubuntu 22.04
  3. 经常用到的透视函数(行转列列转行)函数 PIVOT()UNPIVOT
  4. 直升机空气动力学基础--003翼型的升力
  5. 关于YDWE在保存时的Lua数据添加
  6. lucene初探(-):lucene基本,实现文件查找
  7. 安装xdebug-helper插件
  8. python tkinter listbox_Python Tkinter Listbox和Combobox控件用法
  9. KMIP(密钥管理互通协议)
  10. 分享Windows版pgadmin(v4.17)