結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-30 23:28:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 830 ms / 2,000 ms
コード長 952 bytes
コンパイル時間 328 ms
コンパイル使用メモリ 81,976 KB
実行使用メモリ 82,616 KB
最終ジャッジ日時 2024-04-17 00:11:48
合計ジャッジ時間 14,357 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,876 KB
testcase_01 AC 39 ms
52,884 KB
testcase_02 AC 39 ms
52,860 KB
testcase_03 AC 39 ms
53,056 KB
testcase_04 AC 40 ms
53,760 KB
testcase_05 AC 40 ms
54,276 KB
testcase_06 AC 40 ms
52,992 KB
testcase_07 AC 62 ms
76,084 KB
testcase_08 AC 92 ms
76,492 KB
testcase_09 AC 617 ms
79,224 KB
testcase_10 AC 74 ms
76,532 KB
testcase_11 AC 543 ms
79,464 KB
testcase_12 AC 182 ms
78,200 KB
testcase_13 AC 340 ms
79,004 KB
testcase_14 AC 45 ms
63,220 KB
testcase_15 AC 89 ms
77,064 KB
testcase_16 AC 158 ms
77,008 KB
testcase_17 AC 56 ms
71,144 KB
testcase_18 AC 51 ms
69,704 KB
testcase_19 AC 376 ms
78,892 KB
testcase_20 AC 46 ms
62,316 KB
testcase_21 AC 691 ms
79,968 KB
testcase_22 AC 39 ms
53,852 KB
testcase_23 AC 39 ms
53,852 KB
testcase_24 AC 38 ms
53,480 KB
testcase_25 AC 38 ms
53,240 KB
testcase_26 AC 39 ms
53,056 KB
testcase_27 AC 38 ms
52,956 KB
testcase_28 AC 52 ms
69,792 KB
testcase_29 AC 128 ms
77,464 KB
testcase_30 AC 48 ms
63,808 KB
testcase_31 AC 75 ms
75,968 KB
testcase_32 AC 41 ms
54,672 KB
testcase_33 AC 286 ms
77,116 KB
testcase_34 AC 176 ms
76,820 KB
testcase_35 AC 130 ms
77,340 KB
testcase_36 AC 111 ms
76,476 KB
testcase_37 AC 89 ms
76,280 KB
testcase_38 AC 130 ms
77,080 KB
testcase_39 AC 121 ms
77,200 KB
testcase_40 AC 39 ms
53,348 KB
testcase_41 AC 37 ms
52,952 KB
testcase_42 AC 37 ms
52,940 KB
testcase_43 AC 114 ms
77,844 KB
testcase_44 AC 101 ms
76,916 KB
testcase_45 AC 830 ms
80,644 KB
testcase_46 AC 74 ms
75,932 KB
testcase_47 AC 166 ms
77,340 KB
testcase_48 AC 482 ms
79,320 KB
testcase_49 AC 82 ms
76,124 KB
testcase_50 AC 40 ms
54,136 KB
testcase_51 AC 39 ms
53,408 KB
testcase_52 AC 215 ms
76,960 KB
testcase_53 AC 168 ms
77,260 KB
testcase_54 AC 730 ms
81,784 KB
testcase_55 AC 740 ms
81,604 KB
testcase_56 AC 765 ms
82,128 KB
testcase_57 AC 770 ms
81,864 KB
testcase_58 AC 821 ms
82,616 KB
testcase_59 AC 806 ms
82,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
inf = 10**18

T = int(input())
N, M = map(int, input().split())
edge = []
G = [[] for _ in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edge.append((a, b, c))
    G[a].append((b, c))
    if T == 0:
        G[b].append((a, c))


def dijkstra(i):
    s, t, c = edge[i]
    dist = [inf] * N
    dist[t] = 0
    que = [(0, t)]
    while que:
        dv, v = heapq.heappop(que)
        if dist[v] < dv:
            continue
        if v == s:
            break
        for u, du in G[v]:
            if (v, u) in ((s, t), (t, s)):
                continue
            if dist[u] > dv + du:
                dist[u] = dv + du
                heapq.heappush(que, (dv + du, u))
    return c + dist[s]


ans = inf
for i in range(M):
    d = dijkstra(i)
    if d < ans:
        ans = d
if ans >= inf:
    ans = -1
print(ans)
0