結果

問題 No.2565 はじめてのおつかい
ユーザー oginoshikibuoginoshikibu
提出日時 2023-12-02 15:51:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 258 ms / 2,000 ms
コード長 1,415 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 112,996 KB
最終ジャッジ日時 2023-12-02 15:51:51
合計ジャッジ時間 9,928 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,600 KB
testcase_01 AC 40 ms
55,600 KB
testcase_02 AC 40 ms
55,600 KB
testcase_03 AC 215 ms
112,996 KB
testcase_04 AC 200 ms
112,996 KB
testcase_05 AC 37 ms
55,600 KB
testcase_06 AC 213 ms
83,592 KB
testcase_07 AC 129 ms
79,496 KB
testcase_08 AC 153 ms
83,336 KB
testcase_09 AC 161 ms
82,056 KB
testcase_10 AC 164 ms
84,104 KB
testcase_11 AC 244 ms
96,776 KB
testcase_12 AC 108 ms
78,464 KB
testcase_13 AC 175 ms
82,568 KB
testcase_14 AC 205 ms
89,864 KB
testcase_15 AC 202 ms
87,688 KB
testcase_16 AC 139 ms
93,960 KB
testcase_17 AC 79 ms
77,704 KB
testcase_18 AC 84 ms
79,624 KB
testcase_19 AC 179 ms
93,192 KB
testcase_20 AC 164 ms
91,656 KB
testcase_21 AC 205 ms
92,680 KB
testcase_22 AC 104 ms
84,104 KB
testcase_23 AC 124 ms
84,488 KB
testcase_24 AC 73 ms
77,320 KB
testcase_25 AC 159 ms
92,680 KB
testcase_26 AC 146 ms
85,512 KB
testcase_27 AC 134 ms
89,224 KB
testcase_28 AC 186 ms
92,808 KB
testcase_29 AC 258 ms
99,336 KB
testcase_30 AC 147 ms
91,528 KB
testcase_31 AC 185 ms
92,936 KB
testcase_32 AC 103 ms
83,464 KB
testcase_33 AC 142 ms
90,504 KB
testcase_34 AC 168 ms
89,224 KB
testcase_35 AC 149 ms
91,928 KB
testcase_36 AC 172 ms
93,320 KB
testcase_37 AC 173 ms
86,272 KB
testcase_38 AC 141 ms
87,688 KB
testcase_39 AC 216 ms
88,200 KB
testcase_40 AC 163 ms
94,984 KB
testcase_41 AC 203 ms
96,176 KB
testcase_42 AC 159 ms
85,000 KB
testcase_43 AC 130 ms
87,048 KB
testcase_44 AC 143 ms
82,184 KB
testcase_45 AC 82 ms
77,960 KB
testcase_46 AC 210 ms
89,608 KB
testcase_47 AC 140 ms
80,648 KB
testcase_48 AC 156 ms
82,952 KB
testcase_49 AC 110 ms
78,344 KB
testcase_50 AC 156 ms
81,544 KB
testcase_51 AC 39 ms
55,600 KB
testcase_52 AC 120 ms
78,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque
n, m = map(int, input().split())
graph = defaultdict(set)

for i in range(m):
    a, b = map(lambda x: int(x) - 1, input().split())
    graph[a].add(b)

INF = float("inf")
startdist = [INF for _ in range(n)]
startdist[0]=0
startD=deque()
startD.append([0,0])

while startD:
    now, dist = startD.popleft()
    if startdist[now] < dist:
        continue
    for next in graph[now]:
        if startdist[next] > dist + 1:
            startdist[next] = dist + 1
            startD.append([next, dist + 1])

if startdist[-1]==INF or startdist[-2]==INF:
    print("-1")
    exit()

INF = float("inf")
n1dist = [INF for _ in range(n)]
n1dist[-1]=0
n1D=deque()
n1D.append([n-1,0])

while n1D:
    now, dist = n1D.popleft()
    if n1dist[now] < dist:
        continue
    for next in graph[now]:
        if n1dist[next] > dist + 1:
            n1dist[next] = dist + 1
            n1D.append([next, dist + 1])



INF = float("inf")
n2dist = [INF for _ in range(n)]
n2dist[-2]=0
n2D=deque()
n2D.append([n-2,0])

while n2D:
    now, dist = n2D.popleft()
    if n2dist[now] < dist:
        continue
    for next in graph[now]:
        if n2dist[next] > dist + 1:
            n2dist[next] = dist + 1
            n2D.append([next, dist + 1])

ans = INF
ans = min(ans, startdist[-1]+n1dist[-2]+n2dist[0])
ans = min(ans, startdist[-2]+n2dist[-1]+n1dist[0])
print(ans if ans!=INF else -1)
0