結果

問題 No.2565 はじめてのおつかい
ユーザー ゼットゼット
提出日時 2023-12-21 00:26:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 806 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,976 KB
最終ジャッジ日時 2023-12-21 00:26:18
合計ジャッジ時間 11,603 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,608 KB
testcase_01 AC 41 ms
55,608 KB
testcase_02 AC 42 ms
55,608 KB
testcase_03 AC 231 ms
89,976 KB
testcase_04 AC 162 ms
89,976 KB
testcase_05 AC 42 ms
55,608 KB
testcase_06 AC 186 ms
79,496 KB
testcase_07 AC 158 ms
77,808 KB
testcase_08 AC 147 ms
78,980 KB
testcase_09 AC 163 ms
78,960 KB
testcase_10 AC 154 ms
79,504 KB
testcase_11 AC 227 ms
84,060 KB
testcase_12 AC 113 ms
77,168 KB
testcase_13 AC 150 ms
78,064 KB
testcase_14 AC 223 ms
82,208 KB
testcase_15 AC 200 ms
81,256 KB
testcase_16 AC 149 ms
85,304 KB
testcase_17 AC 90 ms
76,912 KB
testcase_18 AC 93 ms
78,876 KB
testcase_19 AC 213 ms
83,060 KB
testcase_20 AC 191 ms
81,884 KB
testcase_21 AC 173 ms
81,892 KB
testcase_22 AC 130 ms
79,996 KB
testcase_23 AC 147 ms
79,856 KB
testcase_24 AC 93 ms
77,168 KB
testcase_25 AC 209 ms
82,800 KB
testcase_26 AC 149 ms
79,564 KB
testcase_27 AC 169 ms
82,452 KB
testcase_28 AC 226 ms
82,784 KB
testcase_29 AC 220 ms
84,736 KB
testcase_30 AC 178 ms
83,144 KB
testcase_31 AC 196 ms
82,160 KB
testcase_32 AC 146 ms
79,704 KB
testcase_33 AC 213 ms
82,864 KB
testcase_34 AC 212 ms
82,472 KB
testcase_35 AC 174 ms
84,140 KB
testcase_36 AC 211 ms
83,884 KB
testcase_37 AC 144 ms
79,844 KB
testcase_38 AC 204 ms
81,452 KB
testcase_39 AC 235 ms
81,012 KB
testcase_40 AC 199 ms
84,472 KB
testcase_41 AC 218 ms
85,304 KB
testcase_42 AC 150 ms
79,288 KB
testcase_43 AC 186 ms
81,304 KB
testcase_44 AC 155 ms
78,980 KB
testcase_45 AC 117 ms
77,296 KB
testcase_46 AC 226 ms
81,648 KB
testcase_47 AC 152 ms
78,704 KB
testcase_48 AC 167 ms
79,096 KB
testcase_49 AC 111 ms
77,168 KB
testcase_50 AC 157 ms
78,192 KB
testcase_51 AC 42 ms
55,608 KB
testcase_52 AC 126 ms
76,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int,input().split())
G=[[] for i in range(N)]
for i in range(M):
  a,b=map(int,input().split())
  G[a-1].append(b-1)
from collections import deque
S=deque()
dist=[10**10]*N
dist[0]=0
S.append(0)
while S:
  x=S.popleft()
  for y in G[x]:
    if dist[y]<10**8:
      continue
    dist[y]=dist[x]+1
    S.append(y)
L1=[10**10]*N
L1[N-1]=0
S=deque()
S.append(N-1)
while S:
  x=S.popleft()
  for y in G[x]:
    if L1[y]<10**8:
      continue
    L1[y]=L1[x]+1
    S.append(y)
L2=[10**10]*N
L2[N-2]=0
S=deque()
S.append(N-2)
while S:
  x=S.popleft()
  for y in G[x]:
    if L2[y]<10**8:
      continue
    L2[y]=L2[x]+1
    S.append(y)
result=10**10
ans=dist[N-1]+L1[N-2]+L2[0]
result=min(result,ans)
ans=dist[N-2]+L2[N-1]+L1[0]
result=min(result,ans)
if result>10**8:
  print(-1)
  exit()
print(result)
0