链接:https://ac.nowcoder.com/acm/contest/331/D

题意:

小希现在要从寝室赶到机房,路途可以按距离分为N段,第i个和i+1个是直接相连的,只需要一秒钟就可以相互到达。

炫酷的是,从第i个到第i+2pi+2p个也是直接相连的(其中p为任意非负整数),只需要一秒钟就可以相互到达。

更炫酷的是,有K个传送法阵使得某些u,v之间也是直接相连的,只需要一秒钟就可以相互到达,当然,由于设备故障,可能会有一些u=v的情况发生。

现在小希为了赶路,她需要在最短的时间里从寝室(编号为1)到达机房(编号为N),她不希望到达这N个部分以外的地方(不能到达位置N+1),也不能走到比自己当前位置编号小的地方(比如从5走到3是非法的)。

她想知道最短的时间是多少。

思路:

bfs,先进对传送点,在进入i+2^p的最大点,或者,某个点存在传送门也进队。

感觉是个假算法。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXK = 20; struct Node
{
int _w;
int _step;
Node(int w,int step):_w(w), _step(step){}
};
pair<int, int> C[MAXK]; int Quick_M(int t)
{
int x = 2;
int res = 1;
while (t > 0)
{
if (t&1)
res *= x;
x *= x;
t >>= 1;
}
return res;
} int main()
{
int n,k;
cin >> n >> k;
for (int i = 1;i <= k;i++)
cin >> C[i].first >> C[i].second;
queue<Node> que;
que.emplace(1,0);
while (!que.empty())
{
//cout << 1 << endl;
Node now = que.front();
if (now._w == n)
break;
for (int i = 1;i <= k;i++)
{
if (C[i].first == now._w)
{
if (C[i].second <= now._w)
continue;
que.emplace(C[i].second, now._step + 1);
}
}
for (int i = 0;i <= 30;i++)
{
LL tx = now._w + Quick_M(i);
if (tx > n)
{
que.emplace(now._w + Quick_M(i - 1), now._step + 1);
break;
}
else
{
for (int i = 1;i <= k;i++)
{
if (C[i].first == tx)
{
if (C[i].second <= tx)
continue;
que.emplace(tx, now._step + 1);
}
}
}
}
que.pop();
}
cout << que.front()._step << endl; return 0;
}

  

最新文章

  1. BPM实例分享——金额规则大写
  2. js事件(Event)知识整理
  3. python-网络编程-socket编程
  4. webuploader跨域上传
  5. Java菜鸟学习 Script 脚本语言(简介)
  6. [ubuntu14.04 amd64 ]搜狗拼音輸入法安裝
  7. 三相异步电动机过载保护及报警PLC控制
  8. [Bootstrap] 7. Working Herader
  9. linux中/etc与/var目录,各是什么意思?这两个目录下的文件有什么特点?
  10. Directx11学习笔记【十】 画一个简单的三角形
  11. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(18)-过滤器的使用和批量删除数据(伪删除和直接删除)
  12. Azure IoT Hub和Event Hub相关的技术系列-索引篇
  13. hdu 6093---Rikka with Number(计数)
  14. Build up java environment(配置java环境)
  15. php unset对json_encode的影响
  16. springBoot的事物管理
  17. shiro-redis实现session存储到redis
  18. link &amp; auto cards
  19. URL编码表、Base64编码表、HTTP消息含义
  20. The value for the useBean class attribute is invalid.

热门文章

  1. android——实现多语言支持
  2. windows server 2008 + IIS 7.5实现多用户FTP(多账号对应不同目录
  3. android DHCP流程【转】
  4. codeforces 的 Codeforces Round #273 (Div. 2) --C Table Decorations
  5. yum的配置文件yum.conf详解
  6. windows下的host文件在哪里,有什么作用?
  7. codeforces 462C Appleman and Toastman 解题报告
  8. html5--4-4 audio元素/格式的转换
  9. (QA-LSTM)自然语言处理:智能问答 IBM 保险QA QA-LSTM 实现笔记.md
  10. Servlet执行过程