hdu 1548(最短路)
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
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"?
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.
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".
3 3 1 2 5
0
#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(最短路)的更多相关文章
- cogs 364. [HDU 1548] 奇怪的电梯 Dijkstra
364. [HDU 1548] 奇怪的电梯 ★ 输入文件:lift.in 输出文件:lift.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 呵呵,有一天我做了 ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- 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 (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- 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(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- RGB色彩的计算机表示
计算机显示模式[编辑] 24比特模式[编辑] 每像素24位(比特s per pixel,bpp)编码的RGB值:使用三个8位无符号整数(0到255)表示红色.绿色和蓝色的强度.这是当前主流的标准表示方 ...
- [BZOJ2331]地板(插头DP)
Description lxhgww的小名叫"小L",这是因为他总是很喜欢L型的东西.小L家的客厅是一个的矩形,现在他想用L型的地板来铺满整个客厅,客厅里有些位置有柱子,不能铺地板 ...
- [USACO]奶牛抗议(DP+树状数组+离散化)
Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组 ...
- TI C64X+通用库函数使用手册
在使用前,当知悉以下几点: 函数进程由手动汇编而成,已充分发挥器件效率.同时TI对外提供C和线性汇编代码 对于个人一些特殊应用,DSPLIB可能会带来额外的cycle消耗 TI DSPLIB依平台和时 ...
- LOJ #6008. 「网络流 24 题」餐巾计划
#6008. 「网络流 24 题」餐巾计划 题目描述 一个餐厅在相继的 n nn 天里,每天需用的餐巾数不尽相同.假设第 i ii 天需要 ri r_iri 块餐巾.餐厅可以购买新的餐巾,每块餐 ...
- 03015_JSTL技术
1.JSTL概述 (1)JSP(JSP Standard Tap Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能.jstl出现的目的同el一样也是要替代js ...
- socketCluster 使用
<html> <head> <title>test</title> <script src="https://cdn.bootcss.c ...
- 54、edittext输入类型限制为ip,inputType应该如何设置
<EditText android:id="@+id/et_setting_printer_edit_info_ip" android:layout_width=" ...
- RSA进阶之低加密指数攻击
适用场景: n很大,4000多位,e很小,e=3 一般来说,e选取65537.但是在RSA加密的时候有可能会选用e=3(不要问为什么,因为选取e =3省时省力,并且他可能觉得n在4000多位是很安全的 ...
- MCMC 浅谈
# MCMC 浅谈 1. 采样(sampling)是什么 MCMC在采样算法中有着举足轻重的地位,那么什么是采样?采样就是根据某种分布生成样本.举个例子,线性同余发生器就是根据均匀分布生成样本,这就很 ...