結果

問題 No.2565 はじめてのおつかい
ユーザー mkawa2mkawa2
提出日時 2023-12-02 15:23:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 1,328 bytes
コンパイル時間 636 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,728 KB
最終ジャッジ日時 2023-12-02 15:23:32
合計ジャッジ時間 8,214 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,608 KB
testcase_01 AC 38 ms
55,608 KB
testcase_02 AC 40 ms
55,608 KB
testcase_03 AC 169 ms
89,728 KB
testcase_04 AC 114 ms
89,728 KB
testcase_05 AC 37 ms
55,608 KB
testcase_06 AC 123 ms
79,880 KB
testcase_07 AC 100 ms
78,064 KB
testcase_08 AC 105 ms
78,856 KB
testcase_09 AC 112 ms
78,832 KB
testcase_10 AC 111 ms
79,252 KB
testcase_11 AC 152 ms
83,920 KB
testcase_12 AC 81 ms
77,040 KB
testcase_13 AC 109 ms
78,832 KB
testcase_14 AC 136 ms
81,960 KB
testcase_15 AC 144 ms
81,000 KB
testcase_16 AC 119 ms
85,312 KB
testcase_17 AC 64 ms
76,912 KB
testcase_18 AC 73 ms
79,008 KB
testcase_19 AC 133 ms
82,808 KB
testcase_20 AC 128 ms
82,144 KB
testcase_21 AC 119 ms
82,152 KB
testcase_22 AC 100 ms
80,252 KB
testcase_23 AC 105 ms
80,368 KB
testcase_24 AC 66 ms
76,912 KB
testcase_25 AC 158 ms
82,676 KB
testcase_26 AC 107 ms
79,948 KB
testcase_27 AC 112 ms
82,716 KB
testcase_28 AC 140 ms
82,660 KB
testcase_29 AC 146 ms
84,744 KB
testcase_30 AC 117 ms
83,536 KB
testcase_31 AC 135 ms
82,676 KB
testcase_32 AC 106 ms
80,088 KB
testcase_33 AC 132 ms
83,256 KB
testcase_34 AC 160 ms
81,964 KB
testcase_35 AC 126 ms
84,660 KB
testcase_36 AC 160 ms
83,380 KB
testcase_37 AC 99 ms
80,228 KB
testcase_38 AC 125 ms
81,840 KB
testcase_39 AC 142 ms
80,888 KB
testcase_40 AC 128 ms
84,224 KB
testcase_41 AC 143 ms
85,056 KB
testcase_42 AC 112 ms
79,672 KB
testcase_43 AC 115 ms
81,180 KB
testcase_44 AC 103 ms
78,980 KB
testcase_45 AC 87 ms
77,424 KB
testcase_46 AC 145 ms
81,268 KB
testcase_47 AC 105 ms
78,704 KB
testcase_48 AC 112 ms
78,968 KB
testcase_49 AC 83 ms
77,040 KB
testcase_50 AC 114 ms
79,088 KB
testcase_51 AC 38 ms
55,608 KB
testcase_52 AC 92 ms
76,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = -1-(-1 << 31)
# inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

from collections import deque

def bfs(u):
    dist=[inf]*n
    dist[u]=0
    q=deque([u])
    while q:
        u=q.popleft()
        for v in to[u]:
            if dist[v]==inf:
                dist[v]=dist[u]+1
                q.append(v)
    return dist

n,m=LI()
to=[[] for _ in range(n)]
for _ in range(m):
    u,v=LI1()
    to[u].append(v)

# 0,n-2,n-1,0
# 0,n-1,n-2,0
dist=bfs(0)
d=[dist[n-2],dist[n-1]]

dist=bfs(n-2)
d[0]+=dist[n-1]
d[1]+=dist[0]

dist=bfs(n-1)
d[0]+=dist[0]
d[1]+=dist[n-2]

ans=min(d)
if ans>=inf:
    print(-1)
else:
    print(ans)
0