結果

問題 No.2565 はじめてのおつかい
ユーザー YuuuTYuuuT
提出日時 2023-12-02 15:59:04
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,137 bytes
コンパイル時間 499 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 114,944 KB
最終ジャッジ日時 2024-09-26 19:38:30
合計ジャッジ時間 11,532 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
55,168 KB
testcase_01 AC 51 ms
54,912 KB
testcase_02 AC 51 ms
55,040 KB
testcase_03 AC 302 ms
114,944 KB
testcase_04 AC 192 ms
114,176 KB
testcase_05 AC 52 ms
55,040 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 342 ms
97,868 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 241 ms
89,344 KB
testcase_16 AC 148 ms
91,904 KB
testcase_17 AC 92 ms
78,976 KB
testcase_18 AC 98 ms
81,920 KB
testcase_19 AC 275 ms
93,056 KB
testcase_20 AC 254 ms
91,412 KB
testcase_21 AC 216 ms
92,672 KB
testcase_22 AC 108 ms
81,664 KB
testcase_23 AC 195 ms
87,552 KB
testcase_24 AC 74 ms
71,808 KB
testcase_25 AC 275 ms
92,800 KB
testcase_26 AC 200 ms
87,552 KB
testcase_27 AC 144 ms
87,936 KB
testcase_28 AC 235 ms
92,672 KB
testcase_29 AC 248 ms
97,536 KB
testcase_30 AC 147 ms
89,472 KB
testcase_31 AC 259 ms
92,020 KB
testcase_32 AC 108 ms
81,280 KB
testcase_33 AC 146 ms
88,576 KB
testcase_34 AC 257 ms
90,672 KB
testcase_35 AC 150 ms
90,240 KB
testcase_36 AC 288 ms
93,204 KB
testcase_37 AC 193 ms
87,296 KB
testcase_38 AC 144 ms
86,272 KB
testcase_39 AC 258 ms
89,232 KB
testcase_40 AC 165 ms
93,056 KB
testcase_41 AC 254 ms
98,816 KB
testcase_42 AC 169 ms
86,528 KB
testcase_43 AC 135 ms
86,016 KB
testcase_44 AC 191 ms
83,840 KB
testcase_45 AC 92 ms
79,104 KB
testcase_46 WA -
testcase_47 AC 136 ms
79,872 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 50 ms
55,296 KB
testcase_52 AC 101 ms
78,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,math
#if len(sys.argv)==2:sys.stdin=open(sys.argv[1])
from collections import defaultdict,deque
from itertools import combinations,permutations,accumulate,product
from bisect import bisect,bisect_left,bisect_right
from heapq import heappop,heappush,heapify
#from sortedcontainers import SortedList,SortedSet
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def ms(): return map(int, input().split())
def li(): return list(map(int,input().split()))
N,M = ms()
G = [set() for _ in range(N+1)]
for _ in range(M):
  u,v = ms()
  G[u].add(v)
  
ans = 10**18
tmp = 0
ng = False
visited = [False]*(N+1)
visited[1] = True
dist = [-1]*(N+1)
dist[1] = 0
que = deque()
que.append(1)
end = False
while que:
  v = que.popleft()
  visited[v] = True
  for v2 in G[v]:
    if visited[v2]: continue
    dist[v2] = dist[v] + 1
    if v2==N-1:
      end = True
      break
    que.append(v2)
  if end: break

if end==False: ng = True
if ng==False:
  tmp += dist[N-1]
  visited = [False]*(N+1)
  visited[N-1] = True
  dist = [-1]*(N+1)
  dist[N-1] = 0
  que = deque()
  que.append(N-1)
  end = False
  while que:
    v = que.popleft()
    visited[v] = True
    for v2 in G[v]:
      if visited[v2]: continue
      dist[v2] = dist[v] + 1
      if v2==N:
        end = True
        break
      que.append(v2)
    if end: break
  if end==False: ng = True
    
  tmp += dist[N]
  end = False
  visited = [False]*(N+1)
  visited[N] = True
  dist = [-1]*(N+1)
  dist[N] = 0
  que = deque()
  que.append(N)
  end = False
  while que:
    v = que.popleft()
    visited[v] = True
    for v2 in G[v]:
      if visited[v2]: continue
      dist[v2] = dist[v] + 1
      if v2==1:
        end = True
        break
      que.append(v2)
    if end: break
  if end==False: ng = True
  
  tmp += dist[1]
  if ng==False: ans = min(ans,tmp)
  
tmp = 0
ng = False
visited = [False]*(N+1)
visited[1] = True
dist = [-1]*(N+1)
dist[1] = 0
que = deque()
que.append(1)
end = False
while que:
  v = que.popleft()
  visited[v] = True
  for v2 in G[v]:
    if visited[v2]: continue
    dist[v2] = dist[v] + 1
    if v2==N:
      end = True
      break
    que.append(v2)
  if end: break

if end==False: ng = True
if ng==False:
  tmp += dist[N]
  visited = [False]*(N+1)
  visited[N] = True
  dist = [-1]*(N+1)
  dist[N] = 0
  que = deque()
  que.append(N)
  end = False
  while que:
    v = que.popleft()
    visited[v] = True
    for v2 in G[v]:
      if visited[v2]: continue
      dist[v2] = dist[v] + 1
      if v2==N-1:
        end = True
        break
      que.append(v2)
    if end: break
  if end==False: ng = True
    
  tmp += dist[N-1]
  end = False
  visited = [False]*(N+1)
  visited[N-1] = True
  dist = [-1]*(N+1)
  dist[N-1] = 0
  que = deque()
  que.append(N-1)
  end = False
  while que:
    v = que.popleft()
    visited[v] = True
    for v2 in G[v]:
      if visited[v2]: continue
      dist[v2] = dist[v] + 1
      if v2==1:
        end = True
        break
      que.append(v2)
    if end: break
  if end==False: ng = True
  
  tmp += dist[1]
  if ng==False: ans = min(ans,tmp)

if ans!=10**18: print(ans)
else: print(-1)
0