链接:

https://codeforces.com/contest/1272/problem/E

题意:

You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i−ai (if 1≤i−ai) or to the position i+ai (if i+ai≤n).

For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that aj has the opposite parity from ai (i.e. if ai is odd then aj has to be even and vice versa).

思路:

写了好久DFS发现有环不能处理。。看了大佬博客才懂。

考虑从a开始的最短路,计算的是a到x的最短路。

反向建图,跑出来的最短路,就是他能到达的点往自己的最短路。

建立两个新点,分别连奇数点和偶数点,跑最短路。

代码:

#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAXN = 2e5+10; vector<int> G[MAXN];
int a[MAXN], ans[MAXN], dis[MAXN], vis[MAXN];
int n; void SPFA(int s)
{
queue<int> que;
for (int i = 1;i <= n+2;i++)
dis[i] = INF, vis[i] = 0;
que.push(s);
dis[s] = 0;
vis[s] = 1;
while(!que.empty())
{
int u = que.front();
que.pop();
vis[u] = 0;
for (int i = 0;i < (int)G[u].size();++i)
{
int node = G[u][i];
if (dis[node] > dis[u]+1)
{
dis[node] = dis[u]+1;
if (vis[node] == 0)
{
que.push(node);
vis[node] = 1;
}
}
}
}
} int main()
{
cin >> n;
for (int i = 1;i <= n;++i)
cin >> a[i];
for (int i = 1;i <= n;++i)
{
if (i-a[i] >= 1)
G[i-a[i]].push_back(i);
if (i+a[i] <= n)
G[i+a[i]].push_back(i);
}
for (int i = 1;i <= n;++i)
{
if (a[i]%2 == 1)
G[n+1].push_back(i);
else
G[n+2].push_back(i);
}
SPFA(n+1);
for (int i = 1;i <= n;++i)
if (a[i]%2 == 0) ans[i] = dis[i];
SPFA(n+2);
for (int i = 1;i <= n;++i)
if (a[i]%2 == 1) ans[i] = dis[i];
for (int i = 1;i <= n;++i)
cout << ((ans[i] == INF) ? -1 : ans[i]-1) << ' ' ;
cout << endl; return 0;
}

Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)的更多相关文章

  1. Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity

    题目链接:http://codeforces.com/contest/1272/problem/E 题意:给定n,给定n个数a[i],对每个数输出d[i]. 对于每个i,可以移动到i+a[i]和i-a ...

  2. Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity (超级源点)

  3. Codeforces Round #605 (Div. 3)

    地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...

  4. Codeforces Round #605 (Div. 3) 题解

    Three Friends Snow Walking Robot Yet Another Broken Keyboard Remove One Element Nearest Opposite Par ...

  5. Codeforces Round #172 (Div. 2) B. Nearest Fraction 二分

    B. Nearest Fraction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/281/p ...

  6. 【cf比赛记录】Codeforces Round #605 (Div. 3)

    比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...

  7. Codeforces Round #605 (Div. 3) D. Remove One Element(DP)

    链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...

  8. Codeforces Round #605 (Div. 3) C. Yet Another Broken Keyboard

    链接: https://codeforces.com/contest/1272/problem/C 题意: Recently, Norge found a string s=s1s2-sn consi ...

  9. Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)

    链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...

随机推荐

  1. Linux crontab 查看所有用户的crontab任务

    查看所有用户的crontab任务 - mlzhu007的专栏 - CSDN博客 https://blog.csdn.net/mlzhu007/article/details/81662091 以roo ...

  2. 通过fsockopen()方法从中国福彩网获取双色球历史中奖数据

    # 以下代码基于 CI 框架 # public function history_draw($page = 0) { set_time_limit(0); $page++; $up2now = dat ...

  3. Go基础编程实践(八)—— 系统编程

    捕捉信号 // 运行此程序,控制台将打印"Waiting for signal" // 按Ctrl + C 发送信号以关闭程序,将发生中断 // 随后控制台依次打印"Si ...

  4. (十一)pdf的构成之文件尾

    件尾部(trailer)如何找到交叉引用表和其他特殊对象 属性: / Size [integer]:指定交叉引用表中的条目数(也计算更新部分中的对象).使用的数字不应是间接参考. / Prev [in ...

  5. golang 之文件操作

    文件操作要理解一切皆文件. Go 在 os 中提供了文件的基本操作,包括通常意义的打开.创建.读写等操作,除此以外为了追求便捷以及性能上,Go 还在 io/ioutil 以及 bufio 提供一些其他 ...

  6. nginx通过自定义header属性来转发不同的服务

    一.背景 因为需要上线灰度发布,只要nginx接收到头部为: wx_unionid: 就会跳转到另外一个url,比如: 通过配置nginx 匹配请求头wx_unionid 来转发到灰度环境.核心:客户 ...

  7. 使用KONG网关实现接口迁移的灰度验证

    在我们对一个API站点进行微服务化的过程中,使用KONG网关可以实现以下几个效果: 1. 业务线无感知,其实内部已经被Kong转到其他站点上执行了,这对业务线特别友好. 2. 可以实现租户级/接口级灰 ...

  8. Python进阶(十五)----面向对象之~继承(单继承,多继承MRO算法)

    Python进阶(十五)----面向对象之~继承 一丶面向对象的三大特性:封装,继承,多态 二丶什么是继承 # 什么是继承 # b 继承 a ,b是a的子类 派生类 , a是b的超类 基类 父类 # ...

  9. java基本结构

    前言 Java文件的运行过程: 1,javac.exe:编译器 2,java.exe:解释器 微软shell下运行实例: C:\Users\Administrator>cd D:\文档\JAVA ...

  10. js-Date对象(九)

    一.Date对象的创建1.new Date()[创建当前时间对象]eg: var date = new Date(); console.log(date); //Thu Jul 18 2019 18: ...