結果

問題 No.2565 はじめてのおつかい
ユーザー hiro1729hiro1729
提出日時 2023-12-02 15:00:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 592 bytes
コンパイル時間 470 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,980 KB
最終ジャッジ日時 2023-12-02 15:00:52
合計ジャッジ時間 9,752 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,616 KB
testcase_01 AC 37 ms
55,616 KB
testcase_02 AC 37 ms
55,616 KB
testcase_03 AC 181 ms
89,980 KB
testcase_04 AC 149 ms
89,852 KB
testcase_05 AC 39 ms
55,616 KB
testcase_06 AC 156 ms
79,368 KB
testcase_07 AC 118 ms
77,808 KB
testcase_08 AC 139 ms
78,980 KB
testcase_09 AC 151 ms
78,960 KB
testcase_10 AC 140 ms
78,864 KB
testcase_11 AC 184 ms
84,056 KB
testcase_12 AC 100 ms
77,168 KB
testcase_13 AC 129 ms
78,064 KB
testcase_14 AC 155 ms
81,572 KB
testcase_15 AC 153 ms
81,256 KB
testcase_16 AC 148 ms
85,180 KB
testcase_17 AC 78 ms
76,784 KB
testcase_18 AC 81 ms
78,752 KB
testcase_19 AC 157 ms
82,936 KB
testcase_20 AC 145 ms
81,888 KB
testcase_21 AC 134 ms
81,896 KB
testcase_22 AC 108 ms
79,996 KB
testcase_23 AC 120 ms
79,856 KB
testcase_24 AC 80 ms
77,040 KB
testcase_25 AC 158 ms
82,804 KB
testcase_26 AC 122 ms
79,564 KB
testcase_27 AC 131 ms
82,456 KB
testcase_28 AC 187 ms
82,660 KB
testcase_29 AC 166 ms
84,740 KB
testcase_30 AC 139 ms
83,020 KB
testcase_31 AC 154 ms
82,164 KB
testcase_32 AC 120 ms
79,704 KB
testcase_33 AC 148 ms
82,868 KB
testcase_34 AC 166 ms
82,348 KB
testcase_35 AC 142 ms
84,144 KB
testcase_36 AC 168 ms
83,888 KB
testcase_37 AC 134 ms
79,716 KB
testcase_38 AC 148 ms
81,328 KB
testcase_39 AC 176 ms
80,884 KB
testcase_40 AC 157 ms
84,476 KB
testcase_41 AC 164 ms
85,308 KB
testcase_42 AC 121 ms
79,288 KB
testcase_43 AC 164 ms
81,308 KB
testcase_44 AC 123 ms
78,980 KB
testcase_45 AC 93 ms
77,168 KB
testcase_46 AC 187 ms
81,136 KB
testcase_47 AC 135 ms
78,704 KB
testcase_48 AC 137 ms
78,584 KB
testcase_49 AC 119 ms
77,168 KB
testcase_50 AC 135 ms
78,192 KB
testcase_51 AC 38 ms
55,616 KB
testcase_52 AC 112 ms
76,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(m):
	u, v = map(int, input().split())
	u -= 1
	v -= 1
	g[u].append(v)

from collections import deque

def bfs(u):
	q = deque([u])
	d = [-1] * n
	d[u] = 0
	while q:
		v = q.popleft()
		for i in g[v]:
			if d[i] == -1:
				d[i] = d[v] + 1
				q.append(i)
	return d
d0 = bfs(0)
d1 = bfs(n - 2)
d2 = bfs(n - 1)
ans = 10 ** 9
if d0[n-2]>0 and d1[n-1]>0 and d2[0]>0:
	ans=min(ans,d0[n-2] + d1[n-1] + d2[0])
if d0[n-1]>0 and d2[n-2]>0 and d1[0]>0:
	ans=min(ans,d0[n-1] + d2[n-2] + d1[0])
print(-1 if ans == 10 ** 9 else ans)
0