HDU 1548 A strange lift (Dijkstra)
https://vjudge.net/problem/HDU-1548
题意:
电梯每层有一个不同的数字,例如第n层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层,给定起点与终点,要求出最少要按几次键。
思路:
可以用BFS,也可以用迪杰斯特拉算法。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; #define INF 100000000 int n, A, B;
int map[][];
int vis[];
int d[];
int num[]; void Dijkstra()
{
memset(vis, , sizeof(vis));
for (int i = ; i <= n; i++)
{
num[i] = map[A][i];
}
num[A] = ;
vis[A] = ;
for (int i = ; i < n; i++)
{
int min = INF;
int pos;
for (int j = ; j <= n; j++)
{
if (num[j] < min && !vis[j])
{
pos = j;
min = num[j];
}
}
if (min == INF) break;
vis[pos] = ;
for (int j = ; j <= n; j++)
{
if (num[pos] + map[pos][j] < num[j] && !vis[j])
num[j] = num[pos] + map[pos][j];
}
}
if (num[B] == INF) printf("-1\n");
else printf("%d\n", num[B]);
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int a;
while (scanf("%d", &n) && n)
{
scanf("%d%d", &A, &B);
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
map[i][j] = INF;
for (int i = ; i <= n; i++)
{
scanf("%d", &a);
if (i + a <= n)
map[i][i + a] = ;
if (i - a >= )
map[i][i - a] = ;
}
Dijkstra();
}
return ;
}
HDU 1548 A strange lift (Dijkstra)的更多相关文章
- HDU 1548 A strange lift(BFS)
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- 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 (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
- 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(dij+邻接矩阵)
( ̄▽ ̄)" //dijkstra算法, //只是有效边(即能从i楼到j楼)的边权都为1(代表次数1): //关于能否到达目标楼层b,只需判断最终lowtime[b]是否等于INF即可. # ...
- 杭电 1548 A strange lift(广搜)
http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift 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 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
随机推荐
- 006-Redis 发布订阅
Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 客户端可以订阅任意数量的频道. 下图展示了频道 channel1 , 以及订 ...
- .NET Core 2.0 官方下载地址及中文教程
开发.net core 应用需要安装.NET Core 2.0 SDK http://www.microsoft.com/net/download/core#/sdk 电脑上运行 .net core ...
- SDUT1157:小鼠迷宫问题(bfs+dfs)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1157 题目描述 小鼠a与小鼠b身处一个m×n的 ...
- Java后台通过jxl生成Excel表格
这里封装了一个工具类,将对象的list集合解析生成表格,只要按照参数要求传参就好了. 工具类代码如下: package com.hd.erpreport.utils; import java.io.F ...
- FPKM\RPKM\TPM学习[转载]
转自:http://www.360doc.com/content/18/0112/02/50153987_721216719.shtml 1.问题提出 在RNA-Seq的分析中,对基因或转录本的rea ...
- categoriy 重写函数会怎样?
From comp.lang.objective-C FAQ listing: "What if multiple categories implement the same method? ...
- liferay中如何获取实例的id和portletId
在Portlet中request分为两种renderRequet和actionRequest而portlet需要取得实例Id的时候都在renderRequest的时候才可以取到,如下例子 Portle ...
- Javascript-蔬菜运算价格
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mysql buffer
php与mysql的连接有三种方式,mysql,mysqli,pdo.不管使用哪种方式进行连接,都有使用buffer和不使用buffer的区别. 什么叫使用buffer和不使用buffer呢? 客户端 ...
- 解读jquery.filtertable.min
jQuery.FilterTable是一款表格搜索过滤和单元格高亮插件. 该插件允许你对任意表格进行条件过滤,并且它会将搜索到的结果单元格高亮显示,非常实用和强大. 使用方法在页面中引入jquery和 ...