結果

問題 No.2565 はじめてのおつかい
ユーザー rlangevinrlangevin
提出日時 2023-12-11 08:57:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 817 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 91,092 KB
最終ジャッジ日時 2023-12-11 08:58:00
合計ジャッジ時間 9,354 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
55,608 KB
testcase_01 AC 38 ms
55,608 KB
testcase_02 AC 39 ms
55,608 KB
testcase_03 AC 142 ms
91,092 KB
testcase_04 AC 106 ms
91,092 KB
testcase_05 AC 35 ms
55,608 KB
testcase_06 AC 110 ms
79,824 KB
testcase_07 AC 83 ms
77,240 KB
testcase_08 AC 126 ms
79,440 KB
testcase_09 AC 103 ms
78,776 KB
testcase_10 AC 96 ms
79,580 KB
testcase_11 AC 142 ms
84,776 KB
testcase_12 AC 72 ms
76,600 KB
testcase_13 AC 96 ms
78,904 KB
testcase_14 AC 115 ms
82,288 KB
testcase_15 AC 118 ms
81,840 KB
testcase_16 AC 94 ms
87,180 KB
testcase_17 AC 49 ms
72,504 KB
testcase_18 AC 51 ms
75,372 KB
testcase_19 AC 145 ms
83,396 KB
testcase_20 AC 116 ms
82,988 KB
testcase_21 AC 103 ms
82,996 KB
testcase_22 AC 73 ms
80,196 KB
testcase_23 AC 92 ms
80,696 KB
testcase_24 AC 53 ms
72,824 KB
testcase_25 AC 123 ms
83,904 KB
testcase_26 AC 97 ms
80,276 KB
testcase_27 AC 101 ms
83,688 KB
testcase_28 AC 122 ms
83,120 KB
testcase_29 AC 158 ms
85,312 KB
testcase_30 AC 102 ms
84,508 KB
testcase_31 AC 110 ms
83,264 KB
testcase_32 AC 88 ms
79,904 KB
testcase_33 AC 107 ms
84,228 KB
testcase_34 AC 130 ms
83,064 KB
testcase_35 AC 100 ms
85,760 KB
testcase_36 AC 131 ms
84,992 KB
testcase_37 AC 94 ms
80,044 KB
testcase_38 AC 118 ms
82,304 KB
testcase_39 AC 139 ms
81,468 KB
testcase_40 AC 109 ms
85,068 KB
testcase_41 AC 132 ms
86,668 KB
testcase_42 AC 92 ms
80,128 KB
testcase_43 AC 114 ms
81,892 KB
testcase_44 AC 95 ms
79,308 KB
testcase_45 AC 74 ms
77,880 KB
testcase_46 AC 128 ms
82,104 KB
testcase_47 AC 90 ms
78,264 KB
testcase_48 AC 100 ms
79,168 KB
testcase_49 AC 85 ms
76,732 KB
testcase_50 AC 97 ms
78,008 KB
testcase_51 AC 36 ms
55,608 KB
testcase_52 AC 72 ms
76,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from collections import deque
inf = 10 ** 18
def bfs(G, s, N):
    Q = deque([])
    dist = [inf] * N
    par = [inf] * N
    dist[s] = 0
    for u in G[s]:
        par[u] = s
        dist[u] = 1
        Q.append(u)

    while Q:
        u = Q.popleft()
        for v in G[u]:
            if dist[v] != inf:
                continue
            dist[v] = dist[u] + 1
            par[v] = u
            Q.append(v)
            
    return dist


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

D0 = bfs(G, 0, N)
D1 = bfs(G, N-2, N)
D2 = bfs(G, N-1, N)
ans = min(D0[N - 2] + D1[N - 1] + D2[0], D0[N - 1] + D2[N - 2] + D1[0])
print(ans) if ans < 10 ** 10 else print(-1)
0