結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
55,596 KB
testcase_01 AC 44 ms
55,596 KB
testcase_02 AC 45 ms
55,596 KB
testcase_03 AC 170 ms
89,712 KB
testcase_04 AC 116 ms
89,456 KB
testcase_05 AC 45 ms
55,596 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 114 ms
84,788 KB
testcase_17 AC 70 ms
77,156 KB
testcase_18 AC 77 ms
79,380 KB
testcase_19 AC 159 ms
83,052 KB
testcase_20 AC 171 ms
81,876 KB
testcase_21 AC 162 ms
82,780 KB
testcase_22 AC 88 ms
79,088 KB
testcase_23 AC 127 ms
80,608 KB
testcase_24 AC 60 ms
70,628 KB
testcase_25 AC 146 ms
82,664 KB
testcase_26 AC 122 ms
80,188 KB
testcase_27 AC 108 ms
81,936 KB
testcase_28 AC 159 ms
83,032 KB
testcase_29 AC 181 ms
84,604 KB
testcase_30 AC 112 ms
82,500 KB
testcase_31 AC 155 ms
83,048 KB
testcase_32 AC 85 ms
78,792 KB
testcase_33 AC 116 ms
82,220 KB
testcase_34 AC 157 ms
82,336 KB
testcase_35 AC 117 ms
83,496 KB
testcase_36 AC 155 ms
83,240 KB
testcase_37 AC 131 ms
80,468 KB
testcase_38 AC 114 ms
80,804 KB
testcase_39 AC 137 ms
80,612 KB
testcase_40 AC 122 ms
82,932 KB
testcase_41 AC 167 ms
85,300 KB
testcase_42 AC 130 ms
80,040 KB
testcase_43 AC 109 ms
80,524 KB
testcase_44 AC 117 ms
79,604 KB
testcase_45 AC 72 ms
77,156 KB
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

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 = [-1] * N
check2 = [-1] * N
check3 = [-1] * N
q = deque()
q.append((0,0,-1))
check[0] = 0

while(q):
    now,times,flag = q.popleft()
    for nxt in G[now]:
        if nxt == N-1 or nxt == N-2:
            if flag != -1 and flag != nxt:
                check[nxt] = times+1
            else:
                flag = now
                q.append((nxt,times+1,flag))
                
        else:
            if check[nxt] == -1:
                q.append((nxt,times+1,flag))
                check[nxt] = times + 1
            else:
                check[nxt] = times+1

if check[N-2] != -1:
    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] == -1:
                q.append((nxt,times+1))
                check2[nxt] = times + 1
            else:
                check2[nxt] = min(check2[nxt],times+1)
        if now == 0:
            break

if check[N-1] != -1:
    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] == -1:
                q.append((nxt,times+1))
                check3[nxt] = times + 1
            else:
                check3[nxt] = min(check3[nxt],times+1)
        if now == 0:
            break

ans = INF
#print(check)
##print(check2)
#print(check3)
if check[N-2] != -1 and check2[0] != -1:
    ans = min(check[N-2] + check2[0],ans)
elif check[N-1] != -1 and check3[0] != -1:
    ans = min(check[N-1] + check3[0],ans)

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