結果

問題 No.2565 はじめてのおつかい
ユーザー mealymealy
提出日時 2023-12-02 16:12:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 1,616 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 100,140 KB
最終ジャッジ日時 2023-12-02 16:13:02
合計ジャッジ時間 9,735 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
55,612 KB
testcase_01 AC 34 ms
55,612 KB
testcase_02 AC 35 ms
55,612 KB
testcase_03 AC 235 ms
100,140 KB
testcase_04 AC 162 ms
100,140 KB
testcase_05 AC 38 ms
55,612 KB
testcase_06 AC 155 ms
85,764 KB
testcase_07 AC 119 ms
80,748 KB
testcase_08 AC 140 ms
82,868 KB
testcase_09 AC 146 ms
85,984 KB
testcase_10 AC 140 ms
84,212 KB
testcase_11 AC 196 ms
94,084 KB
testcase_12 AC 108 ms
80,812 KB
testcase_13 AC 160 ms
83,840 KB
testcase_14 AC 169 ms
88,896 KB
testcase_15 AC 158 ms
86,128 KB
testcase_16 AC 122 ms
87,040 KB
testcase_17 AC 77 ms
76,844 KB
testcase_18 AC 88 ms
80,832 KB
testcase_19 AC 170 ms
91,364 KB
testcase_20 AC 150 ms
86,028 KB
testcase_21 AC 138 ms
86,024 KB
testcase_22 AC 132 ms
81,788 KB
testcase_23 AC 130 ms
85,808 KB
testcase_24 AC 82 ms
76,952 KB
testcase_25 AC 183 ms
90,876 KB
testcase_26 AC 132 ms
85,420 KB
testcase_27 AC 140 ms
86,156 KB
testcase_28 AC 174 ms
91,744 KB
testcase_29 AC 185 ms
94,884 KB
testcase_30 AC 182 ms
92,472 KB
testcase_31 AC 167 ms
91,172 KB
testcase_32 AC 135 ms
85,136 KB
testcase_33 AC 170 ms
91,804 KB
testcase_34 AC 190 ms
90,532 KB
testcase_35 AC 151 ms
93,496 KB
testcase_36 AC 185 ms
91,924 KB
testcase_37 AC 159 ms
85,404 KB
testcase_38 AC 179 ms
90,544 KB
testcase_39 AC 167 ms
87,984 KB
testcase_40 AC 156 ms
88,484 KB
testcase_41 AC 177 ms
95,388 KB
testcase_42 AC 132 ms
85,436 KB
testcase_43 AC 149 ms
86,128 KB
testcase_44 AC 133 ms
82,504 KB
testcase_45 AC 114 ms
79,424 KB
testcase_46 AC 173 ms
89,768 KB
testcase_47 AC 138 ms
85,432 KB
testcase_48 AC 148 ms
84,232 KB
testcase_49 AC 109 ms
80,676 KB
testcase_50 AC 147 ms
87,376 KB
testcase_51 AC 36 ms
55,612 KB
testcase_52 AC 137 ms
86,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Input_kyopro:
    def II(self): return int(input())
    def MI(self): return map(int,input().split())
    def MS(self): return map(str,input().split())
    def LMI(self): return list(self.MI())
    def LMS(self): return list(self.MS())
    def LLI(self,N): return [self.LMI() for _ in range(N)]
    def LLS(self,N): return [self.LMS() for _ in range(N)]
    def LS(self,N): return [input() for _ in range(N)]
    def LSL(self,N): return [list(input()) for _ in range(N)]
    def LI(self,N): return [self.II() for _ in range(N)]
I=Input_kyopro()
#入力
from collections import deque
N,M=I.MI()
uv=I.LLI(M)
G=[[] for _ in range(N)]
for u,v in uv:
    u-=1
    v-=1
    G[u].append(v)
q=deque()
q.append(0)
dist=[-1]*N
dist[0]=0
while q:
    pos=q.popleft()
    for nex in G[pos]:
        if dist[nex]!=-1:
            continue
        dist[nex]=dist[pos]+1
        q.append(nex)
q=deque()
q.append(N-1)
dist1=[-1]*N
dist1[N-1]=0
while q:
    pos=q.popleft()
    for nex in G[pos]:
        if dist1[nex]!=-1:
            continue
        dist1[nex]=dist1[pos]+1
        q.append(nex)
q=deque()
q.append(N-2)
dist2=[-1]*N
dist2[N-2]=0
while q:
    pos=q.popleft()
    for nex in G[pos]:
        if dist2[nex]!=-1:
            continue
        dist2[nex]=dist2[pos]+1
        q.append(nex)
ans=float('inf')
if dist[-1]==-1 or dist[-2]==-1:
    print(-1)
else:
    if dist1[N-2]!=-1 and dist2[0]!=-1:
        ans=min(ans,dist[N-1]+dist1[N-2]+dist2[0])
    if dist2[N-1]!=-1 and dist1[0]!=-1:
        ans=min(ans,dist[N-2]+dist2[N-1]+dist1[0])
    if ans==float('inf'):
        print(-1)
    else:
        print(ans)
0