結果

問題 No.3263 違法な散歩道
ユーザー Yoshio Yoshi
提出日時 2025-09-07 17:52:21
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,237 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 82,852 KB
実行使用メモリ 148,416 KB
最終ジャッジ日時 2025-09-07 17:52:36
合計ジャッジ時間 13,037 ms
ジャッジサーバーID
(参考情報)
judge4 / judge
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 RE * 1
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())
    A = list(map(lambda x: int(x) - 1, input().split()))
    A = set(A)

    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