hdu 1548 升降梯
题目大意:有一个升降机,它有两个按钮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 升降梯的更多相关文章
- cogs 364. [HDU 1548] 奇怪的电梯 Dijkstra
364. [HDU 1548] 奇怪的电梯 ★ 输入文件:lift.in 输出文件:lift.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 呵呵,有一天我做了 ...
- hdu 1548 楼梯 bfs或最短路 dijkstra
http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- 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 ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- hdu 1548 A strange lift (dijkstra算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几 ...
- hdu 1548 A strange lift 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目意思:给出 n 个 floor 你,每个floor 有一个数k,按下它可以到达 floor ...
- 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 ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
随机推荐
- JAVA记录-java代码优化策略
java代码优化策略 1.生成对象时,合理分配空间和大小:new ArrayList(100); 2.优化for循环: Vector vect = new Vector(1000); For(int ...
- CM记录-JVM调优
1.堆栈大小 2.JVM重用 3.GC
- Neural Networks and Deep Learning 课程笔记(第三周)浅层神经网络(Shallow neural networks)
3.1 神经网络概述(Neural Network Overview ) (神经网络中,我们要反复计算a和z,最终得到最后的loss function) 3.2 神经网络的表示(Neural Netw ...
- Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程
待解决的问题 Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程 解决办法 为spring session添加spr ...
- centos7 pgpool+postgresql
安装postgresql CentOS7安装并配置PostgreSQL 安装pgpool rpm -ivh http://www.pgpool.net/yum/rpms/3.7/redhat/rhel ...
- 第k个素数
题目描述 Output the k-th prime number. 输入描述: k≤10000 输出描述: The k-th prime number. #include <iostream& ...
- Python中的包ImportError
前言 Python中的包给我提供了很好的代码组织,相似的功能模块放在同一个包内,不仅代码结构清晰,而且调用起来也比较方便(可以用*导入) 但是,我们在刚开始使用Python包的时候总是会遇到导入错误& ...
- 表格重新加载 where 携带上次值问题
表格重载两种方式: 方式一: tableIns.reload(options) 注意这种方式的重载是不会携带上次数据加载时的where值 //使用 第一次渲染返回的对象 var table ...
- 数据库的一致性读,赃读,多线程与赃读,ACID,UNDO
赃读 对于对象额同步异步方法,我们在设计自己的程序的时候,一定要考虑的问题整体,不然会出现数据不一致的错误,很经典的就是赃读(dityread) 示例: package com.nbkj.thre ...
- 绘图QPainter-画刷
Qt提供的画刷风格: Qt.TexturePattern 自定义图像画刷 线性渐变 QLinearGradientPattern QLinearGradient需要传入的参数为需要进行渐变的区域坐 ...