链接:

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. 前端中常见字节编码(base64、hex、utf8)及其转换

    /* * 字节编码转换 * 首先都需要转为二级制数组 (ArrayBuffer) * 然后才能转换对应的编码字符 * 前端常见编码: * base64:就是将二进制转为字符串,将每6个字节转为一个特定 ...

  2. Java生产消费者模型——代码解析

    我们将生产者.消费者.库存.和调用线程的主函数分别写进四个类中,通过抢夺非线程安全的数据集合来直观的表达在进行生产消费者模型的过程中可能出现的问题与解决办法. 我们假设有一个生产者,两个消费者来共同抢 ...

  3. [终极巨坑]golang+vue开发日记【二】,登陆界面制作(一)

    写在前面 本期内容是适合第一次使用vue或者golang开发的,内容会以实战的形式来讲解.看懂本段内容需要了解基础内容有html,css,最好可以看一下vue的基础.并且这里的每个知识点不可能详细解说 ...

  4. chrome Network 过滤和高级过滤

    转自:https://blog.csdn.net/tengdazhang770960436/article/details/90644523

  5. 构建helm chart应用

    使用helm命令创建基础目录 helm create t2cp [root@node04 ~]# tree t2cp t2cp ├── charts ├── Chart.yaml ├── templa ...

  6. The driver is automatically registered via the SPI and manual loading of the driver class....

    Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdb ...

  7. 学习docker 部署nginx记录

    docker pull nginx $ docker pull nginx $ docker run --name nginx-test -p 8081:80 -d nginx docker conf ...

  8. ubuntu16.04 打开chrome弹出“Enter password to unlock your login keyring”解决方法

    问题如图 输入开机密码发现验证失败. 解决 命令: find ~/ -name login.keyring 查找相关文件. 命令: sudo rm -rf /home/la/.local/share/ ...

  9. C# vb .net实现透视图效果滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的透视图效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一 ...

  10. DOS之cd命令

    cd命令是改变子目录的命令, 下面是cd命令的常见用法 cd/?可显示帮助信息 cd 返回到当前所在的根目录中 cd.. 返回上一层目录 cd +目录名,进入下一层目录 驱动器:,进入到另一个驱动器 ...