結果

問題 No.2565 はじめてのおつかい
ユーザー K5h1n0K5h1n0
提出日時 2023-12-02 16:29:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 889 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 98,012 KB
最終ジャッジ日時 2023-12-02 16:30:03
合計ジャッジ時間 9,788 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,604 KB
testcase_01 AC 39 ms
55,604 KB
testcase_02 AC 39 ms
55,604 KB
testcase_03 AC 180 ms
98,008 KB
testcase_04 AC 183 ms
98,012 KB
testcase_05 AC 41 ms
55,604 KB
testcase_06 AC 156 ms
81,276 KB
testcase_07 AC 113 ms
78,080 KB
testcase_08 AC 123 ms
80,508 KB
testcase_09 AC 145 ms
79,612 KB
testcase_10 AC 136 ms
81,020 KB
testcase_11 AC 200 ms
88,816 KB
testcase_12 AC 103 ms
76,672 KB
testcase_13 AC 125 ms
79,484 KB
testcase_14 AC 162 ms
84,604 KB
testcase_15 AC 175 ms
83,196 KB
testcase_16 AC 142 ms
91,284 KB
testcase_17 AC 85 ms
77,312 KB
testcase_18 AC 93 ms
81,276 KB
testcase_19 AC 181 ms
88,060 KB
testcase_20 AC 191 ms
87,036 KB
testcase_21 AC 162 ms
87,164 KB
testcase_22 AC 126 ms
82,940 KB
testcase_23 AC 130 ms
82,812 KB
testcase_24 AC 87 ms
77,056 KB
testcase_25 AC 183 ms
88,060 KB
testcase_26 AC 145 ms
82,172 KB
testcase_27 AC 176 ms
88,572 KB
testcase_28 AC 183 ms
87,420 KB
testcase_29 AC 200 ms
91,100 KB
testcase_30 AC 171 ms
89,388 KB
testcase_31 AC 196 ms
88,188 KB
testcase_32 AC 145 ms
82,300 KB
testcase_33 AC 197 ms
88,956 KB
testcase_34 AC 199 ms
84,988 KB
testcase_35 AC 172 ms
89,768 KB
testcase_36 AC 199 ms
88,572 KB
testcase_37 AC 133 ms
82,556 KB
testcase_38 AC 168 ms
85,756 KB
testcase_39 AC 166 ms
83,580 KB
testcase_40 AC 201 ms
90,736 KB
testcase_41 AC 200 ms
91,568 KB
testcase_42 AC 134 ms
81,532 KB
testcase_43 AC 156 ms
84,348 KB
testcase_44 AC 127 ms
80,508 KB
testcase_45 AC 99 ms
78,076 KB
testcase_46 AC 166 ms
83,836 KB
testcase_47 AC 132 ms
79,104 KB
testcase_48 AC 144 ms
80,252 KB
testcase_49 AC 98 ms
76,672 KB
testcase_50 AC 141 ms
78,592 KB
testcase_51 AC 40 ms
55,604 KB
testcase_52 AC 120 ms
77,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict,deque

n,m = map(int,input().split())
d = defaultdict(list)
for _ in range(m):
    a,b = map(int,input().split())
    d[a].append(b)
from1 = [float("INF")]*(n+1)
fromnminus1 = [float("INF")]*(n+1)
fromn = [float("INF")]*(n+1)

def bfs(start,result):
    global d
    visited = [False]*(n+1)
    q = deque()
    visited[start] = True
    cnt = 0
    q.append((start,cnt))
    while q:
        nowv,nowcnt = q.popleft()
        result[nowv] = nowcnt
        for nextv in d[nowv]:
            if visited[nextv] == False:
                visited[nextv] = True
                q.append((nextv,nowcnt+1))
    return result

bfs(1,from1)
bfs(n-1,fromnminus1)
bfs(n,fromn)
route1 = from1[n-1] + fromnminus1[n] + fromn[1]
route2 = from1[n] + fromn[n-1] + fromnminus1[1]
result = min(route1,route2)
if result == float("inf"):
    print(-1)
else:
    print(result)
0