結果

問題 No.2565 はじめてのおつかい
ユーザー hatonobuhatonobu
提出日時 2023-12-02 17:46:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 1,602 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,712 KB
最終ジャッジ日時 2023-12-02 17:46:41
合計ジャッジ時間 8,431 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,600 KB
testcase_01 AC 46 ms
55,600 KB
testcase_02 AC 41 ms
55,600 KB
testcase_03 AC 153 ms
89,712 KB
testcase_04 AC 122 ms
89,712 KB
testcase_05 AC 41 ms
55,600 KB
testcase_06 AC 135 ms
80,380 KB
testcase_07 AC 109 ms
78,180 KB
testcase_08 AC 128 ms
79,480 KB
testcase_09 AC 125 ms
78,692 KB
testcase_10 AC 128 ms
79,492 KB
testcase_11 AC 195 ms
84,164 KB
testcase_12 AC 90 ms
76,900 KB
testcase_13 AC 117 ms
79,076 KB
testcase_14 AC 162 ms
82,072 KB
testcase_15 AC 148 ms
81,368 KB
testcase_16 AC 102 ms
84,916 KB
testcase_17 AC 66 ms
77,284 KB
testcase_18 AC 70 ms
79,508 KB
testcase_19 AC 145 ms
83,180 KB
testcase_20 AC 175 ms
82,516 KB
testcase_21 AC 126 ms
82,012 KB
testcase_22 AC 78 ms
79,216 KB
testcase_23 AC 111 ms
80,480 KB
testcase_24 AC 56 ms
70,756 KB
testcase_25 AC 134 ms
82,792 KB
testcase_26 AC 112 ms
80,188 KB
testcase_27 AC 96 ms
82,064 KB
testcase_28 AC 133 ms
82,776 KB
testcase_29 AC 196 ms
84,732 KB
testcase_30 AC 108 ms
82,628 KB
testcase_31 AC 144 ms
82,792 KB
testcase_32 AC 86 ms
78,920 KB
testcase_33 AC 105 ms
82,348 KB
testcase_34 AC 147 ms
81,952 KB
testcase_35 AC 105 ms
83,624 KB
testcase_36 AC 145 ms
83,496 KB
testcase_37 AC 110 ms
80,340 KB
testcase_38 AC 117 ms
80,932 KB
testcase_39 AC 161 ms
81,508 KB
testcase_40 AC 107 ms
83,060 KB
testcase_41 AC 136 ms
85,300 KB
testcase_42 AC 114 ms
79,912 KB
testcase_43 AC 95 ms
80,652 KB
testcase_44 AC 110 ms
79,092 KB
testcase_45 AC 67 ms
77,284 KB
testcase_46 AC 156 ms
81,380 KB
testcase_47 AC 124 ms
78,308 KB
testcase_48 AC 126 ms
79,080 KB
testcase_49 AC 91 ms
76,900 KB
testcase_50 AC 119 ms
78,052 KB
testcase_51 AC 45 ms
55,600 KB
testcase_52 AC 91 ms
76,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0