結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー qibqib
提出日時 2023-04-09 00:08:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,059 ms / 2,000 ms
コード長 896 bytes
コンパイル時間 356 ms
コンパイル使用メモリ 82,820 KB
実行使用メモリ 83,024 KB
最終ジャッジ日時 2024-04-14 19:38:49
合計ジャッジ時間 15,751 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,748 KB
testcase_01 AC 38 ms
53,316 KB
testcase_02 AC 37 ms
52,816 KB
testcase_03 AC 38 ms
54,420 KB
testcase_04 AC 38 ms
52,948 KB
testcase_05 AC 38 ms
53,620 KB
testcase_06 AC 39 ms
53,892 KB
testcase_07 AC 82 ms
76,676 KB
testcase_08 AC 105 ms
76,704 KB
testcase_09 AC 728 ms
80,136 KB
testcase_10 AC 100 ms
77,432 KB
testcase_11 AC 677 ms
79,440 KB
testcase_12 AC 229 ms
79,044 KB
testcase_13 AC 397 ms
78,676 KB
testcase_14 AC 53 ms
66,956 KB
testcase_15 AC 107 ms
77,484 KB
testcase_16 AC 169 ms
77,384 KB
testcase_17 AC 74 ms
76,432 KB
testcase_18 AC 70 ms
74,932 KB
testcase_19 AC 440 ms
78,768 KB
testcase_20 AC 51 ms
63,544 KB
testcase_21 AC 708 ms
79,924 KB
testcase_22 AC 39 ms
52,808 KB
testcase_23 AC 38 ms
53,448 KB
testcase_24 AC 37 ms
52,992 KB
testcase_25 AC 37 ms
53,776 KB
testcase_26 AC 38 ms
52,740 KB
testcase_27 AC 38 ms
53,040 KB
testcase_28 AC 58 ms
70,960 KB
testcase_29 AC 175 ms
77,624 KB
testcase_30 AC 50 ms
62,900 KB
testcase_31 AC 86 ms
76,376 KB
testcase_32 AC 40 ms
54,056 KB
testcase_33 AC 361 ms
77,576 KB
testcase_34 AC 214 ms
77,916 KB
testcase_35 AC 140 ms
76,920 KB
testcase_36 AC 120 ms
77,332 KB
testcase_37 AC 95 ms
76,568 KB
testcase_38 AC 139 ms
76,900 KB
testcase_39 AC 120 ms
76,912 KB
testcase_40 AC 39 ms
52,748 KB
testcase_41 AC 40 ms
54,612 KB
testcase_42 AC 38 ms
53,496 KB
testcase_43 AC 144 ms
77,968 KB
testcase_44 AC 118 ms
77,172 KB
testcase_45 AC 1,059 ms
79,932 KB
testcase_46 AC 85 ms
76,356 KB
testcase_47 AC 198 ms
77,900 KB
testcase_48 AC 483 ms
78,728 KB
testcase_49 AC 96 ms
76,568 KB
testcase_50 AC 39 ms
52,960 KB
testcase_51 AC 39 ms
54,320 KB
testcase_52 AC 198 ms
77,068 KB
testcase_53 AC 148 ms
76,924 KB
testcase_54 AC 785 ms
81,788 KB
testcase_55 AC 766 ms
81,768 KB
testcase_56 AC 773 ms
81,632 KB
testcase_57 AC 823 ms
82,796 KB
testcase_58 AC 848 ms
83,024 KB
testcase_59 AC 842 ms
82,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

INF = 1 << 60

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

g = [[] for _ in range(n)]
if t == 0:
  for i in range(m):
    u, v, w = edges[i]
    g[u].append((v, w, i))
    g[v].append((u, w, i))
else:
  for i in range(m):
    u, v, w = edges[i]
    g[u].append((v, w, i))

ans = INF
for i in range(m):
  dst, src, w = edges[i]
  dist = [INF for _ in range(n)]
  dist[src] = 0
  hp = [(0, src)]
  while len(hp) > 0:
    cd, cur = heapq.heappop(hp)
    if cur == dst:
      break
    if cd > dist[cur]:
      continue
    for nxt, cost, j in g[cur]:
      if j == i or dist[nxt] <= dist[cur] + cost:
        continue
      dist[nxt] = dist[cur] + cost
      heapq.heappush(hp, (dist[nxt], nxt))

  ans = min(ans, dist[dst] + w)

if ans < INF:
  print(ans)
else:
  print("-1")
0