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;
} 
目录
相关文章
|
8月前
E. Nearest Opposite Parity(多源最短路bfs)
E. Nearest Opposite Parity(多源最短路bfs)
|
8月前
|
vr&ar C++
B. Fake Plastic Trees(贪心+dp)
B. Fake Plastic Trees(贪心+dp)
|
8月前
G - Prime Ring Problem(深搜)
G - Prime Ring Problem(深搜)
CF1272 E.Nearest Opposite Parity(反向建图+BFS)
CF1272 E.Nearest Opposite Parity(反向建图+BFS)
117 0
CF1272 E.Nearest Opposite Parity(反向建图+BFS)
|
存储 算法
Prim
复习acwing算法基础课的内容,本篇为讲解基础算法:Prim,关于时间复杂度:目前博主不太会计算,先鸽了,日后一定补上。
153 0
Prim
【欧拉计划第 3 题】最大质因数 Largest prime factor
【欧拉计划第 3 题】最大质因数 Largest prime factor
269 0
Sticks(剪枝+BFS)
Problem Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long.
1419 0
1046. Shortest Distance (20)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.
970 0
|
算法
Prim Algoritm(最小生成树)
Prim Algorithm。这个算法可以分为下面几个步骤: 将顶点集V分成两个集合A和B,其中集合A表示目前已经在MST中的顶点,而集合B则表示目前不在MST中的顶点。 在B寻找与集合A连通的最短的边(u,v),将这条边加入最小生成树中。
869 0