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 ...
随机推荐
- django之模型层
1. ORM MVC或者MTV框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员 ...
- poj 1258 建光迁问题 最小生成树
题意:给全村建光纤,求花费最小 思路:最小生成树,树相对于图来说就是没有环 m用来存图 v判断是否访问 low用来存两点间的最短距离 给low赋值 for(i=1;i<=n;i++){if(i ...
- 配置Spring.NET
先引入关键的程序集 Common.Logging.dll Spring.Core.dll 在配置文件配置中: <configSections> ...... <sectionGrou ...
- Java中Scanner中nextLine()方法和next()方法的区别
https://blog.csdn.net/hello_word2/article/details/54895106
- leetcode 【 Majority Element 】python 实现
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- SQL语句Not IN优化方案
总结网友们在CSDN社区上对于not in的优化策略,整理如下,备查. select * from emp where emp_no not in (select emp_no from emp ...
- Adaboost和GBDT的区别以及xgboost和GBDT的区别
Adaboost和GBDT的区别以及xgboost和GBDT的区别 以下内容转自 https://blog.csdn.net/chengfulukou/article/details/76906710 ...
- PAT——乙级1018
题目是 1018 锤子剪刀布 (20 point(s)) 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出 ...
- STL之string使用简介
声明一个C++字符串 string类的构造函数和析构函数如下: string s; //生成一个空字符串s string s(str) //拷贝构造函数 生成str的复制品 string s(str, ...
- springboot集成pagehelper插件
1.在pom.xml中引入依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifact ...