結果

問題 No.2565 はじめてのおつかい
ユーザー ymskskyymsksky
提出日時 2024-04-09 23:43:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 705 ms / 2,000 ms
コード長 772 bytes
コンパイル時間 505 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 96,420 KB
最終ジャッジ日時 2024-04-09 23:43:28
合計ジャッジ時間 23,460 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,868 KB
testcase_01 AC 36 ms
53,512 KB
testcase_02 AC 41 ms
53,920 KB
testcase_03 AC 288 ms
96,420 KB
testcase_04 AC 154 ms
96,296 KB
testcase_05 AC 36 ms
53,220 KB
testcase_06 AC 648 ms
89,536 KB
testcase_07 AC 376 ms
82,892 KB
testcase_08 AC 370 ms
82,588 KB
testcase_09 AC 579 ms
88,520 KB
testcase_10 AC 419 ms
84,388 KB
testcase_11 AC 561 ms
90,624 KB
testcase_12 AC 313 ms
81,592 KB
testcase_13 AC 429 ms
83,992 KB
testcase_14 AC 530 ms
88,224 KB
testcase_15 AC 659 ms
89,816 KB
testcase_16 AC 133 ms
92,308 KB
testcase_17 AC 75 ms
77,144 KB
testcase_18 AC 79 ms
81,980 KB
testcase_19 AC 554 ms
89,556 KB
testcase_20 AC 510 ms
88,436 KB
testcase_21 AC 324 ms
86,208 KB
testcase_22 AC 236 ms
83,032 KB
testcase_23 AC 413 ms
84,416 KB
testcase_24 AC 160 ms
78,452 KB
testcase_25 AC 581 ms
89,840 KB
testcase_26 AC 389 ms
84,308 KB
testcase_27 AC 400 ms
86,184 KB
testcase_28 AC 552 ms
89,628 KB
testcase_29 AC 457 ms
89,888 KB
testcase_30 AC 391 ms
88,576 KB
testcase_31 AC 458 ms
88,560 KB
testcase_32 AC 327 ms
83,500 KB
testcase_33 AC 457 ms
87,704 KB
testcase_34 AC 705 ms
91,144 KB
testcase_35 AC 273 ms
89,656 KB
testcase_36 AC 516 ms
87,964 KB
testcase_37 AC 343 ms
84,560 KB
testcase_38 AC 488 ms
88,204 KB
testcase_39 AC 580 ms
88,416 KB
testcase_40 AC 371 ms
90,436 KB
testcase_41 AC 429 ms
91,312 KB
testcase_42 AC 469 ms
85,136 KB
testcase_43 AC 387 ms
85,368 KB
testcase_44 AC 411 ms
82,996 KB
testcase_45 AC 268 ms
80,508 KB
testcase_46 AC 659 ms
90,172 KB
testcase_47 AC 469 ms
87,444 KB
testcase_48 AC 421 ms
83,900 KB
testcase_49 AC 290 ms
81,208 KB
testcase_50 AC 470 ms
89,792 KB
testcase_51 AC 36 ms
53,760 KB
testcase_52 AC 445 ms
91,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    edges[u].append(v)

INF = float("inf")
def dijkstra(s, g):
    dist = [INF] * n
    que = [(0, s)]
    heapq.heapify(que)
    while que:
        cost, crt = heapq.heappop(que)
        if dist[crt] <= cost:
            continue
        dist[crt] = cost
        for nxt in edges[crt]:
            if dist[nxt] <= cost + 1:
                continue
            heapq.heappush(que, (cost + 1, nxt))
    return dist[g]

ans = INF
for l in ((0, n - 1, n - 2, 0), (0, n - 2, n - 1, 0)):
    tmp = 0
    for a, b in zip(l, l[1:]):
        tmp += dijkstra(a, b)
    ans = min(ans, tmp)
print(-1 if ans == INF else ans)
0