結果

問題 No.2565 はじめてのおつかい
コンテスト
ユーザー Kemty
提出日時 2023-12-02 15:45:46
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 1,159 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 122 ms
コンパイル使用メモリ 85,504 KB
実行使用メモリ 102,272 KB
最終ジャッジ日時 2026-04-13 19:49:43
合計ジャッジ時間 8,545 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import defaultdict, deque, Counter
import copy
from itertools import combinations, permutations, product, accumulate, groupby, chain
from heapq import heapify, heappop, heappush
import math
import bisect
from pprint import pprint
from random import randint
import sys
# sys.setrecursionlimit(700000)
input = lambda: sys.stdin.readline().rstrip('\n')
inf = float('inf')
mod1 = 10**9+7
mod2 = 998244353
def ceil_div(x, y): return -(-x//y)

################################################

def bfs(s, t):
    dist = [-1]*N
    dq = deque([s])
    dist[s] = 0
    while dq:
        now = dq.pop()
        for nxt in adj[now]:
            if dist[nxt] == -1:
                dist[nxt] = dist[now]+1
                if nxt == t:
                    return dist[t]
                dq.appendleft(nxt)
    return inf

N, M = map(int, input().split())
adj = [[] for _ in range(N)]
for _ in range(M):
    u, v = map(int, input().split())
    u -= 1; v -= 1
    adj[u].append(v)
d1 = 0
d1 += bfs(0, N-2)
d1 += bfs(N-2, N-1)
d1 += bfs(N-1, 0)
d2 = 0
d2 += bfs(0, N-1)
d2 += bfs(N-1, N-2)
d2 += bfs(N-2, 0)
print(min(d1, d2) if min(d1, d2) != inf else -1)
0