結果

問題 No.2565 はじめてのおつかい
ユーザー とろちゃとろちゃ
提出日時 2023-12-02 15:57:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 1,938 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 120,808 KB
最終ジャッジ日時 2023-12-02 15:57:53
合計ジャッジ時間 9,024 ms
ジャッジサーバーID
(参考情報)
judge15 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
55,608 KB
testcase_01 AC 42 ms
55,608 KB
testcase_02 AC 43 ms
55,608 KB
testcase_03 AC 236 ms
120,804 KB
testcase_04 AC 198 ms
120,808 KB
testcase_05 AC 38 ms
55,608 KB
testcase_06 AC 141 ms
91,064 KB
testcase_07 AC 100 ms
83,744 KB
testcase_08 AC 105 ms
85,736 KB
testcase_09 AC 126 ms
88,724 KB
testcase_10 AC 116 ms
85,416 KB
testcase_11 AC 183 ms
104,504 KB
testcase_12 AC 83 ms
79,456 KB
testcase_13 AC 133 ms
82,484 KB
testcase_14 AC 152 ms
95,220 KB
testcase_15 AC 156 ms
94,500 KB
testcase_16 AC 134 ms
100,276 KB
testcase_17 AC 67 ms
77,152 KB
testcase_18 AC 69 ms
80,244 KB
testcase_19 AC 171 ms
101,016 KB
testcase_20 AC 152 ms
95,168 KB
testcase_21 AC 144 ms
95,932 KB
testcase_22 AC 130 ms
85,808 KB
testcase_23 AC 111 ms
83,940 KB
testcase_24 AC 75 ms
77,388 KB
testcase_25 AC 166 ms
97,840 KB
testcase_26 AC 125 ms
90,464 KB
testcase_27 AC 151 ms
95,808 KB
testcase_28 AC 185 ms
100,756 KB
testcase_29 AC 189 ms
106,840 KB
testcase_30 AC 181 ms
101,744 KB
testcase_31 AC 166 ms
99,032 KB
testcase_32 AC 114 ms
83,780 KB
testcase_33 AC 155 ms
93,776 KB
testcase_34 AC 163 ms
98,008 KB
testcase_35 AC 141 ms
91,628 KB
testcase_36 AC 190 ms
101,448 KB
testcase_37 AC 111 ms
83,792 KB
testcase_38 AC 196 ms
98,532 KB
testcase_39 AC 153 ms
93,284 KB
testcase_40 AC 165 ms
98,520 KB
testcase_41 AC 192 ms
106,320 KB
testcase_42 AC 137 ms
89,328 KB
testcase_43 AC 154 ms
95,780 KB
testcase_44 AC 118 ms
86,396 KB
testcase_45 AC 94 ms
81,908 KB
testcase_46 AC 157 ms
96,092 KB
testcase_47 AC 113 ms
83,308 KB
testcase_48 AC 114 ms
83,516 KB
testcase_49 AC 85 ms
79,448 KB
testcase_50 AC 120 ms
85,380 KB
testcase_51 AC 37 ms
55,608 KB
testcase_52 AC 114 ms
88,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# import pypyjit;pypyjit.set_param("max_unroll_recursion=-1")
import os
import sys

# from bisect import *
from collections import *

# from datetime import *
# from decimal import *  # PyPyだと遅い
# from heapq import *
from itertools import *

# from math import gcd, lcm
# from random import *
# from string import *

# import numpy as np
# from atcoder.dsu import *
# from atcoder.segtree import *
# from more_itertools import *
# from sortedcontainers import *

# sys.setrecursionlimit(10**7)  # PyPyは呪文を付ける
# sys.set_int_max_str_digits(0)
INF = 1 << 61
MOD = 998244353
# MOD = 10**9 + 7
IS_ATCODER = os.getenv("ATCODER", 0)

File = sys.stdin


def input():
    return File.readline().replace("\n", "")


# ///////////////////////////////////////////////////////////////////////////
def bfs(G, start):
    """
    グラフで幅優先探索をする関数
    seenとdistを配列で管理
    何度も繰り返す場合はseenの生成がO(N)なので
    辞書で管理するものを使う

    引数:
    G (defaultdict): グラフ
    start (int): 始点

    戻り値:
    dist (list): 始点からの距離
    """
    que = deque()
    seen = [0] * N
    dist = [INF] * N

    que.append(start)
    seen[start] = True
    dist[start] = 0
    while que:
        for _ in range(len(que)):
            pop = que.popleft()
            for i in G[pop]:
                if seen[i] == False:
                    seen[i] = True
                    dist[i] = dist[pop] + 1
                    que.append(i)

    return dist


N, M = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(M)]

G = [set() for _ in range(N)]
for i, j in edges:
    G[i - 1].add(j - 1)

dist1 = bfs(G, 0)
dist2 = bfs(G, N - 1)
dist3 = bfs(G, N - 2)
ans = INF

ans = min(ans, dist1[N - 1] + dist2[N - 2] + dist3[0])
ans = min(ans, dist1[N - 2] + dist3[N - 1] + dist2[0])

print(ans if ans != INF else -1)
0