結果

問題 No.2565 はじめてのおつかい
ユーザー RYOH2718RYOH2718
提出日時 2023-12-02 16:09:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 492 ms / 2,000 ms
コード長 1,227 bytes
コンパイル時間 617 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 94,212 KB
最終ジャッジ日時 2023-12-02 16:09:57
合計ジャッジ時間 17,873 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,460 KB
testcase_01 AC 39 ms
53,460 KB
testcase_02 AC 40 ms
53,460 KB
testcase_03 AC 254 ms
94,212 KB
testcase_04 AC 174 ms
94,212 KB
testcase_05 AC 40 ms
53,460 KB
testcase_06 AC 372 ms
83,760 KB
testcase_07 AC 263 ms
80,664 KB
testcase_08 AC 329 ms
81,920 KB
testcase_09 AC 330 ms
82,744 KB
testcase_10 AC 340 ms
82,476 KB
testcase_11 AC 492 ms
90,536 KB
testcase_12 AC 194 ms
78,584 KB
testcase_13 AC 332 ms
82,260 KB
testcase_14 AC 434 ms
86,664 KB
testcase_15 AC 433 ms
86,272 KB
testcase_16 AC 164 ms
89,028 KB
testcase_17 AC 88 ms
77,048 KB
testcase_18 AC 96 ms
80,040 KB
testcase_19 AC 420 ms
88,220 KB
testcase_20 AC 383 ms
86,796 KB
testcase_21 AC 287 ms
85,976 KB
testcase_22 AC 216 ms
82,256 KB
testcase_23 AC 303 ms
83,308 KB
testcase_24 AC 135 ms
77,816 KB
testcase_25 AC 464 ms
88,084 KB
testcase_26 AC 292 ms
82,784 KB
testcase_27 AC 269 ms
86,048 KB
testcase_28 AC 399 ms
87,696 KB
testcase_29 AC 391 ms
89,608 KB
testcase_30 AC 271 ms
86,868 KB
testcase_31 AC 359 ms
87,172 KB
testcase_32 AC 282 ms
82,648 KB
testcase_33 AC 387 ms
87,832 KB
testcase_34 AC 476 ms
87,448 KB
testcase_35 AC 265 ms
88,376 KB
testcase_36 AC 463 ms
89,276 KB
testcase_37 AC 282 ms
83,124 KB
testcase_38 AC 381 ms
86,424 KB
testcase_39 AC 404 ms
85,664 KB
testcase_40 AC 300 ms
88,304 KB
testcase_41 AC 402 ms
90,204 KB
testcase_42 AC 298 ms
82,704 KB
testcase_43 AC 375 ms
85,780 KB
testcase_44 AC 325 ms
81,800 KB
testcase_45 AC 231 ms
79,992 KB
testcase_46 AC 430 ms
86,324 KB
testcase_47 AC 286 ms
81,752 KB
testcase_48 AC 329 ms
81,896 KB
testcase_49 AC 189 ms
78,584 KB
testcase_50 AC 299 ms
82,624 KB
testcase_51 AC 40 ms
53,460 KB
testcase_52 AC 149 ms
79,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def INT():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(map(int, input().split()))


import heapq


def dijkstra(n, G, start=0):
    INF = 10**18
    dist = [INF] * n
    dist[start] = 0
    pq = []
    heapq.heappush(pq, (0, start))
    visited = [False] * n

    while pq:
        now = heapq.heappop(pq)[1]
        visited[now] = True
        for nxt, cost in G[now]:
            if dist[nxt] > dist[now] + cost and not visited[nxt]:
                dist[nxt] = dist[now] + cost
                heapq.heappush(pq, (dist[now] + cost, nxt))

    return dist


N, M = MI()
G = [[] for _ in range(N)]
for _ in range(M):
    u, v = MI()
    u -= 1
    v -= 1
    G[u].append((v, 1))

dist1 = dijkstra(N, G, 0)  # from 0
dist2 = dijkstra(N, G, N - 2)  # from N - 2
dist3 = dijkstra(N, G, N - 1)  # from N - 1
INF = 10**18
if (dist1[N - 2] == INF or dist2[N - 1] == INF or dist3[0] == INF) or (
    dist1[N - 1] == INF or dist3[N - 2] == INF or dist2[0] == INF
):
    print(-1)
    exit()

cand1 = dist1[N - 2] + dist2[N - 1] + dist3[0]
cand2 = dist1[N - 1] + dist3[N - 2] + dist2[0]
ans = min(cand1, cand2)
print(ans)
0