結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,460 KB
testcase_01 AC 39 ms
53,460 KB
testcase_02 AC 39 ms
53,460 KB
testcase_03 AC 131 ms
89,272 KB
testcase_04 AC 103 ms
89,400 KB
testcase_05 RE -
testcase_06 AC 106 ms
80,324 KB
testcase_07 AC 89 ms
77,996 KB
testcase_08 AC 97 ms
78,912 KB
testcase_09 AC 109 ms
79,916 KB
testcase_10 AC 101 ms
79,308 KB
testcase_11 AC 128 ms
84,236 KB
testcase_12 AC 77 ms
77,228 KB
testcase_13 AC 93 ms
79,020 KB
testcase_14 AC 116 ms
81,508 KB
testcase_15 AC 114 ms
81,444 KB
testcase_16 RE -
testcase_17 AC 56 ms
73,132 KB
testcase_18 RE -
testcase_19 AC 122 ms
83,256 KB
testcase_20 AC 108 ms
82,208 KB
testcase_21 AC 101 ms
82,216 KB
testcase_22 AC 83 ms
79,416 KB
testcase_23 AC 96 ms
80,428 KB
testcase_24 AC 58 ms
70,732 KB
testcase_25 AC 118 ms
82,740 KB
testcase_26 AC 93 ms
80,008 KB
testcase_27 AC 106 ms
82,648 KB
testcase_28 AC 118 ms
83,108 KB
testcase_29 AC 130 ms
84,804 KB
testcase_30 AC 104 ms
83,212 KB
testcase_31 AC 118 ms
82,228 KB
testcase_32 AC 98 ms
80,020 KB
testcase_33 AC 108 ms
82,928 KB
testcase_34 AC 124 ms
82,284 KB
testcase_35 AC 102 ms
83,824 KB
testcase_36 AC 130 ms
82,800 KB
testcase_37 AC 95 ms
79,776 KB
testcase_38 AC 116 ms
82,292 KB
testcase_39 AC 117 ms
81,072 KB
testcase_40 AC 141 ms
83,900 KB
testcase_41 AC 128 ms
85,244 KB
testcase_42 AC 92 ms
79,476 KB
testcase_43 AC 111 ms
81,496 KB
testcase_44 AC 96 ms
78,656 KB
testcase_45 AC 73 ms
77,612 KB
testcase_46 AC 124 ms
81,836 KB
testcase_47 AC 97 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 77 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=None):
    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
    if v2:
        return dists[v], dists[v2]
    return dists[v]

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