結果

問題 No.2565 はじめてのおつかい
ユーザー torippytorippy
提出日時 2023-12-02 15:48:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 472 ms / 2,000 ms
コード長 2,440 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 97,380 KB
最終ジャッジ日時 2023-12-02 15:49:14
合計ジャッジ時間 16,354 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,608 KB
testcase_01 AC 41 ms
55,608 KB
testcase_02 AC 40 ms
55,608 KB
testcase_03 AC 210 ms
97,368 KB
testcase_04 AC 177 ms
97,380 KB
testcase_05 AC 44 ms
55,608 KB
testcase_06 AC 348 ms
84,988 KB
testcase_07 AC 268 ms
81,196 KB
testcase_08 AC 322 ms
83,248 KB
testcase_09 AC 305 ms
83,516 KB
testcase_10 AC 313 ms
83,812 KB
testcase_11 AC 472 ms
92,268 KB
testcase_12 AC 187 ms
79,500 KB
testcase_13 AC 308 ms
83,288 KB
testcase_14 AC 389 ms
87,992 KB
testcase_15 AC 414 ms
87,240 KB
testcase_16 AC 154 ms
91,432 KB
testcase_17 AC 89 ms
77,320 KB
testcase_18 AC 94 ms
81,084 KB
testcase_19 AC 382 ms
89,700 KB
testcase_20 AC 362 ms
88,412 KB
testcase_21 AC 266 ms
87,596 KB
testcase_22 AC 221 ms
84,040 KB
testcase_23 AC 277 ms
84,664 KB
testcase_24 AC 133 ms
78,344 KB
testcase_25 AC 448 ms
89,612 KB
testcase_26 AC 266 ms
84,284 KB
testcase_27 AC 253 ms
88,340 KB
testcase_28 AC 365 ms
89,572 KB
testcase_29 AC 381 ms
92,152 KB
testcase_30 AC 249 ms
88,792 KB
testcase_31 AC 353 ms
88,676 KB
testcase_32 AC 271 ms
83,932 KB
testcase_33 AC 387 ms
89,796 KB
testcase_34 AC 446 ms
88,588 KB
testcase_35 AC 239 ms
90,400 KB
testcase_36 AC 441 ms
90,400 KB
testcase_37 AC 253 ms
84,372 KB
testcase_38 AC 353 ms
87,544 KB
testcase_39 AC 370 ms
86,716 KB
testcase_40 AC 315 ms
90,048 KB
testcase_41 AC 341 ms
92,476 KB
testcase_42 AC 278 ms
83,912 KB
testcase_43 AC 328 ms
86,780 KB
testcase_44 AC 324 ms
82,880 KB
testcase_45 AC 222 ms
80,684 KB
testcase_46 AC 392 ms
87,572 KB
testcase_47 AC 256 ms
82,604 KB
testcase_48 AC 298 ms
83,272 KB
testcase_49 AC 211 ms
79,112 KB
testcase_50 AC 259 ms
82,460 KB
testcase_51 AC 41 ms
55,608 KB
testcase_52 AC 142 ms
79,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
from collections import deque

INF = 10**18
class Dijkstra():
    # 全ての辺の距離が1のとき
    def bfs(self, graph, start, goal=-2):
        N = len(graph)
        is_visited = [False]*N
        is_visited[start] = True
        dists = [INF]*N
        dists[start] = 0
        
        que = deque([(start, 0)])
        while que:
            p, d = que.popleft()
            for next in graph[p]:
                if not is_visited[next]:
                    que.append((next, d+1))
                    dists[next] = d+1
                    is_visited[next] = True
                    if goal == next:
                        return dists
        return dists

    def dijkstra(self, graph, start):
        N = len(graph)
        dists = [INF for _ in range(N)]
        dists[start] = 0
        is_done = [False for _ in range(N)]
        hp = []
        # 経路復元が必要な場合
        prev = [-1 for _ in range(N)]
        heapq.heappush(hp, (dists[start], start))

        while hp:
            min_dist, min_v = heapq.heappop(hp)
            if is_done[min_v]:
                continue
            is_done[min_v] = True
            # 隣接リスト形式でコストが無いなら、ここのcostを削除することに注意
            for to, d in graph[min_v]:
                # 隣接リスト形式でコストが無いなら、ここのコストを1に変更することに注意
                dist = dists[min_v] + d
                if  dist < dists[to]:
                    dists[to] = dist
                    prev[to] = min_v
                    heapq.heappush(hp, (dists[to], to))
        return dists, prev

    # 後ろから順になっている事に注意
    def get_path(self, prev, end):
        path = []
        p = end
        while prev[p] != -1:
            path.append(p)
            p = prev[p]
        path.append(p)
        return path
    

N, M = map(int, input().split())
graph = [[] for _ in range(N)]

for _ in range(M):
    ui, vi = map(int, input().split())
    ui -= 1
    vi -= 1

    graph[ui].append((vi, 1))

dijk = Dijkstra()
dist_from_0, _ = dijk.dijkstra(graph, 0)
dist_from_N1, _ = dijk.dijkstra(graph, N-2)
dist_from_N, _ = dijk.dijkstra(graph, N-1)

ans = INF
ans = min(ans, dist_from_0[N-2] + dist_from_N1[N-1 ]+ dist_from_N[0])
ans = min(ans, dist_from_0[N-1] + dist_from_N[N-2] + dist_from_N1[0])
if ans == INF:
    print(-1)
else:
    print(ans)
0