HDU 1548 A strange lift(dij+邻接矩阵)
//dijkstra算法,
//只是有效边(即能从i楼到j楼)的边权都为1(代表次数1);
//关于能否到达目标楼层b,只需判断最终lowtime[b]是否等于INF即可。
#include<iostream>
#include<cstdio>
using namespace std;
const int INF=10e7;
const int MAXN=210;
int k,minn;
int K[MAXN];
int cost[MAXN][MAXN];
int lowtime[MAXN];
bool vis[MAXN]; void dij(int n,int start)
{
for(int i=1;i<=n;i++)
{
lowtime[i]=INF;vis[i]=0;
}
lowtime[start]=0;
for(int i=1;i<=n;i++)
{
k=-1,minn=INF;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&lowtime[i]<minn)
{minn=lowtime[i];k=i;}
}
if(k==-1) break;
vis[k]=1;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&cost[k][i]>=0&&lowtime[k]+cost[k][i]<lowtime[i])
{
lowtime[i]=lowtime[k]+cost[k][i];
}
}
}
} int main()
{
int n,m,a,b;
while(scanf("%d",&n)&&n!=0)
{
scanf("%d%d",&a,&b);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cost[i][j]=cost[j][i]=INF;
for(int i=1;i<=n;i++)
{
scanf("%d",&K[i]);
if(i+K[i]<=n)
cost[i][i+K[i]]=1;
if(i-K[i]>=1)
cost[i][i-K[i]]=1;
}
dij(n,a);
if(lowtime[b]==INF)
printf("-1\n");
else
printf("%d\n",lowtime[b]);
}
return 0;
}
HDU 1548 A strange lift(dij+邻接矩阵)的更多相关文章
- 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 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(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 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 搜索
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 ...
- 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 (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
随机推荐
- 激活JetBrains PhpStorm 2016.3.2和JetBrains WebStorm 2016.3.2
1.打开 phpstorm 2.在激活界面选择license server 在线激活方式 输入:http://idea.imsxm.com/ 3.激活成功,打开使用
- 快速排序(js版本)
快速排序的时间复杂度为:O(n*log2n),相比较其他O(n2)的排序算法,还是比较有优势的.原文参考在此处,因为本人对原文的一小段代码有点不理解,所以进行了小的修改. 1.基本思想:在数组的第一个 ...
- L2-001. 紧急救援
L2-001. 紧急救援 题目链接:https://www.patest.cn/contests/gplt/L2-001 Dijstra 本题是dijstra的拓展,在求最短路的同时,增加了不同的最短 ...
- 针对IE的CSS样式hack
针对IE的CSS样式hack,如下: 例子:background:#000: 1.只针对IE6的hack方式(_):_background:#000: 2.只针对IE7的hack方式(+):+back ...
- Access denied for user 'root'@'localhost' (using password:YES) 解决方案[转]
关于昨天下午说的MySQL服务无法启动的问题,解决之后没有进入数据库,就直接关闭了电脑. 今早打开电脑,开始-运行 输入“mysql -uroot -pmyadmin”后出现以下错误: “Access ...
- Linux入门(二)Linux基本命令及基本操作
1 常用Linux命令 图形界面进入到字符界面: ctrl+alt+F2~F6 字符界面进入到图形界面:ctrl +alt+F7 查看本机ip: ifconfig (windows是:ipconf ...
- hdu_5286_wyh2000 and sequence(分块)
题目链接:hdu_5286_wyh2000 and sequence 题意: 给一段长度为N的序列,每次询问l-r(l和r和上一次询问的答案有关)内 不同的数的 出现次数的次方 的和.强制在线 题解: ...
- 5、判断、循环、数组综合练习案例(迷你DVD)
迷你dvd代码如下: package com.manager; import java.util.Scanner; public class DVDManage { public static voi ...
- OpenCV2.x自学笔记——固定阈值
threshold( const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type) ...
- LeetCode OJ 73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...