hdu 1548 A strange lift (dijkstra算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548

题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间。

注意的几点

(1)每次按一下,只能表示上或者是下,然后根据输入的看是上几层或者是下几层。

(2)注意不能到底不存在的楼层。

详见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int INF=9999999;
 5 int map[205][205],node[205],vis[205],Min,n;
 6
 7 void set()
 8 {
 9     for (int i=1; i<=n; i++)
10     {
11         vis[i]=0;
12         node[i]=INF;
13         for (int j=1; j<=n; j++)
14             map[i][j]=INF;
15     }
16 }
17
18 int dijkstra(int m,int end)
19 {
20     int tm=m;
21     vis[m]=1;
22     node[m]=0;
23     for (int k=2; k<=n; k++)
24     {
25         Min=INF;
26         for (int i=1; i<=n; i++)
27             if (!vis[i])
28             {
29                 if (node[i]>node[tm]+map[tm][i])
30                     node[i]=node[tm]+map[tm][i];
31                 if (Min>node[i])
32                 {
33                     Min=node[i];
34                     m=i;
35                 }
36             }
37         vis[m]=1;
38         tm=m;
39         if(vis[end])
40             return node[end];
41     }
42     return -1;
43 }
44
45 int main ()
46 {
47     int k;
48     while (scanf("%d",&n),n)
49     {
50         set();
51         int a,b;
52         scanf("%d%d",&a,&b);
53         for (int i=1; i<=n; i++)
54         {
55             scanf("%d",&k);
56             if (i+k<=n)
57                 map[i][i+k]=1;
58             if (i-k>=1)
59                 map[i][i-k]=1;
60         }
61         printf ("%d\n",dijkstra(a,b));
62     }
63     return 0;
64 }

hdu 1548 A strange lift (dijkstra算法)

时间: 2024-10-09 22:40:46

hdu 1548 A strange lift (dijkstra算法)的相关文章

HDU 1548 A strange lift(Dijkstra,简单BFS)

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st

hdu 1548 A strange lift Dijkstra+SPFA算法AC

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13777    Accepted Submission(s): 5254 Problem Description There is a strange lift.The lift can stop can at every floor as you want

hdu 1548 A strange lift (dijkstra)

A strange liftTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32529    Accepted Submission(s): 11664 Problem DescriptionThere is a strange lift.The lift can stop can at every floor as you want,

HDU 1548 A strange lift 搜索

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11341    Accepted Submission(s): 4289 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548 A strange lift【不错的最短路,spfa】

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21281    Accepted Submission(s): 7786 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548.A strange lift

A strange lift Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1548 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)

HDU 1548 A strange lift(dij+邻接矩阵)

( ̄▽ ̄)" //dijkstra算法, //只是有效边(即能从i楼到j楼)的边权都为1(代表次数1): //关于能否到达目标楼层b,只需判断最终lowtime[b]是否等于INF即可. #include<iostream> #include<cstdio> using namespace std; const int INF=10e7; const int MAXN=210; int k,minn; int K[MAXN]; int cost[MAXN][MAXN];

HDU 1548 A strange lift(最短路&amp;&amp;bfs)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26943    Accepted Submission(s): 9699 Problem Description There is a strange lift.The lift can stop can at every floor as you want,

HDU 1548 A strange lift (Dijkstra)

https://vjudge.net/problem/HDU-1548 题意: 电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点,要求出最少要按几次键. 思路: 可以用BFS,也可以用迪杰斯特拉算法. 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 using name