题目链接: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. BZOJ2527Meteors

    BZOJ2527 整体二分模板题 整体二分: 主要用于解决第K大问题 #include<cstdio> #include<cctype> #include<vector& ...

  2. ios 使用keychain具体方法

    Dictionary  写入: if ([self.currentUserAccount length] > 0) {                                Keycha ...

  3. CUDA 实现JPEG图像解码为RGB数据

    了解JPEG数据格式的人应该easy想到.其对图像以8*8像素块大小进行切割压缩的方法非常好用并行处理的思想来实现.而其实英伟达的CUDA自v5.5開始也提供了JPEG编解码的演示样例.该演示样例存储 ...

  4. 辛星浅析raid

    我们来分析一下影响计算机性能的主要组件,主要就是CPU.主板的总线IO.内存IO.硬盘IO.网卡IO等等,并且如今CPU的性能已经非常好了,可是计算机的总体的IO性能却较低,严重影响了计算机的性能,如 ...

  5. uva 11127(暴力)

    题意:给出一个字符串,包含0.1.*,当中×是能够替换成0或者1的,假设字符串的某个子串S有SSS这种连续反复3次出现,不是Triple-free串,问给出的字符串能够形成多少个非Triple-fre ...

  6. Odoo configuration items

    [options] addons_path = /opt/odoo/server/openerp/addons,/opt/odoo/server/addons admin_passwd = admin ...

  7. Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

    Given a string S and a string T, count the number of distinct subsequences ofT inS. A subsequence of ...

  8. C语言变长数组 struct中char data[0]的用法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 今天在看一段代码时出现了用结构体实现变长数组的写法,一开始因为忘记了这种技术,所以老觉得作者的源码有误,最后经过我深思之后,终于想起以前看过的用s ...

  9. 区分拖曳(drag)和点击(click)事件

    假设页面上有一个a标签: <a href="http://www.google.com">google</a> 现在需要对这个标签进行拖放操作,会发现当拖曳 ...

  10. Linux 系统编程中环境变量的使用

    前言 在 UNIX Like 系统中,存有各类系统/应用程序的环境变量,可通过修改之改变系统/应用程序的执行效果:除此之外,用户还可以定义自己的环境变量,供自己写的程序使用.本文将说明如何在程序中设置 ...