๐Ÿšง This website is under construction. ๐Ÿšง
AtCoder
ABC
291
F

ABC
F
DP

ABC291 F - Teleporter and Closed off

https://atcoder.jp/contests/abc291/tasks/abc291_f (opens in a new tab)

ๆฐด่‰ฒไธŠไฝใ€‚DPใ€‚

keywords ้ ‚็‚นใ‚’้€šใ‚‰ใชใ„ใ€€้ ‚็‚น k ใ‚’้€šใ‚‰ใชใ„ใ€€็‰นๅฎšใฎ้ ‚็‚นใ‚’้€šใ‚‰ใชใ„ใ€€ใ‚ใ‚‹้ ‚็‚นใ‚’้€šใ‚‰ใชใ„ใ€€ใ‚ใ‚‹้ƒฝๅธ‚ใ‚’้€šใ‚‰ใชใ„ใ€€ k ใ‚’้€šใ‚‰ใชใ„


# dp0[i] := v(0) ~ v(i)ใธใฎๆœ€็Ÿญ่ท้›ข # dp1[i] := v(i) ~ v(N - 1)ใธใฎๆœ€็Ÿญ่ท้›ข

2 ใคใฎ DP ้…ๅˆ—ใ‚’็”จๆ„ใ™ใ‚‹ใ“ใจใงไปปๆ„ใฎ่พบใ‚’้€šใ‚‹ใ‚ˆใ†ใชๆœ€็Ÿญ่ท้›ขใ‚’ๆฑ‚ใ‚ใ‚‹ใ“ใจใŒๅ‡บๆฅใ‚‹ใ€‚ ไปŠๅ›žใฎๅˆถ็ด„ใงใฏใ€ๅ„้ ‚็‚นใซ้šฃๆŽฅใ—ใŸ้ ‚็‚นใŒ้ซ˜ใ€… 10 ๅ€‹ใ‹ใค้€ฃ็ถšใ—ใŸ้ ‚็‚น็•ชๅทไธŠใซๅญ˜ๅœจใ™ใ‚‹ใŸใ‚ใ€kใ‚’้€šใ‚‰ใชใ„ใ‚ˆใ†ใช่พบใ‚’ๅ…จๆŽข็ดขใ™ใ‚Œใฐใ‚ˆใ„ใ“ใจใจใชใ‚‹ใ€‚

่บ“ใ„ใŸ็‚นใฏ dp0,dp1ใฎ่จˆ็ฎ—ใ‚’ๆœ€ๅˆ DFS ้ †ใงใ‚„ใฃใฆใ„ใŸ TLE๏ผˆ้ ‚็‚นiใซๅฏพใ—ใฆ้ ‚็‚นi+1~i+mใŒ้šฃๆŽฅใ—ใฆใ„ใ‚‹ใ‚ˆใ†ใชใ‚ฐใƒฉใƒ•ใซใŠใ„ใฆใ€่จˆ็ฎ—้‡ใฏ O(MN)O(M^N) ๏ผ‰ dp1ใ‚’ใฉใ†ๅŸ‹ใ‚ใ‚‹ใ‹ ๆœ‰ๅŠน่พบใฎ้€†่พบใ‚’็ฎก็†ใ—ใ€ใใฎๆƒ…ๅ ฑใซๅŸบใฅใ„ใฆdp1ใ‚’้€†้ †ใซๅŸ‹ใ‚ใ‚ˆใ†ใจ่ฉฆใฟใŸใŒ WA ใ—ใŸ for i in range(n)[::-1]: for j in graph[j] ...ใจใ—ใฆใ„ใŸใ“ใจใซๅพŒใ€…ๆฐ—ใฅใ ๅฎŸใฏๆœ‰ๅŠน่พบใฎ้€†่พบใ‚’ไฟๅญ˜ใ—ใฆใ„ใชใใฆใ‚‚ใ€ใ‚‚ใจใฎๆœ‰ๅŠน่พบใฎใฟใงdp1ใ‚’ๅŸ‹ใ‚ใ‚‹ใ“ใจใŒใงใใ‚‹๏ผˆๅฎŸ่ฃ…ไพ‹ๅ‚็…ง๏ผ‰

INF = float("inf")
 
n, m = map(int, input().split())
 
graph = [[] for _ in range(n)]
 
edges = set()
 
for i in range(n):
    s = input()
    for j in range(m):
        if s[j] == "1":
            graph[i].append(i + j + 1)
            edges.add((i, i + j + 1))
 
# dp0[i] := v(0) ~ v(i)ใธใฎๆœ€็Ÿญ่ท้›ข
dp0 = [INF] * n
# dp1[i] := v(i) ~ v(N - 1)ใธใฎๆœ€็Ÿญ่ท้›ข
dp1 = [INF] * n
 
dp0[0] = 0
for u in range(n):
    for v in graph[u]:
        dp0[v] = min(dp0[v], dp0[u] + 1)
 
dp1[-1] = 0
for u in range(n)[::-1]:
    for v in graph[u]:
        dp1[u] = min(dp1[u], dp1[v] + 1)
 
for k in range(1, n - 1):
    ans = INF
    for via in range(k - m, k):
        for exit in range(k + 1, k + m + 1):
            if (via, exit) in edges:
                ans = min(ans, dp0[via] + dp1[exit] + 1)
    if ans is INF:
        print(-1)
    else:
        print(ans)