• 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


    Problem 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) 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"?
     
    Input
    The input consists of several test cases.,Each test 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.
     
    Output
    For each case of the input output a interger, the least 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".
     
    Sample Input
    5 1 5
    3 3 1 2 5
    0
     
    Sample Output
    3
     
    Recommend
    8600   |   We have carefully selected several similar problems for you:  1385 1142 1372 1072 1690 
     
    最短路的模板题,代码还是很好理解的。
     

    题意:一个特别的电梯,按up可升上k[i]层,到大i+k[i]层,down则到达i-k[i]层,最高不能超过n,最低不能小于1,给你一个起点和终点,问最少可以按几次到达目的地。在一个N层高的楼有一个奇怪的电梯,在每一层只能上升或下降一个特定的层数,中间不会停止,在给定的条件下,问能不能到达指定楼层,可以到达的话返回转操作次数,不可以的话返回-1.

     

    附上代码:

     

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define M 205
     5 #define MAX 0x3f3f3f3f
     6 using namespace std;
     7 int map[M][M],vis[M],dis[M];
     8 int main()
     9 {
    10     int n,a,b,i,j,s;
    11     while(~scanf("%d",&n)&&n)
    12     {
    13         scanf("%d%d",&a,&b);
    14         memset(vis,0,sizeof(vis));
    15         memset(dis,0,sizeof(dis));
    16         for(i=1; i<=n; i++)
    17             for(j=1; j<=n; j++)
    18             {
    19                 if(i==j)
    20                     map[i][j]=0;  //同一个地方距离为0
    21                 else
    22                     map[i][j]=MAX;
    23             }
    24         for(i=1; i<=n; i++)
    25         {
    26             scanf("%d",&s);
    27             if(i+s<=n)       //标记这一层电梯可以去的楼层
    28                 map[i][s+i]=1;
    29             if(i-s>=1)
    30                 map[i][i-s]=1;
    31         }
    32         vis[a]=1;    //起点已走过
    33         for(i=1; i<=n; i++)
    34             dis[i]=map[a][i];   //初始化距离为每个点到起点的距离
    35         int min,k,t;
    36         for(i=1; i<=n; i++)
    37         {
    38             min=MAX;
    39             for(j=1; j<=n; j++)
    40                 if(!vis[j]&&dis[j]<min)  //每次都找离终点最近的点
    41                 {
    42                     min=dis[j];
    43                     t=j;
    44                 }
    45             vis[t]=1;               //标记为已经找过此点
    46             for(j=1; j<=n; j++)
    47                 if(!vis[j]&&map[t][j]<MAX)   //从最近的点到下一个点的距离与初始距离进行比较
    48                     if(dis[j]>dis[t]+map[t][j])
    49                         dis[j]=dis[t]+map[t][j];
    50         }
    51         if(dis[b]<MAX)
    52         printf("%d
    ",dis[b]);
    53         else           //不能到 则输出-1
    54         printf("-1
    ");
    55     }
    56     return 0;
    57 }

     邻接表代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 #define inf 0x3f3f3f3f
     6 using namespace std;
     7 struct Edge
     8 {
     9     int from,to,val,next;
    10 }edge[40500];
    11 int tol,s,t,n;
    12 int dis[205];
    13 bool vis[205];
    14 int head[40500];
    15 
    16 void init()
    17 {
    18     tol=0;
    19     memset(head,-1,sizeof(head));
    20 }
    21 
    22 void addEdge(int u,int v)
    23 {
    24     edge[tol].from=u;
    25     edge[tol].to=v;
    26     edge[tol].val=1;
    27     edge[tol].next=head[u];
    28     head[u]=tol++;
    29 }
    30 
    31 void getmap()
    32 {
    33     int x;
    34     for(int i=1;i<=n;i++)
    35     {
    36         scanf("%d",&x);
    37         if(i-x>=1) addEdge(i,i-x);
    38         if(i+x<=n) addEdge(i,i+x);
    39     }
    40     memset(vis,false,sizeof(vis));
    41     memset(dis,inf,sizeof(dis));
    42 }
    43 
    44 void spfa()
    45 {
    46     queue<int>q;
    47     q.push(s);
    48     vis[s]=true;
    49     dis[s]=0;
    50     while(!q.empty())
    51     {
    52         int u=q.front();
    53         q.pop();
    54         vis[u]=false;
    55         for(int i=head[u];i!=-1;i=edge[i].next)
    56         {
    57             int v=edge[i].to;
    58             if(dis[v]>dis[u]+edge[i].val)
    59             {
    60                 dis[v]=dis[u]+edge[i].val;
    61                 if(!vis[v])
    62                 {
    63                     vis[v]=true;
    64                     q.push(v);
    65                 }
    66             }
    67         }
    68     }
    69     if(dis[t]<inf)
    70     printf("%d
    ",dis[t]);
    71     else
    72     printf("-1
    ");
    73     return;
    74 }
    75 
    76 int main()
    77 {
    78     int i,j;
    79     while(~scanf("%d",&n)&&n)
    80     {
    81         scanf("%d%d",&s,&t);
    82         init();
    83         getmap();
    84         spfa();
    85     }
    86 }
  • 相关阅读:
    floyd的魔改应用——洛谷P2419 [USACO08JAN]牛大赛Cow Contest 题解
    洛谷P2142 高精度减法 题解
    浅谈SPFA——洛谷P1576 最小花费 题解
    洛谷P1301 魔鬼之城 题解
    洛谷P1009 阶乘之和 题解
    20200926模拟
    [NOIP 2013]货车运输
    带权并查集--P2024 [NOI2001]食物链
    归并排序/树状数组求逆序对-lgP1908 逆序对
    LCA模块+求树上两点距离最短
  • 原文地址:https://www.cnblogs.com/pshw/p/5365489.html
Copyright © 2020-2023  润新知