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;
}
时间: 2024-10-15 07:26:49

HDU 1548 A strange lift(dij+邻接矩阵)的相关文章

HDU 1548 A strange lift(Dijkstra,简单BFS)

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st

hdu 1548 A strange lift (dijkstra算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几点 (1)每次按一下,只能表示上或者是下,然后根据输入的看是上几层或者是下几层. (2)注意不能到底不存在的楼层. 详见代码. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int IN

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): 11341    Accepted Submission(s): 4289 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548 A strange lift【不错的最短路,spfa】

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21281    Accepted Submission(s): 7786 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548.A strange lift

A strange lift Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1548 Description There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N)

HDU 1548 A strange lift(最短路&amp;&amp;bfs)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26943    Accepted Submission(s): 9699 Problem Description There is a strange lift.The lift can stop can at every floor as you want,

hdu 1548 A strange lift Dijkstra+SPFA算法AC

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13777    Accepted Submission(s): 5254 Problem Description There is a strange lift.The lift can stop can at every floor as you want

hdu 1548 A strange lift (dijkstra)

A strange liftTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32529    Accepted Submission(s): 11664 Problem DescriptionThere is a strange lift.The lift can stop can at every floor as you want,

Hdu 1548 A strange lift(BFS)

Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1548 一道简单的bfs,适合新手.你现在所在的电梯层为一个节点,而你从这个节点可以拜访另外两个节点(电梯向上走为一个节点,电梯向下走有一个节点),而拜访的时候自然也要避免拜访重复,否则会陷入死循环. #include <iostream> #include <queue> using namespace std; const int maxn = 200+10; int N,