結果

問題 No.2565 はじめてのおつかい
ユーザー hatonobuhatonobu
提出日時 2023-12-02 17:40:26
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,917 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,712 KB
最終ジャッジ日時 2023-12-02 17:40:38
合計ジャッジ時間 9,322 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,596 KB
testcase_01 AC 43 ms
55,596 KB
testcase_02 AC 44 ms
55,596 KB
testcase_03 AC 220 ms
89,712 KB
testcase_04 AC 114 ms
89,584 KB
testcase_05 AC 44 ms
55,596 KB
testcase_06 AC 144 ms
79,484 KB
testcase_07 AC 114 ms
78,180 KB
testcase_08 WA -
testcase_09 AC 141 ms
78,692 KB
testcase_10 AC 140 ms
79,748 KB
testcase_11 AC 209 ms
84,804 KB
testcase_12 AC 98 ms
76,900 KB
testcase_13 WA -
testcase_14 AC 184 ms
82,072 KB
testcase_15 AC 175 ms
81,368 KB
testcase_16 AC 118 ms
84,916 KB
testcase_17 AC 73 ms
77,284 KB
testcase_18 AC 78 ms
79,508 KB
testcase_19 AC 220 ms
83,436 KB
testcase_20 AC 173 ms
82,516 KB
testcase_21 AC 150 ms
82,012 KB
testcase_22 AC 86 ms
79,216 KB
testcase_23 AC 127 ms
80,736 KB
testcase_24 AC 60 ms
70,756 KB
testcase_25 AC 167 ms
82,792 KB
testcase_26 AC 136 ms
80,444 KB
testcase_27 AC 117 ms
82,064 KB
testcase_28 AC 166 ms
83,032 KB
testcase_29 AC 191 ms
84,860 KB
testcase_30 AC 120 ms
82,628 KB
testcase_31 AC 173 ms
83,176 KB
testcase_32 AC 91 ms
78,920 KB
testcase_33 AC 118 ms
82,348 KB
testcase_34 AC 170 ms
81,952 KB
testcase_35 AC 150 ms
83,624 KB
testcase_36 AC 177 ms
83,496 KB
testcase_37 AC 132 ms
80,596 KB
testcase_38 AC 115 ms
80,932 KB
testcase_39 AC 195 ms
81,636 KB
testcase_40 AC 132 ms
83,060 KB
testcase_41 AC 170 ms
85,300 KB
testcase_42 AC 153 ms
80,040 KB
testcase_43 AC 112 ms
80,780 KB
testcase_44 AC 128 ms
79,092 KB
testcase_45 AC 76 ms
77,284 KB
testcase_46 AC 170 ms
81,252 KB
testcase_47 AC 120 ms
78,308 KB
testcase_48 AC 140 ms
79,592 KB
testcase_49 AC 97 ms
76,900 KB
testcase_50 AC 130 ms
78,052 KB
testcase_51 AC 44 ms
55,596 KB
testcase_52 AC 88 ms
76,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque,defaultdict
import itertools
import heapq
import bisect
import math

#sys.setrecursionlimit(10 ** 9)
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
ml = lambda: map(str, input().split())
li = lambda: list(mi())
li_st = lambda: list(map(str, input().split()))
lli = lambda n: [li() for _ in range(n)]
mod = 998244353
INF = 10**18

N,M = mi()
G = [[] for _ in range(N)]

for i in range(M):
    u,v = mi()
    u -= 1
    v -= 1
    G[u].append(v)

check = [INF] * N
check2 = [INF] * N
check3 = [INF] * N
q = deque()
q.append((0,0,-1))
check[0] = 0

while(q):
    now,times,flag = q.popleft()
    for nxt in G[now]:
        if check[nxt] == INF:
            q.append((nxt,times+1,flag))
            check[nxt] = min(times + 1, check[nxt])
        else:
            check[nxt] = min(times+1,check[nxt])

if check[N-2] != INF:
    q = deque()
    q.append((N-2,0))
    check2[N-2] =  0
    while(q):
        now,times = q.popleft()          
        for nxt in G[now]:
            if check2[nxt] == INF:
                q.append((nxt,times+1))
                check2[nxt] = min(check2[nxt],times + 1)
            else:
                check2[nxt] = min(check2[nxt],times+1)
        if now == 0:
            break

if check[N-1] != INF:
    q = deque()
    q.append((N-1,0))
    check3[N-1] =  0
    while(q):
        now,times = q.popleft()
        for nxt in G[now]:
            if check3[nxt] == INF:
                q.append((nxt,times+1))
                check3[nxt] = min(check3[nxt],times + 1)
            else:
                check3[nxt] = min(check3[nxt],times+1)
        if now == 0:
            break

ans1 = check[N-1] + check3[N-2] + check2[0]
ans2 = check[N-2] + check2[N-1] + check3[0]

#print(check)
#print(check2)
#print(check3)
#print(ans1,ans2)
print(min(ans1,ans2) if min(ans1,ans2) < INF else -1)
0