結果
| 問題 |
No.2565 はじめてのおつかい
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-04 14:56:19 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 253 ms / 2,000 ms |
| コード長 | 1,521 bytes |
| コンパイル時間 | 692 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 91,264 KB |
| 最終ジャッジ日時 | 2024-09-26 23:00:41 |
| 合計ジャッジ時間 | 12,576 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 50 |
ソースコード
#!/usr/bin/env python3
import sys
from bisect import bisect_left, bisect_right, insort_left, insort_right # type: ignore
from collections import Counter, defaultdict, deque # type: ignore
from math import gcd, sqrt, ceil, factorial # type: ignore
from heapq import heapify, heappop, heappush, heappushpop, heapreplace, merge # type: ignore
from itertools import accumulate, combinations, permutations, product # type: ignore
from string import ascii_lowercase, ascii_uppercase # type: ignore
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode("utf-8").split()
def S(): return sys.stdin.buffer.readline().rstrip().decode("utf-8")
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def SRL(n): return [list(S()) for _ in range(n)]
N,M = LI()
G = [[] for _ in range(N)]
for _ in range(M):
u,v = LI()
u-=1
v-=1
G[u].append(v)
INF = 10**9
def bfs(s):
dist = [INF]*N
dist[s] = 0
q = deque([s])
while q:
v = q.popleft()
cur = dist[v]
for nex in G[v]:
if dist[nex]<INF: continue
dist[nex] = cur+1
q.append(nex)
return dist
d0 = bfs(0)
dn2 = bfs(N-2)
dn1 = bfs(N-1)
ans = min(
INF,
d0[N-2]+dn2[N-1]+dn1[0],
d0[N-1]+dn1[N-2]+dn2[0]
)
print(ans if ans<INF else -1)