結果

問題 No.2565 はじめてのおつかい
ユーザー loop0919loop0919
提出日時 2023-10-31 13:24:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 814 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 81,848 KB
実行使用メモリ 89,848 KB
最終ジャッジ日時 2023-10-31 13:24:44
合計ジャッジ時間 12,727 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,660 KB
testcase_01 AC 38 ms
55,660 KB
testcase_02 AC 39 ms
55,660 KB
testcase_03 AC 186 ms
89,840 KB
testcase_04 AC 142 ms
89,848 KB
testcase_05 AC 38 ms
55,660 KB
testcase_06 AC 145 ms
79,700 KB
testcase_07 AC 109 ms
77,916 KB
testcase_08 AC 109 ms
78,924 KB
testcase_09 AC 134 ms
78,964 KB
testcase_10 AC 123 ms
79,208 KB
testcase_11 AC 178 ms
84,076 KB
testcase_12 AC 95 ms
76,644 KB
testcase_13 AC 115 ms
78,412 KB
testcase_14 AC 141 ms
81,436 KB
testcase_15 AC 142 ms
80,808 KB
testcase_16 AC 127 ms
85,572 KB
testcase_17 AC 83 ms
77,168 KB
testcase_18 AC 87 ms
79,184 KB
testcase_19 AC 188 ms
83,016 KB
testcase_20 AC 156 ms
82,256 KB
testcase_21 AC 153 ms
82,376 KB
testcase_22 AC 119 ms
80,432 KB
testcase_23 AC 120 ms
80,228 KB
testcase_24 AC 83 ms
77,176 KB
testcase_25 AC 151 ms
82,508 KB
testcase_26 AC 121 ms
79,876 KB
testcase_27 AC 142 ms
82,916 KB
testcase_28 AC 175 ms
82,864 KB
testcase_29 AC 175 ms
84,808 KB
testcase_30 AC 155 ms
83,504 KB
testcase_31 AC 155 ms
82,524 KB
testcase_32 AC 122 ms
80,040 KB
testcase_33 AC 171 ms
83,224 KB
testcase_34 AC 158 ms
81,696 KB
testcase_35 AC 157 ms
84,504 KB
testcase_36 AC 162 ms
83,212 KB
testcase_37 AC 118 ms
80,152 KB
testcase_38 AC 144 ms
81,704 KB
testcase_39 AC 142 ms
80,916 KB
testcase_40 AC 163 ms
84,692 KB
testcase_41 AC 180 ms
85,412 KB
testcase_42 AC 123 ms
79,624 KB
testcase_43 AC 150 ms
81,260 KB
testcase_44 AC 117 ms
79,040 KB
testcase_45 AC 92 ms
77,512 KB
testcase_46 AC 150 ms
80,924 KB
testcase_47 AC 127 ms
78,664 KB
testcase_48 AC 124 ms
78,872 KB
testcase_49 AC 94 ms
76,652 KB
testcase_50 AC 133 ms
78,372 KB
testcase_51 AC 40 ms
55,660 KB
testcase_52 AC 119 ms
77,008 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