题目大意:有一个升降机,它有两个按钮UP和DOWN,给你一些数i表示层数,并且每层对应的Ki,如果按UP按钮,会从第i层升到第i+Ki层;如果按了DOWN则会从第i层降到第i-Ki层;并规定能到的层数为1到N,现在的要求就是给你N,A,B和一串数K1到Kn,问你从A到B,至少按几下按钮。

构造一个图,边的权值为1

Sample Input
5 1 5 //层数 起点 终点
3 3 1 2 5
0

Sample Output
3

Dijkstra:(O(n^2))

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; const int MAXN=;
const int INF=0x3f3f3f3f;
int n ;
bool vis[MAXN];
int cost[MAXN][MAXN] ;
int lowcost[MAXN] ;
int pre[MAXN];
void Dijkstra(int beg)
{
for(int i=;i<n;i++)
{
lowcost[i]=INF;vis[i]=false;pre[i]=-;
}
lowcost[beg]=;
for(int j=;j<n;j++)
{
int k=-;
int Min=INF;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-)
break ;
vis[k]=true;
for(int i=;i<n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
pre[i]=k;
}
} } int main ()
{
// freopen("in.txt","r",stdin) ; while (scanf("%d" , &n ) !=EOF)
{
if (n==)
break ;
int i , j ;
for (i = ; i < n ; i++)
for (j = ; j < n ; j++)
cost[i][j] = INF ;
int s , e , t;
scanf("%d %d" , &s , &e) ;
for (i = ; i < n ; i++)
{
scanf("%d" , &t) ;
if (i + t <= n-)
cost[i][i+t] = ;
if (i - t >= )
cost[i][i-t] = ;
}
Dijkstra(s-) ;
if (lowcost[e-] != INF)
printf("%d\n" , lowcost[e-]) ;
else
printf("-1\n") ;
} return ;
}

堆优化:

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <queue>
# define LL long long
using namespace std ; const int INF=0x3f3f3f3f;
const int MAXN=;
struct qnode
{
int v;
int c;
qnode(int _v=,int _c=):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
int v,cost;
Edge(int _v=,int _cost=):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
int n ;
void Dijkstra(int start)//点的编号从1开始
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=;
que.push(qnode(start,));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=;i<E[u].size();i++)
{
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
} int main ()
{
// freopen("in.txt","r",stdin) ;
int m ;
while (scanf("%d" , &n) !=EOF)
{
if (n== )
break ;
int u , v , w ;
int i , j ;
for(i=;i<=n;i++)
E[i].clear(); int s , e , t;
scanf("%d %d" , &s , &e) ;
for (i = ; i <= n ; i++)
{
scanf("%d" , &t) ;
if (i + t <= n)
addedge(i,i+t,) ;
if (i - t >= )
addedge(i,i-t,) ;
}
Dijkstra(s) ;
if (dist[e] != INF)
printf("%d\n" , dist[e]) ;
else
printf("-1\n") ;
} return ;
}

hdu 1548 升降梯的更多相关文章

  1. cogs 364. [HDU 1548] 奇怪的电梯 Dijkstra

    364. [HDU 1548] 奇怪的电梯 ★   输入文件:lift.in   输出文件:lift.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 呵呵,有一天我做了 ...

  2. hdu 1548 楼梯 bfs或最短路 dijkstra

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...

  3. hdu 1548 A strange lift

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

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

  5. HDU 1548 A strange lift (Dijkstra)

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

  6. hdu 1548 A strange lift (dijkstra算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几 ...

  7. hdu 1548 A strange lift 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目意思:给出 n 个 floor 你,每个floor 有一个数k,按下它可以到达 floor ...

  8. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

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

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

随机推荐

  1. CM记录-选择合适的硬件

    hadoop的运行环境---hadoop/jvm/os/hw 原则1:主节点的可靠性要好于从节点:NameNode(Yarn-ResourceManager).DataNode(NodeManager ...

  2. JAVA锁的优化和膨胀过程

    转自:https://www.cnblogs.com/dsj2016/p/5714921.html https://cloud.tencent.com/developer/article/103675 ...

  3. Neural Networks and Deep Learning(week4)Building your Deep Neural Network: Step by Step

    Building your Deep Neural Network: Step by Step 你将使用下面函数来构建一个深层神经网络来实现图像分类. 使用像relu这的非线性单元来改进你的模型 构建 ...

  4. Nginx实现数据库端口转发

    前言 因开发.测试.生成等服务器网络策略问题,导致部分服务器A需要访问数据库而无法正常访问数据库,此处采用端口代理方式解决此问题,即通过一台能正常访问数据库的服务器B做tcp端口代理,实现服务器A通过 ...

  5. C# 实现立体图形变换(vs2008)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. IIC AT24C02读写数据的一点小体会

    一.写数据 unsigned char I2CWriteByte(unsigned int mem_addr,unsigned char*DDATAp,unsigned int count) { u8 ...

  7. Linux - 网络检测

    linux 利用bmon/nload/iftop/vnstat/iptraf实时查看网络带宽状况 .添加yum源方便安装bmon # rpm -Uhv http://apt.sw.be/redhat/ ...

  8. B - 低阶入门膜法 - D-query (查询区间内有多少不同的数)

    题目链接:https://cn.vjudge.net/contest/284294#problem/B 题目大意:查询区间内有多少个不相同的数. 具体思路:主席树的做法,主席树的基础做法是查询区间第k ...

  9. Caffe2 Detectron安装错误记录

    caffe2 caffe2的安装方法有几种.其中最方便的是conda install.但是要求必须安装Anaconda. conda install -c caffe2 caffe2-cuda8.0- ...

  10. python模块分析之typing(三)

    前言:很多人在写完代码一段时间后回过头看代码,很可能忘记了自己写的函数需要传什么参数,返回什么类型的结果,就不得不去阅读代码的具体内容,降低了阅读的速度,加上Python本身就是一门弱类型的语言,这种 ...