結果

問題 No.2565 はじめてのおつかい
ユーザー e60e256e60e256
提出日時 2023-12-02 16:04:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 438 ms / 2,000 ms
コード長 1,435 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 81,972 KB
実行使用メモリ 99,784 KB
最終ジャッジ日時 2024-09-26 19:46:49
合計ジャッジ時間 16,591 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,608 KB
testcase_01 AC 40 ms
52,992 KB
testcase_02 AC 37 ms
52,352 KB
testcase_03 AC 196 ms
99,784 KB
testcase_04 AC 151 ms
99,328 KB
testcase_05 AC 36 ms
52,864 KB
testcase_06 AC 324 ms
84,856 KB
testcase_07 AC 223 ms
80,688 KB
testcase_08 AC 255 ms
82,364 KB
testcase_09 AC 286 ms
83,776 KB
testcase_10 AC 286 ms
82,272 KB
testcase_11 AC 429 ms
91,152 KB
testcase_12 AC 166 ms
79,360 KB
testcase_13 AC 292 ms
81,476 KB
testcase_14 AC 415 ms
86,852 KB
testcase_15 AC 432 ms
87,144 KB
testcase_16 AC 153 ms
93,696 KB
testcase_17 AC 84 ms
77,484 KB
testcase_18 AC 90 ms
81,908 KB
testcase_19 AC 433 ms
89,460 KB
testcase_20 AC 405 ms
88,368 KB
testcase_21 AC 333 ms
87,276 KB
testcase_22 AC 269 ms
85,140 KB
testcase_23 AC 321 ms
84,632 KB
testcase_24 AC 163 ms
78,712 KB
testcase_25 AC 408 ms
88,408 KB
testcase_26 AC 291 ms
83,960 KB
testcase_27 AC 303 ms
88,936 KB
testcase_28 AC 423 ms
89,804 KB
testcase_29 AC 400 ms
91,236 KB
testcase_30 AC 313 ms
87,972 KB
testcase_31 AC 380 ms
88,540 KB
testcase_32 AC 299 ms
83,884 KB
testcase_33 AC 356 ms
89,160 KB
testcase_34 AC 438 ms
88,608 KB
testcase_35 AC 266 ms
88,948 KB
testcase_36 AC 414 ms
89,648 KB
testcase_37 AC 268 ms
83,796 KB
testcase_38 AC 368 ms
87,992 KB
testcase_39 AC 360 ms
86,580 KB
testcase_40 AC 330 ms
89,752 KB
testcase_41 AC 357 ms
91,344 KB
testcase_42 AC 268 ms
84,024 KB
testcase_43 AC 357 ms
86,708 KB
testcase_44 AC 266 ms
82,380 KB
testcase_45 AC 201 ms
79,940 KB
testcase_46 AC 401 ms
86,996 KB
testcase_47 AC 229 ms
81,648 KB
testcase_48 AC 281 ms
82,816 KB
testcase_49 AC 160 ms
79,104 KB
testcase_50 AC 237 ms
82,452 KB
testcase_51 AC 37 ms
52,608 KB
testcase_52 AC 138 ms
80,128 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