HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门
A strange lift
Time Limit: 1000MS Memory Limit: 32768 K
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,否则设置为INF,跑一下Dijkstra就完了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = 205;
int edge[MAX][MAX],dis[MAX];
bool vis[MAX];
int N,A,B;
void Dijkstra()
{
int tmp,pos;
memset(vis,false,sizeof(vis));
for (int i = 1;i <= N;i++)
{
dis[i] = edge[A][i];
}
dis[A] = 0;
vis[A] = true;
for (int i = 2;i <= N;i++)
{
tmp = INF;
for (int j = 1;j <= N;j++)
{
if (!vis[j] && dis[j] < tmp)
{
tmp = dis[j];
pos = j;
}
}
if (tmp == INF) break;
vis[pos] = true;
for (int j = 1;j <= N;j++)
{
if (dis[pos] + edge[pos][j] < dis[j])
{
dis[j] = dis[pos] + edge[pos][j];
}
}
}
printf("%d\n",dis[B] == INF?-1:dis[B]);
}
int main()
{
while (~scanf("%d",&N) && N)
{
int tmp;
memset(edge,INF,sizeof(edge));
for (int i = 0;i <= N;i++)
{
for (int j = 0;j <= i;j++)
{
if (i == j) edge[i][j] = edge[j][i] = 0;
else edge[i][j] = edge[j][i] = INF;
}
}
scanf("%d%d",&A,&B);
for (int i = 1;i <= N;i++)
{
scanf("%d",&tmp);
if (i - tmp > 0)
{
edge[i][i-tmp] = 1;
}
if (i + tmp <= N)
{
edge[i][i+tmp] = 1;
}
}
Dijkstra();
}
return 0;
}
HDU 1548 A strange lift (最短路/Dijkstra)的更多相关文章
- HDU 1548 A strange lift(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- 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 ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- hdu 1548 楼梯 bfs或最短路 dijkstra
http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- hdu 1548 A strange lift(迪杰斯特拉,邻接表)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1548 A strange lift 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- RSA签名验签学习笔记
RSA私钥签名时要基于某个HASH算法,比如MD5或者SHA1等.之前我一直认为签名的过程是:先对明文做HASH计算,然后用私钥直接对HASH值加密.最近才发现不是那么简单,需要对HASH后的数据进行 ...
- Asp.net Mvc中利用ValidationAttribute实现xss过滤
在网站开发中,需要注意的一个问题就是防范XSS攻击,Asp.net mvc中已经自动为我们提供了这个功能.用户提交数据时时,在生成Action参数的过程中asp.net会对用户提交的数据进行验证,一旦 ...
- web安全——目录
说明 写这个目录是为了方便阅读.也是为了记录统一的问题. 这个系列,并不一定是全的,也不一定是对的,所以请大家多做过滤. 这里面场景比较多的是本人在实践中遇到的问题,然后自己思考抽象的. 目录 web ...
- 使用socket()函数创建套接字
在Linux中,一切都是文件,除了文本文件.源文件.二进制文件等,一个硬件设备也可以被映射为一个虚拟的文件,称为设备文件.例如,stdin 称为标准输入文件,它对应的硬件设备一般是键盘,stdout ...
- Firefox about
在firefox的地址栏输入about:about,然后看一下各个链接.有的链接有具体的用途,有的链接疯言疯语,并无软用. about:about集中了火狐浏览器的全部用户界面,平时常见的prefer ...
- getopt
头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const argv[ ],const char * optstring); ...
- 东大OJ-1040-Count-快速幂方法求解斐波那契-
Many ACM team name may be very funny,such as "Complier_Error","VVVVV".Oh,wait fo ...
- android 按钮点击效果实现
在其他人的博客里看到其实实现按钮点击效果的方法有很多,这里提到的只是其中一个办法 图片素材(我自己用截图截的,可以自己搞) 放到mipmap目录下(studio是在这个目录下 , eclipse 直接 ...
- Beta项目冲刺–第四天
考试太多,做项目的时间太少-- 队伍:F4 成员:031302301 毕容甲 031302302 蔡逸轩 031302430 肖阳 031302418 黄彦宁 会议内容: 1.站立式会议照片: 2.项 ...
- (01)javascript 数据类型
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...