結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー H3PO4H3PO4
提出日時 2020-12-17 23:34:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 962 ms / 2,000 ms
コード長 980 bytes
コンパイル時間 677 ms
コンパイル使用メモリ 81,592 KB
実行使用メモリ 83,048 KB
最終ジャッジ日時 2023-10-21 07:39:11
合計ジャッジ時間 17,765 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,412 KB
testcase_01 AC 40 ms
53,412 KB
testcase_02 AC 38 ms
53,412 KB
testcase_03 AC 38 ms
53,412 KB
testcase_04 AC 38 ms
53,412 KB
testcase_05 AC 39 ms
53,412 KB
testcase_06 AC 38 ms
53,412 KB
testcase_07 AC 82 ms
75,412 KB
testcase_08 AC 93 ms
76,020 KB
testcase_09 AC 872 ms
81,380 KB
testcase_10 AC 88 ms
76,552 KB
testcase_11 AC 750 ms
80,228 KB
testcase_12 AC 230 ms
78,188 KB
testcase_13 AC 401 ms
78,788 KB
testcase_14 AC 49 ms
65,568 KB
testcase_15 AC 97 ms
76,936 KB
testcase_16 AC 175 ms
76,548 KB
testcase_17 AC 58 ms
71,928 KB
testcase_18 AC 53 ms
71,788 KB
testcase_19 AC 486 ms
78,600 KB
testcase_20 AC 41 ms
57,512 KB
testcase_21 AC 962 ms
80,832 KB
testcase_22 AC 37 ms
53,416 KB
testcase_23 AC 39 ms
53,416 KB
testcase_24 AC 35 ms
53,416 KB
testcase_25 AC 34 ms
53,416 KB
testcase_26 AC 34 ms
53,416 KB
testcase_27 AC 36 ms
53,416 KB
testcase_28 AC 47 ms
67,616 KB
testcase_29 AC 161 ms
77,660 KB
testcase_30 AC 44 ms
62,044 KB
testcase_31 AC 82 ms
75,644 KB
testcase_32 AC 36 ms
53,416 KB
testcase_33 AC 348 ms
77,624 KB
testcase_34 AC 204 ms
77,336 KB
testcase_35 AC 136 ms
76,188 KB
testcase_36 AC 133 ms
76,324 KB
testcase_37 AC 101 ms
75,876 KB
testcase_38 AC 146 ms
76,252 KB
testcase_39 AC 154 ms
77,000 KB
testcase_40 AC 40 ms
53,416 KB
testcase_41 AC 39 ms
53,416 KB
testcase_42 AC 37 ms
53,416 KB
testcase_43 AC 142 ms
77,756 KB
testcase_44 AC 116 ms
76,984 KB
testcase_45 AC 879 ms
80,760 KB
testcase_46 AC 79 ms
75,740 KB
testcase_47 AC 201 ms
77,572 KB
testcase_48 AC 676 ms
80,564 KB
testcase_49 AC 96 ms
75,980 KB
testcase_50 AC 36 ms
53,416 KB
testcase_51 AC 35 ms
53,416 KB
testcase_52 AC 247 ms
77,200 KB
testcase_53 AC 162 ms
76,856 KB
testcase_54 AC 733 ms
82,000 KB
testcase_55 AC 762 ms
81,828 KB
testcase_56 AC 769 ms
81,984 KB
testcase_57 AC 941 ms
82,904 KB
testcase_58 AC 951 ms
83,048 KB
testcase_59 AC 923 ms
82,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

T = int(input())
N, M = map(int, input().split())
G = [dict() for _ in range(N)]
MAX = 10 ** 13

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


def add_edge(u, v, w):
    G[u][v] = w
    if not T:
        # 無向グラフ
        G[v][u] = w


def dijkstra(source, target):
    dist = [MAX] * N
    d = [(0, source)]
    dist[source] = 0
    while d:
        c, v = heapq.heappop(d)
        if dist[v] < c:
            continue
        for t, cost in G[v].items():
            if dist[v] + cost < dist[t]:
                dist[t] = dist[v] + cost
                heapq.heappush(d, (dist[t], t))
    return dist[target]


for u, v, w in edges:
    add_edge(u, v, w)

ans = MAX
for u, v, w in edges:
    del G[u][v]
    if not T:
        # 無向グラフ
        del G[v][u]

    length = dijkstra(v, u) + w
    ans = min(length, ans)

    add_edge(u, v, w)
print(ans if ans != MAX else -1)
0