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): 18723 Accepted Submission(s):
6926
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"?
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.
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".
题意:一个特别的电梯,按up可升上k[i]层,到大i+k[i]层,down则到达i-k[i]层,最高不能超过n,最低不能小于1,给你一个起点和终点,问最少可以按几次到达目的地。在一个N层高的楼有一个奇怪的电梯,在每一层只能上升或下降一个特定的层数,中间不会停止,在给定的条件下,问能不能到达指定楼层,可以到达的话返回转操作次数,不可以的话返回-1.
附上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define M 205
#define MAX 0x3f3f3f3f
using namespace std;
int map[M][M],vis[M],dis[M];
int main()
{
int n,a,b,i,j,s;
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
if(i==j)
map[i][j]=; //同一个地方距离为0
else
map[i][j]=MAX;
}
for(i=; i<=n; i++)
{
scanf("%d",&s);
if(i+s<=n) //标记这一层电梯可以去的楼层
map[i][s+i]=;
if(i-s>=)
map[i][i-s]=;
}
vis[a]=; //起点已走过
for(i=; i<=n; i++)
dis[i]=map[a][i]; //初始化距离为每个点到起点的距离
int min,k,t;
for(i=; i<=n; i++)
{
min=MAX;
for(j=; j<=n; j++)
if(!vis[j]&&dis[j]<min) //每次都找离终点最近的点
{
min=dis[j];
t=j;
}
vis[t]=; //标记为已经找过此点
for(j=; j<=n; j++)
if(!vis[j]&&map[t][j]<MAX) //从最近的点到下一个点的距离与初始距离进行比较
if(dis[j]>dis[t]+map[t][j])
dis[j]=dis[t]+map[t][j];
}
if(dis[b]<MAX)
printf("%d\n",dis[b]);
else //不能到 则输出-1
printf("-1\n");
}
return ;
}
邻接表代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
int from,to,val,next;
}edge[];
int tol,s,t,n;
int dis[];
bool vis[];
int head[]; void init()
{
tol=;
memset(head,-,sizeof(head));
} void addEdge(int u,int v)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].val=;
edge[tol].next=head[u];
head[u]=tol++;
} void getmap()
{
int x;
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(i-x>=) addEdge(i,i-x);
if(i+x<=n) addEdge(i,i+x);
}
memset(vis,false,sizeof(vis));
memset(dis,inf,sizeof(dis));
} void spfa()
{
queue<int>q;
q.push(s);
vis[s]=true;
dis[s]=;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].val)
{
dis[v]=dis[u]+edge[i].val;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
if(dis[t]<inf)
printf("%d\n",dis[t]);
else
printf("-1\n");
return;
} int main()
{
int i,j;
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&s,&t);
init();
getmap();
spfa();
}
}
hdu 1548 A strange lift(迪杰斯特拉,邻接表)的更多相关文章
- 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 3339 In Action(迪杰斯特拉+01背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- HDU 2544最短路 (迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Me ...
- HDU 1548 A strange lift (Dijkstra)
https://vjudge.net/problem/HDU-1548 题意: 电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点 ...
- 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 (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- 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 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- 洛谷P1077 [NOIP2012普及组]摆花 [2017年四月计划 动态规划14]
P1077 摆花 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能 ...
- web服务发展历程
PhP发展历史1.php: 开始名字含义:personal home page 个人网页 现在名字含义:HyperText Perprocessor 超文本预处理语言 预处理: 说明PHP是在服务器预 ...
- web前端学习(二)html学习笔记部分(5)--拖放元素、canvas画布使用
1.2.11 拖放 1.2.11.1 html拖放 1.2.11.2 html拖放本次资源 showOjb(一个对象)展示一下一个对象的信息. 1.2.12 html画布(canvas) 标 ...
- 【eclipse】解决:eclipse或STS运行maven工程出现Missing artifact jdk.tools:jdk.tools:jar:1.7问题
eclipse或STS运行maven工程出现Missing artifact jdk.tools:jdk.tools:jar:1.7问题 最近项目中使用到大数据平台,代码中应用了hbase-clien ...
- Congratulation!顺利通过-2019年6月份的PMP考试
祝贺邮件 证书
- delete records in table A not in table B
转)A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低 ...
- VM虚拟机下安装无线网卡教程
前言: 由于最近学习olsrd需要,然后需要无线网卡支持.所以将教程分享如下. 实体机:Windows 7 虚拟机:Ubuntu 14.04 无线网卡:Tenda W311M V3.0 虚拟机软件:V ...
- LintCode_69 二叉树前序遍历
题目 给出一棵二叉树,返回其节点值的前序遍历. 和中序遍历基本相同 C++代码 vector<int> preorderTraversal(TreeNode *root) { // wri ...
- NLP+2vec︱认识多种多样的2vec向量化模型
1.word2vec 耳熟能详的NLP向量化模型. Paper: https://papers.nips.cc/paper/5021-distributed-representations-of-wo ...
- JavaScript--DOM操作例子:隔行变色
上效果: 实现思想: 主要是js动态创建标签,还有动态结合css实现样式 <!DOCTYPE html> <html lang="en"> <head ...