题目链接:http://codeforces.com/gym/101147/problem/E

题意:当人在第i个商店时,他可以向左或向右跳di段距离到达另一个商店(在范围之内),一个商店为一段距离。问:对于每一个商店,跳到最后一个商店最少需要跳几次?

题解:题目实际上是求最短距离,而且边权为1,所以可以直接用bfs。由于是求每个点到最后一个点的最短距离,那么可以反向建图,将最后一个点设为起始点,然后向前跑。对于跑不到的点,回到题目上说,实际就是这个商店不能到达最后一个商店。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = ; vector <int > v[maxn];
LL len[maxn]; struct node
{
int sta;
LL k;
};
queue<node>q;
node now, e; int vis[maxn];
int bfs(int be)
{
ms(vis,);
while(!q.empty())
q.pop(); now.sta = be;
now.k = ;
vis[be] = ;
q.push(now);
len[be] = ;
while(!q.empty())
{
now = q.front();
q.pop(); int big = v[now.sta].size();
for(int i = ; i<big; i++)
{
if(!vis[ v[now.sta][i] ])
{
e.sta = v[now.sta][i] ;
e.k = now.k+;
len[e.sta] = e.k; vis[e.sta ] = ;
q.push(e);
}
}
}
return ;
} void solve()
{
int n;
scanf("%d", &n);
for(int i = ; i<=n; i++)
v[i].clear();
for(int i=;i<=n;i++)
{
int d;
scanf("%d", &d);
if(i-d>=) v[i-d].pb(i);//反向建图
if(i+d<=n) v[i+d].pb(i);
} ms(len,-);
bfs(n);
for(int i = ; i<=n; i++)
printf("%lld\n",len[i]);
} int main()
{
#ifdef LOCAL
freopen("jumping.in", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL int T;
scanf("%d", &T);
while(T--){
solve();
} return ;
}

Gym - 101147E E. Jumping —— bfs的更多相关文章

  1. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  2. Gym - 101572D Distinctive Character bfs 思维

    题目传送门 题目大意: 给出n个01串,让你构造一个字符串,使这个字符串和这些字符串中相似程度最高 尽可能低.如果两个字符串对应位置相同,则相似程度加一. 思路: 每一个01串更改自己的一部分后,都可 ...

  3. Security Guards (Gym - 101954B)( bfs + 打表 )

    题意及思路 题目主要是讲先给出所有guard的位置,再给出所有incidents的位置,求出guard到达每个incident处最小的steps,其中guard每次可以向四周8个方向移动. 思路:对于 ...

  4. Gym - 101617D_Jumping Haybales(BFS)

    Sample Input 4 2 .### #... .#.. #.#. 3 1 .#. .#. .#. Sample Output 4 -1 题意:给一个n*n的图,每次最多能跳k个格子,只能向南( ...

  5. Gym 100971A Treasure Island BFS 思维题

    A - Treasure Island Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  6. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  7. Codeforces Gym 100187E E. Two Labyrinths bfs

    E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...

  8. Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】

    F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...

  9. Gym 101617J Treasure Map(bfs暴力)

    http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...

随机推荐

  1. 洛谷—— P2183 巧克力

    https://www.luogu.org/problemnew/show/P2183 题目描述 佳佳邀请了M个同学到家里玩.为了招待客人,她需要将巧克力分给她的好朋友们.她有N(1<=N< ...

  2. commons-lang3工具类学习--------ObjectUtils

    Object工具类 allNotNull(Object... values) 检查所有元素是否为空,返回一个boolean 如果有一个元素为空返回false,所有元素不为空或元素为empty返回tru ...

  3. SQL SERVER 内存

    http://www.cnblogs.com/CareySon/archive/2012/08/16/HowSQLServerManageMemory.html

  4. 【webpack2】-- 入门与解析

    每次学新东西总感觉自己是不是变笨了,看了几个博客,试着试着就跑不下去,无奈只有去看官方文档. webpack是基于node的.先安装最新的node. 1.初始化 安装node后,新建一个目录,比如ht ...

  5. win10安装scrapy

    前提已经安装好python2.7背景:scrapy框架,windows下的部署工作挺麻烦的.需要用的资源整合了一下可以为以后需要的同学节省不少时间. 相关文件:网盘链接: http://pan.bai ...

  6. 如何突破Windows环境限制打开“命令提示符”

    如今,许多企业或组织都会通过使用受限的windows环境来减少系统表面的漏洞.系统加固的越好,那么也就意味着能被访问和使用到的功能就越少. 我最近遇到的情况是,一个已经加固的系统同时受到McAfee ...

  7. python matplotlib包图像配色方案

    可选的配色方案: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_ ...

  8. hdu5373

    题先附上:水题,可是思路不正确,特easy超时(TLE) The shortest problem Time Limit: 3000/1500 MS (Java/Others)    Memory L ...

  9. sonar + ieda实现提交代码前代码校验

    代码风格不同一直是一件停头疼的事情,因为不同的工作经验,工作经历,每个人的代码风格不尽相同,造成一些代码在后期的维护当中难以维护, 查阅一些资料之后发现 idea + sonar 的方式比较适合我,实 ...

  10. 定义自己的代码风格CheckStyle简单使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-/ ...