結果

問題 No.2565 はじめてのおつかい
ユーザー loop0919loop0919
提出日時 2023-09-21 20:18:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 788 bytes
コンパイル時間 380 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 91,596 KB
最終ジャッジ日時 2023-09-21 20:18:39
合計ジャッジ時間 11,733 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,836 KB
testcase_01 AC 87 ms
71,564 KB
testcase_02 AC 87 ms
71,624 KB
testcase_03 AC 212 ms
91,448 KB
testcase_04 AC 180 ms
91,596 KB
testcase_05 AC 88 ms
71,640 KB
testcase_06 AC 181 ms
81,756 KB
testcase_07 AC 150 ms
79,144 KB
testcase_08 AC 151 ms
80,396 KB
testcase_09 AC 179 ms
80,260 KB
testcase_10 AC 169 ms
80,940 KB
testcase_11 AC 215 ms
85,724 KB
testcase_12 AC 142 ms
78,528 KB
testcase_13 AC 160 ms
80,424 KB
testcase_14 AC 188 ms
83,312 KB
testcase_15 AC 207 ms
82,600 KB
testcase_16 AC 176 ms
86,528 KB
testcase_17 AC 129 ms
79,232 KB
testcase_18 AC 135 ms
81,436 KB
testcase_19 AC 202 ms
84,540 KB
testcase_20 AC 194 ms
84,148 KB
testcase_21 AC 197 ms
84,056 KB
testcase_22 AC 165 ms
81,960 KB
testcase_23 AC 168 ms
82,048 KB
testcase_24 AC 131 ms
78,572 KB
testcase_25 AC 200 ms
84,200 KB
testcase_26 AC 167 ms
81,744 KB
testcase_27 AC 188 ms
84,992 KB
testcase_28 AC 208 ms
84,352 KB
testcase_29 AC 213 ms
86,356 KB
testcase_30 AC 189 ms
85,616 KB
testcase_31 AC 192 ms
84,512 KB
testcase_32 AC 163 ms
81,840 KB
testcase_33 AC 216 ms
85,300 KB
testcase_34 AC 192 ms
83,508 KB
testcase_35 AC 187 ms
86,816 KB
testcase_36 AC 198 ms
85,048 KB
testcase_37 AC 162 ms
82,064 KB
testcase_38 AC 187 ms
83,740 KB
testcase_39 AC 194 ms
82,604 KB
testcase_40 AC 194 ms
86,020 KB
testcase_41 AC 209 ms
86,944 KB
testcase_42 AC 166 ms
81,544 KB
testcase_43 AC 176 ms
83,012 KB
testcase_44 AC 159 ms
80,596 KB
testcase_45 AC 144 ms
79,448 KB
testcase_46 AC 192 ms
82,668 KB
testcase_47 AC 165 ms
79,860 KB
testcase_48 AC 169 ms
80,796 KB
testcase_49 AC 142 ms
78,560 KB
testcase_50 AC 182 ms
80,292 KB
testcase_51 AC 92 ms
71,572 KB
testcase_52 AC 168 ms
78,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

INF = 1 << 60

N, M = map(int, input().split())

graph = [[] for _ in range(N)]

for _ in range(M):
    u, v = map(int, input().split())
    u -= 1; v -= 1
    graph[u].append(v)

def bfs(start):
    distances = [INF]*N
    distances[start] = 0
    
    que = deque([(start, 0)])
    
    while que:
        now, d = que.popleft()
        
        for next in graph[now]:
            if distances[next] < INF:
                continue
            distances[next] = d + 1
            que.append((next, d + 1))
    
    return distances

dist = {
    0: bfs(0),
    N-2: bfs(N-2),
    N-1: bfs(N-1)
}

ans = min(
    dist[0][N-2] + dist[N-2][N-1] + dist[N-1][0],
    dist[0][N-1] + dist[N-1][N-2] + dist[N-2][0]
)

if ans < INF:
	print(ans)
else:
	print(-1)

0