1601B - Frog Traveler - CodeForces Solution


data structures dfs and similar dp graphs shortest paths two pointers *1900

Please click on ads to support us..

Python Code:

from collections import deque

n = int(input())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))


def find_path_to_zero():
    paths = {n: (n, n)}
    queue = deque([n])
    curr_min_height = n + 1
    while queue:
        height = queue.popleft()
        if height <= a[height]:
            paths[0] = (height, 0)
            path = []
            curr_node = 0
            count = 0
            while curr_node != n:
                path.append(paths[curr_node][1])
                curr_node = paths[curr_node][0]
                count += 1
            return count, path[::-1]
        for node in range(curr_min_height - 1, height - a[height] - 1, -1):
            new_height = node + b[node]
            if new_height not in paths:
                paths[new_height] = (height, node)
                queue.append(new_height)
        curr_min_height = min(curr_min_height, height - a[height])
    return -1, []

num_steps, path = find_path_to_zero()
print(num_steps)
print(*path)


Comments

Submit
0 Comments
More Questions

485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain
1611F - ATM and Students
660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game
441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos