题目大意:有一个升降机,它有两个按钮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. golang结构体

    声明结构体 定义结构体使用struct关键字.在结构体内部定义它们的成员变量和类型.如果成员变量的类型相同还可以把它们写到同一行. struct里面可以包含多个字段(属性) struct类型可以定义方 ...

  2. SQL语句(一)SQL和数据库数据表的创建

    SQL的组成 (1) 数据定义语言DDL(Data Definition Language) 用于数据库和数据表的创建.修改和删除等操作 CREATE (create) 创建数据库.数据表 ALTER ...

  3. pyinstaller 打包不成功,提示inporterror 缺少xlrd、xlwt

    问题:pyinstaller 打包不成功,提示inporterror 缺少xlrd.xlwt 解决:将 pypiwin 230 改为 219

  4. entity framework 时间操作

    ).FirstOrDefault(); if (useractiveentity == null) { UserActive userActive = new UserActive(); userAc ...

  5. android AsyncHttpClient使用

    1.www.github.com下载jar包 loopj/android-async-http 将下载好的文件导入项目中 2.main.xml <?xml version="1.0&q ...

  6. django学习~第四篇

    django表单   1  今天继续来学学django的表单       首先介绍下http的方法,这是最基本的       GET 方法 GET一般用于获取/查询 资源信息,以?分割URL和传输数据 ...

  7. jquery 学习(六) - 事件绑定

    HTML <!--绑定事件--> <div class="a1"> <button class="bt">按钮</bu ...

  8. python - 包装 和 授权

    包装 # 包装(二次加工标准类型) # 继承 + 派生 的方式实现 定制功能 # 示例: # class list_customization(list): #重新定制append方法,判断添加的数据 ...

  9. 【Linux】Linux中Swap与Memory内存简单介绍

    背景介绍 对于Linux来说,其在服务器市场的使用已经占据了绝对的霸主地位,不可动摇.Linux的各种设计思想和使用也被传承(当然不乏各种黑Linux,而且黑的漂亮).Linux的很多独特的设计,对性 ...

  10. 用struts2 s2-045漏洞拿站记录

    浏览FreeBuf时发现的文章,新出的漏洞: http://www.freebuf.com/vuls/128668.html 漏洞一出,各位大神早就写出POC: http://www.reg008.c ...