結果

問題 No.3263 違法な散歩道
ユーザー Yoshio Yoshi
提出日時 2025-09-07 18:04:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 544 ms / 2,000 ms
コード長 1,239 bytes
コンパイル時間 416 ms
コンパイル使用メモリ 82,528 KB
実行使用メモリ 146,228 KB
最終ジャッジ日時 2025-09-07 18:04:15
合計ジャッジ時間 11,772 ms
ジャッジサーバーID
(参考情報)
judge / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def main():
    C = 5

    N, M = map(int, input().split())
    G = [[] for _ in range(N * C)]
    UV = []
    for _ in range(M):
        u, v = map(int, input().split())
        UV.append((u - 1, v - 1))

    K = int(input())
    if K:
        A = list(map(lambda x: int(x) - 1, input().split()))
        A = set(A)
    else:
        A = set()

    for u, v in UV:
        if v in A:
            for c in range(C - 1):
                G[u * C + c].append(v * C + c + 1)
        else:
            for c in range(C):
                G[u * C + c].append(v * C)

        if u in A:
            for c in range(C - 1):
                G[v * C + c].append(u * C + c + 1)
        else:
            for c in range(C):
                G[v * C + c].append(u * C)

    INF = 10**18
    checked = [False] * (N * C)
    bfs = deque([(0, 0)])
    
    ans = INF
    while bfs:
        cnt, node = bfs.popleft()
        if node // C == N - 1:
            ans = cnt
            break

        for to in G[node]:
            
            if not checked[to]:
                checked[to] = True
                bfs.append((cnt + 1, to))
    
    if ans < INF:
        print(ans)
    else:
        print(-1)

    return

main()
0