Gym - 101147E E. Jumping —— bfs
题目链接: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的更多相关文章
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- Gym - 101572D Distinctive Character bfs 思维
题目传送门 题目大意: 给出n个01串,让你构造一个字符串,使这个字符串和这些字符串中相似程度最高 尽可能低.如果两个字符串对应位置相同,则相似程度加一. 思路: 每一个01串更改自己的一部分后,都可 ...
- Security Guards (Gym - 101954B)( bfs + 打表 )
题意及思路 题目主要是讲先给出所有guard的位置,再给出所有incidents的位置,求出guard到达每个incident处最小的steps,其中guard每次可以向四周8个方向移动. 思路:对于 ...
- Gym - 101617D_Jumping Haybales(BFS)
Sample Input 4 2 .### #... .#.. #.#. 3 1 .#. .#. .#. Sample Output 4 -1 题意:给一个n*n的图,每次最多能跳k个格子,只能向南( ...
- Gym 100971A Treasure Island BFS 思维题
A - Treasure Island Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- Codeforces Gym 100187E E. Two Labyrinths bfs
E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...
- 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 ...
- Gym 101617J Treasure Map(bfs暴力)
http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...
随机推荐
- VMware虚拟机直连物理网络的两种方式
VMware虚拟机直连物理网络的两种方式 使用VMware构建虚拟机,通常虚拟机都使用NAT模式.这时,虚拟机有独立的网段.使用NAT模式,虚拟机之间数据都通过虚拟网络传输,不会影响实体机所在的实 ...
- 用LCT解一类动态图的问题
很显然,学过了LCT,大家一定都会用LCT来维护动态树结构了 那么,遇到图问题的时候,是不是也能用lct来解决呢? 解决图问题的时候,我们必须要仍然维护一棵树的形态,否则,lct是做不动的 那么下面来 ...
- List遍历时删除遇到的问题
这周在开发中遇到了一个以前没遇到的小Bug,在这里记录下来. List集合,我们平时都经常使用.但是,我在遍历List集合时,调用了List集合的remove方法来删除集合中的元素,简单的代码结构是这 ...
- 唤醒你的大脑 --- javascript冒泡排序
var a; a = [1, 2, 3, 11, 55, 5, 0, 44]; (function bubbleSort() { for (var i = 0; i <= a.length - ...
- XCode 4.3 Unable to load persistent store UserDictionary.sqlite 以及 ios simulator failed to install the application
I have been working on an iOS app for some time, all of a sudden I am getting the following crash ev ...
- 【spring cloud】Feign使用填坑
引用地址:https://blog.csdn.net/liuchuanhong1/article/details/54728681 问题一: 在前面的示例中,我们讲过 @RequestMapping( ...
- mac os+selenium2+Firefox驱动+python3
此文章建立在之前写的chrome+selenium+Python环境配置的基础上,链接http://blog.csdn.net/zxy987872674/article/details/5308289 ...
- dom4j的xpath查找xml的指定节点
递归遍历所有节点http://blog.csdn.net/sidihuo/article/details/47318723 获取Document SAXReader saxReader = new S ...
- web图片转换小工具制作
HTML <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- 使用squid快速搭建代理
shadowsocks停止维护,如何使用squid快速搭建代理 =======本项目主要介绍如何利用国外VPS搭建多协议代理服务.GFW 封锁了 HTTP/Socks5 代理,HTTP 代理是关键 ...