結果
問題 | No.2565 はじめてのおつかい |
ユーザー |
![]() |
提出日時 | 2023-12-02 14:59:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 208 ms / 2,000 ms |
コード長 | 1,111 bytes |
コンパイル時間 | 293 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 89,984 KB |
最終ジャッジ日時 | 2024-09-26 17:49:53 |
合計ジャッジ時間 | 10,394 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 50 |
ソースコード
#############################################################import syssys.setrecursionlimit(10**7)from heapq import heappop,heappushfrom collections import deque,defaultdict,Counterfrom bisect import bisect_left, bisect_rightfrom itertools import product,combinations,permutationsfrom math import sin,cos#from math import isqrt #DO NOT USE PyPyipt = sys.stdin.readlineiin = lambda :int(ipt())lmin = lambda :list(map(int,ipt().split()))INF = 1<<30def bfs01(s,N,G):dq = deque()dq.append(s)cost = [INF]*Ncost[s] = 0while dq:cur = dq.pop()for nxt in G[cur]:if cost[nxt] != INF:continuecost[nxt] = cost[cur]+1dq.appendleft(nxt)return cost#############################################################N,M = lmin()G = [[] for _ in range(N)]for _ in range(M):a,b = lmin()G[a-1].append(b-1)cost1 = bfs01(0,N,G)cost2 = bfs01(N-2,N,G)cost3 = bfs01(N-1,N,G)ans = min(cost1[N-2]+cost2[N-1]+cost3[0],cost1[N-1]+cost3[N-2]+cost2[0])if ans >= INF:print(-1)else:print(ans)