( ̄▽ ̄)"

//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+邻接矩阵)的更多相关文章

  1. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

  2. 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 ...

  3. HDU 1548 A strange lift (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

  4. HDU 1548 A strange lift(最短路&&bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  6. 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 ...

  7. HDU 1548 A strange lift 搜索

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  8. hdu 1548 A strange lift (bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. 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 ...

  10. HDU 1548 A strange lift (广搜)

    题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...

随机推荐

  1. How to use php serialize() and unserialize()

    A PHP array or object or other complex data structure cannot be transported or stored or otherwise u ...

  2. JavaScript DOM编程艺术-学习笔记(总结一)

    1.1)dom-core方法:(不专属于js,支持dom的任何一种程序设计语言都可以使用它,它们的用途,也不仅限于处理网页,也可以用来处理任何一种标记语言编写处理的文档)  ①getElementBy ...

  3. python join()阻塞的用法

    join()阻塞的用法,用来检测线程有没有完全执行完毕 #!/usr/bin/env python#-*- coding:utf-8 -*-import threadingimport time de ...

  4. python多进程,以及进程池并发

    模拟多进程 #!/usr/bin/env python#-*- coding:utf-8 -*-import timefrom multiprocessing import Process def s ...

  5. python的模块!

    以下是对模块的理解,和总结 <1>模块是什么 模块是这样用的 import os 这就是导入了os模块 这和c语言里的#include<stdio.h>导入方式是一样的 导入了 ...

  6. Hibernate5-课程笔记6

    Hibernate检索优化: 检索即查询.为了减轻DB的访问压力,提高检索效率,Hibernate对检索进行了优化. 所谓检索优化,指的是对查询语句的执行时机进行了细致.严格的把控:并不是代码中一出现 ...

  7. Python 2.X-关于函数返回的数值类型

    在使用同一个函数,相同的参数的时候,参数在传递的过程中使用了不同的形式(有无小数点)决定了该函数返回的值的类型. # -*- coding:utf-8 -*- def return_types(one ...

  8. iScroll屏幕滑动函数封装总结

    //iScroll.js屏幕滚动函数 function funScroll(a,b) { var d; function beforload() { d = new iScroll(a, { chec ...

  9. 2016 SyScan360 国际前瞻信息安全会议 多角度探讨信息安全

    SyScan360国际前瞻信息安全会议由与中国第一大互联网安全公司-360公司与SyScan前瞻信息安全技术年会(TheSymposiumonSecurityforAsiaNetwork,以下简称Sy ...

  10. npm-link

    https://docs.npmjs.com/cli/link Description Package linking is a two-step process. First, npm link i ...