結果

問題 No.2565 はじめてのおつかい
ユーザー mymelochanmymelochan
提出日時 2023-12-02 14:59:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,111 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,212 KB
最終ジャッジ日時 2023-12-02 14:59:21
合計ジャッジ時間 8,712 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,596 KB
testcase_01 AC 41 ms
55,596 KB
testcase_02 AC 37 ms
55,596 KB
testcase_03 AC 151 ms
89,212 KB
testcase_04 AC 169 ms
89,212 KB
testcase_05 AC 40 ms
55,596 KB
testcase_06 AC 126 ms
79,760 KB
testcase_07 AC 92 ms
78,328 KB
testcase_08 AC 100 ms
78,860 KB
testcase_09 AC 110 ms
78,968 KB
testcase_10 AC 108 ms
79,384 KB
testcase_11 AC 149 ms
84,556 KB
testcase_12 AC 78 ms
77,056 KB
testcase_13 AC 130 ms
78,840 KB
testcase_14 AC 160 ms
81,708 KB
testcase_15 AC 133 ms
80,876 KB
testcase_16 AC 101 ms
85,564 KB
testcase_17 AC 63 ms
77,176 KB
testcase_18 AC 66 ms
79,272 KB
testcase_19 AC 127 ms
82,936 KB
testcase_20 AC 127 ms
82,144 KB
testcase_21 AC 113 ms
82,280 KB
testcase_22 AC 87 ms
80,004 KB
testcase_23 AC 103 ms
80,372 KB
testcase_24 AC 70 ms
77,056 KB
testcase_25 AC 182 ms
83,316 KB
testcase_26 AC 100 ms
80,080 KB
testcase_27 AC 108 ms
82,712 KB
testcase_28 AC 133 ms
82,788 KB
testcase_29 AC 145 ms
84,484 KB
testcase_30 AC 119 ms
83,280 KB
testcase_31 AC 128 ms
82,804 KB
testcase_32 AC 124 ms
80,220 KB
testcase_33 AC 125 ms
83,508 KB
testcase_34 AC 147 ms
82,100 KB
testcase_35 AC 121 ms
84,404 KB
testcase_36 AC 143 ms
83,504 KB
testcase_37 AC 133 ms
80,360 KB
testcase_38 AC 130 ms
81,976 KB
testcase_39 AC 133 ms
81,016 KB
testcase_40 AC 142 ms
84,348 KB
testcase_41 AC 144 ms
85,180 KB
testcase_42 AC 109 ms
79,804 KB
testcase_43 AC 129 ms
81,312 KB
testcase_44 AC 105 ms
79,112 KB
testcase_45 AC 95 ms
78,072 KB
testcase_46 AC 130 ms
81,272 KB
testcase_47 AC 117 ms
78,968 KB
testcase_48 AC 110 ms
78,972 KB
testcase_49 AC 84 ms
77,048 KB
testcase_50 AC 113 ms
79,096 KB
testcase_51 AC 38 ms
55,596 KB
testcase_52 AC 85 ms
77,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#############################################################

import sys
sys.setrecursionlimit(10**7)

from heapq import heappop,heappush
from collections import deque,defaultdict,Counter
from bisect import bisect_left, bisect_right
from itertools import product,combinations,permutations
from math import sin,cos
#from math import isqrt #DO NOT USE PyPy

ipt = sys.stdin.readline
iin = lambda :int(ipt())
lmin = lambda :list(map(int,ipt().split()))

INF = 1<<30

def bfs01(s,N,G):
    dq = deque()
    dq.append(s)
    cost = [INF]*N
    cost[s] = 0
    while dq:
        cur = dq.pop()
        for nxt in G[cur]:
            if cost[nxt] != INF:
                continue
            cost[nxt] = cost[cur]+1
            dq.appendleft(nxt)
    return cost
#############################################################

N,M = lmin()
G = [[] for _ in range(N)]
for _ in range(M):
    a,b = lmin()
    G[a-1].append(b-1)

cost1 = bfs01(0,N,G)
cost2 = bfs01(N-2,N,G)
cost3 = bfs01(N-1,N,G)

ans = min(cost1[N-2]+cost2[N-1]+cost3[0],cost1[N-1]+cost3[N-2]+cost2[0])
if ans >= INF:
    print(-1)
else:
    print(ans)
0