結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
55,168 KB
testcase_01 AC 49 ms
55,168 KB
testcase_02 AC 50 ms
55,040 KB
testcase_03 AC 268 ms
113,152 KB
testcase_04 AC 178 ms
112,512 KB
testcase_05 AC 50 ms
55,168 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 313 ms
97,340 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 149 ms
91,776 KB
testcase_17 AC 90 ms
78,720 KB
testcase_18 AC 96 ms
81,792 KB
testcase_19 AC 257 ms
92,800 KB
testcase_20 AC 251 ms
91,392 KB
testcase_21 AC 214 ms
91,776 KB
testcase_22 AC 108 ms
81,280 KB
testcase_23 AC 208 ms
87,040 KB
testcase_24 AC 76 ms
71,552 KB
testcase_25 AC 278 ms
92,544 KB
testcase_26 AC 202 ms
86,656 KB
testcase_27 AC 144 ms
88,448 KB
testcase_28 AC 239 ms
93,056 KB
testcase_29 WA -
testcase_30 AC 155 ms
88,960 KB
testcase_31 AC 271 ms
92,288 KB
testcase_32 AC 111 ms
81,408 KB
testcase_33 AC 145 ms
88,192 KB
testcase_34 AC 267 ms
90,420 KB
testcase_35 AC 155 ms
89,984 KB
testcase_36 AC 295 ms
92,940 KB
testcase_37 AC 197 ms
87,424 KB
testcase_38 AC 145 ms
86,144 KB
testcase_39 AC 258 ms
88,340 KB
testcase_40 AC 173 ms
92,672 KB
testcase_41 AC 262 ms
98,688 KB
testcase_42 AC 161 ms
84,992 KB
testcase_43 AC 142 ms
85,888 KB
testcase_44 AC 192 ms
83,456 KB
testcase_45 AC 92 ms
79,488 KB
testcase_46 WA -
testcase_47 AC 132 ms
79,616 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 52 ms
55,168 KB
testcase_52 AC 102 ms
78,976 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]
  if visited[N]==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==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]
  if visited[N-1]==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==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