Nastya and Unexpected Guest (Codeforces 1340C)
Tags: DP, shortest paths

 If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya. On the way from Denis's house to the girl's house is a road of $$$n$$$ lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up $$$g$$$ seconds green, and then $$$r$$$ seconds red, then again $$$g$$$ seconds green and so on.Formally, the road can be represented as a segment $$$[0, n]$$$. Initially, Denis is at point $$$0$$$. His task is to get to point $$$n$$$ in the shortest possible time.He knows many different integers $$$d_1, d_2, \ldots, d_m$$$, where $$$0 \leq d_i \leq n$$$  — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:  He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by $$$\pm 1$$$ in $$$1$$$ second. While doing so, he must always stay inside the segment $$$[0, n]$$$.  He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by $$$+1$$$ and he walked on a safety island, then he can change his position by $$$\pm 1$$$. Otherwise, he can change his position only by $$$+1$$$. Similarly, if in the previous second he changed his position by $$$-1$$$, on a safety island he can change position by $$$\pm 1$$$, and at any other point by $$$-1$$$.  At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on. Denis has crossed the road as soon as his coordinate becomes equal to $$$n$$$.This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.

Input: The first line contains two integers $$$n$$$ and $$$m$$$ $$$(1 \leq n \leq 10^6, 2 \leq m \leq min(n + 1, 10^4))$$$  — road width and the number of safety islands.The second line contains $$$m$$$ distinct integers $$$d_1, d_2, \ldots, d_m$$$ $$$(0 \leq d_i \leq n)$$$  — the points where the safety islands are located. It is guaranteed that there are $$$0$$$ and $$$n$$$ among them.The third line contains two integers $$$g, r$$$ $$$(1 \leq g, r \leq 1000)$$$  — the time that the green light stays on and the time that the red light stays on.

Output: Output a single integer  — the minimum time for which Denis can cross the road with obeying all the rules.If it is impossible to cross the road output $$$-1$$$.

Note: In the first test, the optimal route is:       for the first green light, go to $$$7$$$ and return to $$$3$$$. In this case, we will change the direction of movement at the point $$$7$$$, which is allowed, since there is a safety island at this point. In the end, we will be at the point of $$$3$$$, where there is also a safety island. The next $$$11$$$ seconds we have to wait for the red light.      for the second green light reaches $$$14$$$. Wait for the red light again.      for $$$1$$$ second go to $$$15$$$. As a result, Denis is at the end of the road. In total, $$$45$$$ seconds are obtained.In the second test, it is impossible to cross the road according to all the rules.

Sample Inputs:
15 5
0 3 7 14 15
11 11

13 4
0 3 7 13
9 9

Sample Outputs:
45

-1

Solution (Python):
from collections import deque
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u * g + v
(n, m) = map(int, input().split())
d = list(map(int, input().split()))
d.sort()
(g, r) = map(int, input().split())
inf = pow(10, 9) + 1
(l0, r0) = ([inf] * m, [inf] * m)
for i in range(m - 1):
    d0 = d[i + 1] - d[i]
    (l0[i + 1], r0[i]) = (d0, d0)
dp = [inf] * (m * g)
dp[0] = 0
q = deque([0])
c = 0
while q:
    v = q.popleft()
    (i, j) = divmod(v, g)
    for (ni, nj) in [(i - 1, j + l0[i]), (i + 1, j + r0[i])]:
        u = f(ni, nj % g)
        if nj < g and dp[u] == inf:
            dp[u] = dp[v]
            q.appendleft(u)
        elif nj == g and dp[u] == inf:
            dp[u] = dp[v] + 1
            q.append(u)
ans = inf
for i in range(m - 1, -1, -1):
    if d[i] + g < n:
        break
    ans = min(ans, dp[f(i, 0)] * (g + r) + n - d[i])
ans = (ans + 1) % (inf + 1) - 1
print(ans)

Solution (Python):
from collections import deque
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u * g + v
(n, m) = map(int, input().split())
d = list(map(int, input().split()))
d.sort()
(g, r) = map(int, input().split())
inf = pow(10, 9) + 1
(l0, r0) = ([inf] * m, [inf] * m)
for i in range(m - 1):
    d0 = d[i + 1] - d[i]
    (l0[i + 1], r0[i]) = (d0, d0)
dp = [inf] * (m * g)
dp[0] = 0
q = deque([0])
c = 0
while q:
    v = q.popleft()
    (i, j) = divmod(v, g)
    for (ni, nj) in [(i - 1, j + l0[i]), (i + 1, j + r0[i])]:
        u = f(ni, nj % g)
        if nj < g and dp[u] == inf:
            dp[u] = dp[v]
            q.appendleft(u)
        elif nj == g and dp[u] == inf:
            dp[u] = dp[v] + 1
            q.append(u)
ans = inf
for i in range(m - 1, -1, -1):
    if d[i] + g < n:
        break
    ans = min(ans, dp[f(i, 0)] * (g + r) + n - d[i])
ans = (ans + 1) % (inf + 1) - 1
print(ans)

Solution (Python):
from collections import deque
(N, M) = map(int, input().split())
D = sorted(list(set([int(a) for a in input().split()] + [-1 << 10, N])))
M = len(D)
DD = [D[i + 1] - D[i] for i in range(M - 1)]
(green, red) = map(int, input().split())
X = [[0] * (green + 1) for _ in range(M)]
Q = deque([(1, green, 0)])
ans = 1 << 100
goal = M - 1
while Q:
    (step, tl, cycles) = deque.popleft(Q)
    if X[step][tl]:
        continue
    X[step][tl] = 1
    if step == goal:
        ans = min(ans, cycles * (green + red) + (green - tl if tl < green else -red))
        continue
    (dl, dr) = (DD[step - 1], DD[step])
    if dl < tl:
        Q.appendleft((step - 1, tl - dl, cycles))
    elif dl == tl:
        Q.append((step - 1, green, cycles + 1))
    if dr < tl:
        Q.appendleft((step + 1, tl - dr, cycles))
    elif dr == tl:
        Q.append((step + 1, green, cycles + 1))
print(ans if ans < 1 << 99 else -1)