結果

問題 No.2565 はじめてのおつかい
ユーザー yaakiyuyaakiyu
提出日時 2023-12-02 15:25:52
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 921 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,400 KB
最終ジャッジ日時 2023-12-02 15:26:00
合計ジャッジ時間 7,242 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,460 KB
testcase_01 AC 34 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 142 ms
89,400 KB
testcase_04 AC 101 ms
89,400 KB
testcase_05 RE -
testcase_06 AC 111 ms
80,324 KB
testcase_07 AC 81 ms
77,996 KB
testcase_08 AC 92 ms
78,912 KB
testcase_09 AC 107 ms
79,916 KB
testcase_10 AC 95 ms
79,308 KB
testcase_11 AC 130 ms
84,236 KB
testcase_12 AC 76 ms
77,228 KB
testcase_13 AC 91 ms
79,020 KB
testcase_14 AC 117 ms
81,508 KB
testcase_15 AC 115 ms
81,444 KB
testcase_16 RE -
testcase_17 AC 51 ms
73,132 KB
testcase_18 RE -
testcase_19 AC 120 ms
83,256 KB
testcase_20 AC 112 ms
82,208 KB
testcase_21 AC 102 ms
82,216 KB
testcase_22 AC 81 ms
79,416 KB
testcase_23 AC 95 ms
80,428 KB
testcase_24 AC 54 ms
70,736 KB
testcase_25 AC 118 ms
82,740 KB
testcase_26 AC 92 ms
80,008 KB
testcase_27 AC 103 ms
82,648 KB
testcase_28 AC 117 ms
83,108 KB
testcase_29 AC 129 ms
84,804 KB
testcase_30 AC 104 ms
83,212 KB
testcase_31 AC 123 ms
82,228 KB
testcase_32 AC 96 ms
80,020 KB
testcase_33 AC 106 ms
82,928 KB
testcase_34 AC 126 ms
82,284 KB
testcase_35 AC 102 ms
83,824 KB
testcase_36 AC 135 ms
82,800 KB
testcase_37 AC 96 ms
79,776 KB
testcase_38 AC 120 ms
82,292 KB
testcase_39 AC 117 ms
81,072 KB
testcase_40 AC 117 ms
83,900 KB
testcase_41 AC 130 ms
85,244 KB
testcase_42 AC 91 ms
79,476 KB
testcase_43 AC 107 ms
81,496 KB
testcase_44 AC 91 ms
78,656 KB
testcase_45 AC 69 ms
77,612 KB
testcase_46 AC 122 ms
81,708 KB
testcase_47 AC 96 ms
79,532 KB
testcase_48 AC 95 ms
78,772 KB
testcase_49 AC 77 ms
77,232 KB
testcase_50 AC 99 ms
79,660 KB
testcase_51 AC 36 ms
53,460 KB
testcase_52 AC 79 ms
78,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(1000000)

import heapq

n, m = map(int, input().split())
G = [[] for i in range(n)]

for i in range(m):
    u, v = map(int, input().split())
    G[u-1].append(v-1)


def bfs(u, v, v2):
    q = [u]
    dists = [-1]*m
    dists[u] = 0
    now = 1
    while q:
        new_q = []
        for i in q:
            for j in G[i]:
                if dists[j] == -1:
                    new_q.append(j)
                    dists[j] = now
        now += 1
        q = new_q
    return dists[v], dists[v2]

p, pp = bfs(0, n-1, n-2)
ppp, pppp = bfs(n-1, 0, n-2)
ppppp, pppppp = bfs(n-2, 0, n-1)

if p == -1 or pppp == -1 or ppppp == -1:
    if pp == -1 or ppp == -1 or pppppp == -1:
        print(-1)
    else:
        print(pp+ppp+pppppp)
elif pp == -1 or ppp == -1 or pppppp == -1:
    print(p+pppp+ppppp)
else:
    print(min(p+pppp+ppppp, pp+ppp+pppppp))
0