結果

問題 No.3263 違法な散歩道
ユーザー 👑 SPD_9X2
提出日時 2025-09-13 00:46:26
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,033 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 82,720 KB
実行使用メモリ 154,004 KB
最終ジャッジ日時 2025-09-13 00:46:42
合計ジャッジ時間 15,454 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 RE * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#重みのないグラフでの最短経路問題
#隣接リストと始点を与えると始点からの距離のリスト & 親のリストを返す
from collections import deque
def NC_Dij(lis,start):

    ret = [float("inf")] * len(lis)
    ret[start] = 0
    
    q = deque([start])
    plis = [i for i in range(len(lis))]

    while len(q) > 0:
        now = q.popleft()

        for nex in lis[now]:

            if ret[nex] > ret[now] + 1:
                ret[nex] = ret[now] + 1
                plis[nex] = now
                q.append(nex)

    return ret,plis

#重み有グラフの最短経路問題
#隣接リストは[隣接点,コスト]で入っていること
#負の重不可

import heapq
def Dijkstra(lis,start):

    ret = [float("inf")] * len(lis)
    ret[start] = 0
    end_flag = [False] * len(lis)
    end_num = 0
    
    q = [(0,start)]

    while len(q) > 0:

        ncost,now = heapq.heappop(q)

        if end_flag[now]:
            continue

        end_flag[now] = True
        end_num += 1

        if end_num == len(lis):
            break

        for nex,ecost in lis[now]:

            if ret[nex] > ncost + ecost:
                ret[nex] = ncost + ecost
                heapq.heappush(q , (ret[nex] , nex))

    return ret

N,M = map(int,input().split())

e = []

for i in range(M):
    u,v = map(int,input().split())
    u -= 1
    v -= 1
    e.append((u,v))

K = int(input())
A = list(map(int,input().split()))
Aset = set( [x-1 for x in A])

lis = [ [] for i in range(N*5) ]

for u,v in e:
    
    if v not in Aset:
        for i in range(5):
            lis[i*N+u].append(v)
    else:
        for i in range(4):
            lis[i*N+u].append( (i+1)*N+v )

    
    if u not in Aset:
        for i in range(5):
            lis[i*N+v].append(u)
    else:
        for i in range(4):
            lis[i*N+v].append( (i+1)*N+u )

dlis,_ = NC_Dij(lis,0)

# print (dlis)

ans = float("inf")

for i in range(5):
    ans = min(ans, dlis[i*N+N-1] )
if ans == float("inf"):
    ans = -1
print (ans)
0