Nearest Opposite Parity最短路径+超级源点

简介: time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou 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).

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output


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).


微信图片_20220607175024.png微信图片_20220607175024.png

微信图片_20220607175024.png

input


10
4 5 7 6 7 5 4 4 6 4


output


1 1 1 2 -1 1 1 3 1 1 


/// spfa最最短路,建立两个超级源点,一个连接奇数点一个链接偶数点,在建边的时候,边权设置为 1

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e6 + 7;
typedef int itn;
int n;
int a[maxn];
struct Node{
  int u;
  int v;
  int next;
  int w;
}edge[maxn]; 
int cnt;
int head[maxn];
int dis[maxn];
itn ans[maxn];
itn vis[maxn];
void _Init(){
  cnt = 0;
  for(int i=0;i<maxn;i++) head[i] = -1;
}
void _Add(int u,int v){
  edge[cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].w = 1;
  edge[cnt].next = head[u];
  head[u] = cnt ++;
}
void _SPFA(int u){
  for(int i=1;i<maxn;i++){
    dis[i] = 0x3f3f3f3f;
    vis[i] = 0;
  }
  dis[u] = 0;
  vis[u] = 1;
  queue<int> que;
  ///cout << que.size() <<endl;
  que.push(u);
  while(que.size()){
    int t = que.front();
    que.pop();
    vis[t] = 0;
    for(int i = head[t]; ~i;i = edge[i].next){
      int to = edge[i].v;
      if(dis[to] > dis[t] + edge[i].w){
        dis[to] = dis[t] + edge[i].w;
        if(vis[to] == 0){
          que.push(to);
          vis[to] = 1;
        } 
      }
      ///puts("ok");
    }
  } 
}
int main(){
  cin >> n;
  _Init();
  for(int i=1;i<=n;i++) cin >> a[i];
  for(int i=1;i<=n;i++){
    if(i - a[i] >= 1) _Add(i - a[i],i);
    if(i + a[i] <= n) _Add(i + a[i],i);
    if(a[i] % 2 == 0) _Add(n+1,i);
    else _Add(n+2,i);
  }
  _SPFA(n + 1);
  ///puts("okay");
  for(int i=1;i<=n;i++){
    if(a[i] % 2) ans[i] = dis[i];
  }
  _SPFA(n + 2);
  for(int i=1;i<=n;i++){
    if(a[i] % 2 == 0) ans[i] = dis[i];
  }
  for(int i=1;i<=n;i++){
    if(ans[i] == 0x3f3f3f3f) printf("-1 ");
    else printf("%d ",ans[i] - 1);
  }
  ///cout << 0x3f3f3f3f <<endl;
  return 0;
} 
目录
相关文章
|
1月前
|
机器学习/深度学习 算法 PyTorch
K-Nearest Neighbors
【10月更文挑战第02天】
45 5
|
6月前
E. Nearest Opposite Parity(多源最短路bfs)
E. Nearest Opposite Parity(多源最短路bfs)
|
6月前
|
算法
Heavy Transportation(Dijkstra算法)
Heavy Transportation(Dijkstra算法)
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
|
算法 索引
LeetCode 214. Shortest Palindrome
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
86 0
LeetCode 214. Shortest Palindrome
CF1272 E.Nearest Opposite Parity(反向建图+BFS)
CF1272 E.Nearest Opposite Parity(反向建图+BFS)
103 0
CF1272 E.Nearest Opposite Parity(反向建图+BFS)
|
SQL 关系型数据库 MySQL
LeetCode 613. Shortest Distance in a Line
LeetCode 613. Shortest Distance in a Line
98 0
|
算法
粒子滤波 PF(Particle filter)算法
粒子滤波 PF(Particle filter)算法
664 2
|
并行计算 算法 搜索推荐
你管这叫 PageRank 算法
算法简介 核心思想 算法原理 算法分析 算法改进
你管这叫 PageRank 算法
【1046】Shortest Distance (20 分)
【1046】Shortest Distance (20 分) 【1046】Shortest Distance (20 分)
91 0