結果

問題 No.3263 違法な散歩道
ユーザー Yoshio Yoshi
提出日時 2025-09-07 17:54:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 697 ms / 2,000 ms
コード長 1,283 bytes
コンパイル時間 400 ms
コンパイル使用メモリ 82,172 KB
実行使用メモリ 147,936 KB
最終ジャッジ日時 2025-09-07 17:54:34
合計ジャッジ時間 13,038 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush

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
    dp = [INF] * (N * C)
    
    dp[0] = 0
    q = [(0, 0)]
    
    while q:
        cnt, node = heappop(q)

        if cnt > dp[node]:
            continue
        
        for to in G[node]:
            if cnt + 1 < dp[to]:
                dp[to] = cnt + 1
                heappush(q, (cnt + 1, to))

    ans = INF
    for c in range(C):
        ans = min(ans, dp[(N - 1) * C + c])
    
    if ans < INF:
        print(ans)
    else:
        print(-1)

    return

main()
0