Tap here to switch tabs
Problem
Submissions

Problem: Shortest Path with Alternating Colors

Try to solve the Shortest Path with Alternating Colors problem.

Statement

You are given an integer n representing the number of nodes in a directed graph, with nodes labeled from 00 to n −1- 1. You are also given two 22D integer arrays, redEdges and blueEdges, where:

  • redEdges[i] = [a_i, b_i] indicates a directed red edge from node a_i to node b_i.

  • blueEdges[j] = [u_j, v_j] indicates a directed blue edge from node u_j to node v_j.

Find the shortest path from node 00 to each node x such that the colors of the edges along the path strictly alternate between red and blue.

Return an array answer of length n, where answer[x] is the length of the shortest alternating path from node 00 to node x, or −1-1 if no valid path exists.

Constraints:

  • 1≤1 \leq n ≤100\leq 100

  • 0≤0 \leq redEdges.length, blueEdges.length ≤400\leq 400

  • redEdges[i].length ==== blueEdges[j].length ==2== 2

  • 0≤0 \leq a_i, b_i, u_j, v_j << n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Shortest Path with Alternating Colors

Try to solve the Shortest Path with Alternating Colors problem.

Statement

You are given an integer n representing the number of nodes in a directed graph, with nodes labeled from 00 to n −1- 1. You are also given two 22D integer arrays, redEdges and blueEdges, where:

  • redEdges[i] = [a_i, b_i] indicates a directed red edge from node a_i to node b_i.

  • blueEdges[j] = [u_j, v_j] indicates a directed blue edge from node u_j to node v_j.

Find the shortest path from node 00 to each node x such that the colors of the edges along the path strictly alternate between red and blue.

Return an array answer of length n, where answer[x] is the length of the shortest alternating path from node 00 to node x, or −1-1 if no valid path exists.

Constraints:

  • 1≤1 \leq n ≤100\leq 100

  • 0≤0 \leq redEdges.length, blueEdges.length ≤400\leq 400

  • redEdges[i].length ==== blueEdges[j].length ==2== 2

  • 0≤0 \leq a_i, b_i, u_j, v_j << n