HDU1584-A strange lift-最短路(Dijkstra模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548

这个题目很容易让人用广搜。。。无语。。。

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define LL long long
#define inf 1<<29
#define s(a) scanf("%d",&a)
#define CL(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=205;
int n,m,a,b;
int Map[N][N];          //  用于记录图的方向;
int dist[N];            //  用于记录从1到k点的路程;
bool vis[N];            //  用于标记是否走过这条路
void dijkstra()
{
    CL(vis,false);
    for(int i=1;i<=n;i++) dist[i]=inf;
    dist[a]=0;      //  这里需要注意一下,不可以写成是dist[1];因为起始点不一定在1位置;
    while(1){
        int v=-1;
        for(int u=1;u<=n;u++)       //  选取最短路径;
            if(!vis[u]&&(v==-1||dist[u]<dist[v])) v=u;
        if(v==-1) break;    //  没有最短路了,返回;
        vis[v]=true;
        for(int u=1;u<=n;u++)       //  更新最短路径;
            dist[u]=min(dist[u],dist[v]+Map[v][u]);
    }
}
int main()
{
    int t;
    while(~s(n)){
        if(!n) break;
        scanf("%d%d",&a,&b);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++) Map[i][j]=inf;
            Map[i][i]=0;
        }
        for(int i=1;i<=n;i++){
            s(t);
            if(i+t<=n) Map[i][i+t]=1;       //  巧妙的将上下楼梯转换成到达的目的地;
            if(i-t>=1) Map[i][i-t]=1;
        }
        dijkstra();
        if(dist[b]<inf) printf("%d\n",dist[b]);
        else printf("-1\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-29 07:22:00

HDU1584-A strange lift-最短路(Dijkstra模板题)的相关文章

HDU 2544最短路dijkstra模板题

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 33657    Accepted Submission(s): 14617 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

hdu1548 A strange lift(bfs 或Dijkstra最短路径)

1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #define M 205 6 #define INF 0x3f3f3f3f 7 using namespace std; 8 9 int map[M][M]; 10 int d[M], vis[M]; 11 int n; 12 int b, e; 13 14 void Dijkstra(){

Dijkstra模板题图论书p133

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <

hdu1584 A strange lift (电梯最短路径问题)

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

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 2544 最短路 dijkstra模板

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 38804    Accepted Submission(s): 16925 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最

最短路Dijkstra模板

传送门:单源最短路 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int MAXN = 10000+1; 7 const int MAXM = 500000+1; 8 const int INT_MAX = 2147483647; 9 10 struct EDGE{ 11 int next,to,dis; 12 }Edge[MAX

hdu 2544 hdu 1874 Dijkstra 模板题

hdu 2544  求点1到点n的最短路 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long

POJ 1502 MPI Maelstrom (Dijkstra 模板题)

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5877   Accepted: 3654 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic