結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー brthyyjpbrthyyjp
提出日時 2021-12-21 21:05:55
言語 PyPy3
(7.3.15)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,305 bytes
コンパイル時間 815 ms
コンパイル使用メモリ 86,916 KB
実行使用メモリ 87,052 KB
最終ジャッジ日時 2023-10-13 19:53:13
合計ジャッジ時間 23,456 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,336 KB
testcase_01 AC 74 ms
71,448 KB
testcase_02 AC 76 ms
71,372 KB
testcase_03 AC 76 ms
71,256 KB
testcase_04 AC 76 ms
71,200 KB
testcase_05 AC 76 ms
71,208 KB
testcase_06 AC 78 ms
71,340 KB
testcase_07 AC 104 ms
77,640 KB
testcase_08 AC 127 ms
78,024 KB
testcase_09 AC 1,104 ms
83,708 KB
testcase_10 AC 114 ms
78,100 KB
testcase_11 AC 890 ms
83,448 KB
testcase_12 AC 256 ms
79,980 KB
testcase_13 AC 493 ms
81,912 KB
testcase_14 AC 85 ms
75,756 KB
testcase_15 AC 125 ms
78,836 KB
testcase_16 AC 229 ms
78,056 KB
testcase_17 AC 90 ms
76,088 KB
testcase_18 AC 90 ms
75,892 KB
testcase_19 AC 583 ms
81,616 KB
testcase_20 AC 82 ms
71,460 KB
testcase_21 AC 1,113 ms
84,468 KB
testcase_22 AC 79 ms
71,296 KB
testcase_23 AC 78 ms
71,228 KB
testcase_24 AC 78 ms
71,260 KB
testcase_25 AC 79 ms
71,340 KB
testcase_26 AC 76 ms
71,240 KB
testcase_27 AC 76 ms
71,340 KB
testcase_28 AC 88 ms
75,360 KB
testcase_29 AC 213 ms
79,964 KB
testcase_30 AC 87 ms
76,296 KB
testcase_31 AC 123 ms
77,524 KB
testcase_32 AC 80 ms
71,300 KB
testcase_33 AC 405 ms
79,928 KB
testcase_34 AC 247 ms
78,692 KB
testcase_35 AC 174 ms
78,048 KB
testcase_36 AC 168 ms
78,332 KB
testcase_37 AC 137 ms
77,604 KB
testcase_38 AC 185 ms
78,244 KB
testcase_39 AC 195 ms
78,292 KB
testcase_40 AC 79 ms
71,392 KB
testcase_41 AC 79 ms
71,456 KB
testcase_42 AC 78 ms
71,440 KB
testcase_43 AC 164 ms
79,708 KB
testcase_44 AC 141 ms
78,576 KB
testcase_45 AC 1,077 ms
84,332 KB
testcase_46 AC 115 ms
77,776 KB
testcase_47 AC 238 ms
79,908 KB
testcase_48 AC 768 ms
84,008 KB
testcase_49 RE -
testcase_50 AC 79 ms
71,584 KB
testcase_51 AC 78 ms
71,524 KB
testcase_52 AC 279 ms
79,488 KB
testcase_53 AC 182 ms
78,344 KB
testcase_54 AC 836 ms
85,196 KB
testcase_55 AC 831 ms
85,024 KB
testcase_56 AC 845 ms
85,328 KB
testcase_57 AC 1,094 ms
86,596 KB
testcase_58 AC 1,108 ms
87,052 KB
testcase_59 AC 1,091 ms
86,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
INF = 10**18
def dijkstra(s, edge):
    n = len(edge)
    dist = [INF]*n
    dist[s] = 0
    edgelist = []
    heapq.heappush(edgelist,(dist[s], s))
    while edgelist:
        minedge = heapq.heappop(edgelist)
        if dist[minedge[1]] < minedge[0]:
            continue
        v = minedge[1]
        for e in edge[v]:
            if dist[e[1]] > dist[v]+e[0]:
                dist[e[1]] = dist[v]+e[0]
                heapq.heappush(edgelist,(dist[e[1]], e[1]))
    return dist

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

t = int(input())
n, m = map(int, input().split())
E = []
edge = [set() for i in range(n)]
for i in range(m):
    u, v, w = map(int, input().split())
    u, v = u-1, v-1
    E.append((u, v, w))
    if t == 0:
        edge[u].add((w, v))
        edge[v].add((w, u))
    else:
        edge[u].add((w, v))

ans = INF
if t == 0:
    for u, v, w in E:
        edge[u].remove((w, v))
        edge[v].remove((w, u))
        D = dijkstra(v, edge)
        ans = min(ans, w+D[u])
        edge[u].add((w, v))
        edge[v].add((w, u))
else:
    for u, v, w in E:
        edge[u].remove((w, v))
        D = dijkstra(v, edge)
        ans = min(ans, w+D[u])
        edge[u].add((w, v))
if ans < INF:
    print(ans)
else:
    print(-1)
0