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记录-SpringMVC scope属性的两种模式
singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对Bean的请求,只要id与该Bean定义相匹配,则只 ...
- HDU - 3980 Paint Chain(SG函数)
https://vjudge.net/problem/HDU-3980 题意 一串长度为n的柱子,每个人只能给连续的珠子涂色,涂过的不能再涂,不能涂的人就输了,问最后谁获胜. 分析 第一个人先涂m个, ...
- 浅谈分词算法(4)基于字的分词方法(CRF)
目录 前言 目录 条件随机场(conditional random field CRF) 核心点 线性链条件随机场 简化形式 CRF分词 CRF VS HMM 代码实现 训练代码 实验结果 参考文献 ...
- C++ STL sort()函数用法
C++STL提供的在里的排序函数,有以下两种形式 此外还提供有稳定排序版本stable_sort(),用法类似. 第一种形式: template <class RandomAccessItera ...
- 25. Spring Boot与缓存 JSR-107、Spring缓存抽象
JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry和Expiry. CachingProvider ...
- 【洛谷P1896【SCOI2005】】互不侵犯King
题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 输入输出格式 输入格式: 只有一行,包 ...
- Pytorch中的torch.cat()函数
cat是concatnate的意思:拼接,联系在一起. 先说cat( )的普通用法 如果我们有两个tensor是A和B,想把他们拼接在一起,需要如下操作: C = torch.cat( (A,B),0 ...
- 【逆向笔记】OD工具使用-逆向TraceMe.exe
名词注释 System breakpoint:系统断点,OllyDbg用CreateProcessA加载DEBUG_ONLY_THIS_PROCESS参数执行,程序运行之后会触发一个INT13,在系统 ...
- [转]HEX文件格式解析
1.前言 本文主要讲述keil MDK 下STM32编译生成的的HEX镜像文件格式.并说明镜像load地址是如何添加进HEX文件的. 2.keil MDK如何在HEX文件中添加load addr 通过 ...
- oracle数据库自增主键重复
select max(t.id) from T_PLAT_ENUM_VALUE tdrop sequence T_PLAT_ENUM_VALUE;create sequence T_PLAT_ENUM ...