結果

問題 No.2565 はじめてのおつかい
ユーザー e60e256e60e256
提出日時 2023-12-02 16:04:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 599 ms / 2,000 ms
コード長 1,435 bytes
コンパイル時間 160 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 98,948 KB
最終ジャッジ日時 2023-12-02 16:04:20
合計ジャッジ時間 20,167 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,460 KB
testcase_01 AC 40 ms
53,460 KB
testcase_02 AC 40 ms
53,460 KB
testcase_03 AC 291 ms
98,948 KB
testcase_04 AC 184 ms
98,948 KB
testcase_05 AC 40 ms
53,460 KB
testcase_06 AC 417 ms
84,240 KB
testcase_07 AC 269 ms
80,268 KB
testcase_08 AC 323 ms
81,580 KB
testcase_09 AC 406 ms
83,112 KB
testcase_10 AC 363 ms
82,240 KB
testcase_11 AC 599 ms
90,632 KB
testcase_12 AC 188 ms
78,584 KB
testcase_13 AC 324 ms
81,184 KB
testcase_14 AC 460 ms
86,312 KB
testcase_15 AC 517 ms
86,400 KB
testcase_16 AC 167 ms
93,380 KB
testcase_17 AC 88 ms
77,048 KB
testcase_18 AC 98 ms
81,832 KB
testcase_19 AC 484 ms
89,248 KB
testcase_20 AC 480 ms
87,684 KB
testcase_21 AC 368 ms
86,988 KB
testcase_22 AC 294 ms
84,560 KB
testcase_23 AC 395 ms
84,220 KB
testcase_24 AC 181 ms
78,324 KB
testcase_25 AC 511 ms
88,004 KB
testcase_26 AC 368 ms
83,568 KB
testcase_27 AC 359 ms
88,264 KB
testcase_28 AC 526 ms
88,772 KB
testcase_29 AC 515 ms
90,932 KB
testcase_30 AC 369 ms
87,916 KB
testcase_31 AC 461 ms
87,740 KB
testcase_32 AC 363 ms
83,376 KB
testcase_33 AC 435 ms
88,200 KB
testcase_34 AC 551 ms
88,328 KB
testcase_35 AC 336 ms
88,780 KB
testcase_36 AC 525 ms
89,280 KB
testcase_37 AC 329 ms
83,664 KB
testcase_38 AC 475 ms
87,192 KB
testcase_39 AC 459 ms
85,796 KB
testcase_40 AC 409 ms
89,320 KB
testcase_41 AC 452 ms
90,660 KB
testcase_42 AC 336 ms
83,228 KB
testcase_43 AC 438 ms
86,176 KB
testcase_44 AC 314 ms
81,976 KB
testcase_45 AC 236 ms
79,512 KB
testcase_46 AC 498 ms
86,828 KB
testcase_47 AC 292 ms
81,356 KB
testcase_48 AC 348 ms
82,396 KB
testcase_49 AC 184 ms
78,200 KB
testcase_50 AC 285 ms
81,768 KB
testcase_51 AC 39 ms
53,460 KB
testcase_52 AC 161 ms
79,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def dijkstra2(adjList, s, g):
    # Initialize the distance from the start node s to all other nodes as infinity
    distance = [float('inf')] * len(adjList)
    distance[s] = 0

    # Initialize the priority queue with the start node
    queue = [(0, s)]  # (distance, node)

    while queue:
        # Get the node with the smallest distance
        dist, node = heapq.heappop(queue)

        # If this node has already been processed, then ignore it
        if dist > distance[node]:
            continue

        # Update the distances to the neighboring nodes
        for neighbor, cost in adjList[node]:
            new_dist = dist + cost
            if new_dist < distance[neighbor]:
                distance[neighbor] = new_dist
                heapq.heappush(queue, (new_dist, neighbor))

    # If the distance to the goal node g is infinity, it means there is no path.
    if distance[g] == float('inf'):
        return 1e10
    else:
        return distance[g]


N, M = map(int, input().split())

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

ans1 = dijkstra2(adj_list, 0, N-2) + dijkstra2(adj_list, N-2, N-1) + dijkstra2(adj_list, N-1, 0)
ans2 = dijkstra2(adj_list, 0, N-1) + dijkstra2(adj_list, N-1, N-2) + dijkstra2(adj_list, N-2, 0)

ans = min(ans1, ans2)
if (ans >= 1e10):
    print(-1)
else:
    print(ans)
0