結果

問題 No.2565 はじめてのおつかい
ユーザー kinoko_econkinoko_econ
提出日時 2023-12-02 16:15:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 563 ms / 2,000 ms
コード長 1,210 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 99,088 KB
最終ジャッジ日時 2023-12-02 16:15:34
合計ジャッジ時間 17,942 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 216 ms
99,088 KB
testcase_04 AC 193 ms
99,084 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 412 ms
90,380 KB
testcase_07 AC 248 ms
83,180 KB
testcase_08 AC 311 ms
84,092 KB
testcase_09 AC 355 ms
88,096 KB
testcase_10 AC 346 ms
85,760 KB
testcase_11 AC 563 ms
98,368 KB
testcase_12 AC 182 ms
80,508 KB
testcase_13 AC 317 ms
85,160 KB
testcase_14 AC 457 ms
91,808 KB
testcase_15 AC 529 ms
92,516 KB
testcase_16 AC 139 ms
92,348 KB
testcase_17 AC 74 ms
77,560 KB
testcase_18 AC 81 ms
80,416 KB
testcase_19 AC 431 ms
95,644 KB
testcase_20 AC 425 ms
92,476 KB
testcase_21 AC 278 ms
90,984 KB
testcase_22 AC 188 ms
84,904 KB
testcase_23 AC 288 ms
86,520 KB
testcase_24 AC 115 ms
78,200 KB
testcase_25 AC 500 ms
93,808 KB
testcase_26 AC 274 ms
86,088 KB
testcase_27 AC 241 ms
90,640 KB
testcase_28 AC 423 ms
94,636 KB
testcase_29 AC 441 ms
96,684 KB
testcase_30 AC 255 ms
91,596 KB
testcase_31 AC 366 ms
92,788 KB
testcase_32 AC 262 ms
85,544 KB
testcase_33 AC 358 ms
92,628 KB
testcase_34 AC 553 ms
94,632 KB
testcase_35 AC 244 ms
93,424 KB
testcase_36 AC 486 ms
93,892 KB
testcase_37 AC 287 ms
86,520 KB
testcase_38 AC 413 ms
92,848 KB
testcase_39 AC 474 ms
91,168 KB
testcase_40 AC 318 ms
94,588 KB
testcase_41 AC 397 ms
96,248 KB
testcase_42 AC 304 ms
86,208 KB
testcase_43 AC 424 ms
91,472 KB
testcase_44 AC 351 ms
85,028 KB
testcase_45 AC 195 ms
80,760 KB
testcase_46 AC 489 ms
94,188 KB
testcase_47 AC 263 ms
86,008 KB
testcase_48 AC 363 ms
85,844 KB
testcase_49 AC 173 ms
80,508 KB
testcase_50 AC 317 ms
87,160 KB
testcase_51 AC 39 ms
53,460 KB
testcase_52 AC 163 ms
84,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int,input().split())
G = [[] for _ in range(N)]
for i in range(M):
    u, v = map(int,input().split())
    G[u-1].append([v-1, 1])

## ダイクストラ法の実装
## 「ある頂点から全ての頂点への最短距離」をO(E*logV)で出力できる。
## 辺の長さはバラバラで、非負の値。

import heapq

def dijkstra(V, G, r):
    done = [False]*V
    dist = [10**10]*V
    dist[r] = 0
    node_heap = []
    heapq.heappush(node_heap, [dist[r], r])

    while node_heap:
        tmp = heapq.heappop(node_heap)
        cur_node = tmp[1]
        if not done[cur_node]:
            for e in G[cur_node]:
                if dist[e[0]] > dist[cur_node] + e[1]:
                    dist[e[0]] = dist[cur_node] + e[1]
                    heapq.heappush(node_heap, [dist[e[0]], e[0]])
        done[cur_node] = True
    return dist

ans = 10**15
d1 = dijkstra(N, G, 0)
d2 = dijkstra(N, G, N-2)
d3 = dijkstra(N, G, N-1)
if d1[N-2] < 10**10 and d2[N-1] < 10**10 and d3[0] < 10**10:
    ans = min(ans, d1[N-2] + d2[N-1] + d3[0])
if d1[N-1] < 10**10 and d3[N-2] < 10**10 and d2[0] < 10**10:
    ans = min(ans, d1[N-1] + d3[N-2] + d2[0])
if ans < 10**15:
    print(ans)
else:
    print(-1)
0