結果
| 問題 | No.2565 はじめてのおつかい | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-12-02 17:46:31 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 186 ms / 2,000 ms | 
| コード長 | 1,602 bytes | 
| コンパイル時間 | 330 ms | 
| コンパイル使用メモリ | 82,304 KB | 
| 実行使用メモリ | 89,984 KB | 
| 最終ジャッジ日時 | 2024-09-26 21:18:57 | 
| 合計ジャッジ時間 | 8,710 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 50 | 
ソースコード
import sys
from collections import deque,defaultdict
import itertools
import heapq
import bisect
import math
#sys.setrecursionlimit(10 ** 9)
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
ml = lambda: map(str, input().split())
li = lambda: list(mi())
li_st = lambda: list(map(str, input().split()))
lli = lambda n: [li() for _ in range(n)]
mod = 998244353
INF = 8*10**18
N,M = mi()
G = [[] for _ in range(N)]
for i in range(M):
    u,v = mi()
    u -= 1
    v -= 1
    G[u].append(v)
check = [INF] * N
check2 = [INF] * N
check3 = [INF] * N
q = deque()
q.append((0,0))
check[0] = 0
while(q):
    now,times = q.popleft()
    for nxt in G[now]:
        if check[nxt] == INF:
            q.append((nxt,times+1))
        check[nxt] = min(times + 1, check[nxt])
if check[N-2] != INF:
    q = deque()
    q.append((N-2,0))
    check2[N-2] = 0
    while(q):
        now,times = q.popleft()          
        for nxt in G[now]:
            if check2[nxt] == INF:
                q.append((nxt,times+1))
            check2[nxt] = min(check2[nxt],times+1)
if check[N-1] != INF:
    q = deque()
    q.append((N-1,0))
    check3[N-1] =  0
    while(q):
        now,times = q.popleft()
        for nxt in G[now]:
            if check3[nxt] == INF:
                q.append((nxt,times+1))
            check3[nxt] = min(check3[nxt],times+1)
ans1 = check[N-1] + check3[N-2] + check2[0]
ans2 = check[N-2] + check2[N-1] + check3[0]
#print(check)
#print(check2)
#print(check3)
#print(ans1,ans2)
print(min(ans1,ans2) if min(ans1,ans2) < INF else -1)
            
            
            
        