結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | 草苺奶昔 |
提出日時 | 2023-04-02 19:16:48 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 822 ms / 2,000 ms |
コード長 | 2,369 bytes |
コンパイル時間 | 185 ms |
コンパイル使用メモリ | 82,376 KB |
実行使用メモリ | 84,016 KB |
最終ジャッジ日時 | 2024-09-25 00:36:29 |
合計ジャッジ時間 | 14,846 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 58 ms
67,300 KB |
testcase_01 | AC | 58 ms
68,724 KB |
testcase_02 | AC | 58 ms
68,452 KB |
testcase_03 | AC | 60 ms
68,064 KB |
testcase_04 | AC | 60 ms
67,800 KB |
testcase_05 | AC | 60 ms
68,460 KB |
testcase_06 | AC | 60 ms
68,588 KB |
testcase_07 | AC | 90 ms
77,748 KB |
testcase_08 | AC | 113 ms
78,252 KB |
testcase_09 | AC | 659 ms
81,620 KB |
testcase_10 | AC | 106 ms
79,364 KB |
testcase_11 | AC | 545 ms
81,552 KB |
testcase_12 | AC | 198 ms
79,912 KB |
testcase_13 | AC | 336 ms
80,296 KB |
testcase_14 | AC | 67 ms
73,472 KB |
testcase_15 | AC | 117 ms
79,240 KB |
testcase_16 | AC | 184 ms
79,056 KB |
testcase_17 | AC | 86 ms
77,796 KB |
testcase_18 | AC | 81 ms
77,840 KB |
testcase_19 | AC | 405 ms
80,880 KB |
testcase_20 | AC | 65 ms
72,056 KB |
testcase_21 | AC | 649 ms
82,328 KB |
testcase_22 | AC | 60 ms
68,136 KB |
testcase_23 | AC | 60 ms
68,828 KB |
testcase_24 | AC | 60 ms
67,212 KB |
testcase_25 | AC | 59 ms
67,732 KB |
testcase_26 | AC | 58 ms
68,152 KB |
testcase_27 | AC | 59 ms
67,916 KB |
testcase_28 | AC | 80 ms
77,620 KB |
testcase_29 | AC | 164 ms
79,420 KB |
testcase_30 | AC | 68 ms
72,284 KB |
testcase_31 | AC | 97 ms
78,168 KB |
testcase_32 | AC | 59 ms
67,712 KB |
testcase_33 | AC | 308 ms
79,860 KB |
testcase_34 | AC | 199 ms
79,580 KB |
testcase_35 | AC | 148 ms
78,676 KB |
testcase_36 | AC | 134 ms
79,100 KB |
testcase_37 | AC | 114 ms
78,272 KB |
testcase_38 | AC | 152 ms
78,476 KB |
testcase_39 | AC | 143 ms
78,576 KB |
testcase_40 | AC | 62 ms
69,032 KB |
testcase_41 | AC | 59 ms
67,356 KB |
testcase_42 | AC | 57 ms
68,400 KB |
testcase_43 | AC | 151 ms
80,200 KB |
testcase_44 | AC | 135 ms
79,716 KB |
testcase_45 | AC | 822 ms
82,776 KB |
testcase_46 | AC | 103 ms
78,296 KB |
testcase_47 | AC | 184 ms
79,880 KB |
testcase_48 | AC | 422 ms
80,740 KB |
testcase_49 | AC | 107 ms
78,180 KB |
testcase_50 | AC | 61 ms
67,648 KB |
testcase_51 | AC | 60 ms
67,504 KB |
testcase_52 | AC | 191 ms
78,816 KB |
testcase_53 | AC | 161 ms
78,572 KB |
testcase_54 | AC | 742 ms
83,448 KB |
testcase_55 | AC | 710 ms
83,264 KB |
testcase_56 | AC | 701 ms
83,384 KB |
testcase_57 | AC | 686 ms
83,620 KB |
testcase_58 | AC | 695 ms
83,896 KB |
testcase_59 | AC | 684 ms
84,016 KB |
ソースコード
from collections import deque from heapq import heappop, heappush from typing import List, Tuple INF = int(1e18) def minCostCycle(n: int, edges: List[Tuple[int, int, int]], directed: bool) -> int: adjList = [[] for _ in range(n)] maxWeight = 0 for u, v, w in edges: if w > maxWeight: maxWeight = w adjList[u].append((v, w)) if not directed: adjList[v].append((u, w)) res = INF for i in range(len(edges)): # remove edge i from_, to, weight = edges[i] dist = _bfs01(adjList, to, from_) if maxWeight <= 1 else _dijkstra(adjList, to, from_) cand = weight + dist if cand < res: res = cand return res def _bfs01(adjList: List[List[Tuple[int, int]]], start: int, target: int) -> int: n = len(adjList) dist = [INF] * n dist[start] = 0 queue = deque([start]) while queue: cur = queue.popleft() if cur == target: return dist[cur] for next, weight in adjList[cur]: if (cur, next) == (start, target) or (cur, next) == (target, start): continue cand = dist[cur] + weight if cand < dist[next]: dist[next] = cand if weight == 0: queue.appendleft(next) else: queue.append(next) return INF def _dijkstra(adjList: List[List[Tuple[int, int]]], start: int, target: int) -> int: n = len(adjList) dist = [INF] * n dist[start] = 0 pq = [(0, start)] while pq: curDist, cur = heappop(pq) if cur == target: return curDist if dist[cur] < curDist: continue for next, weight in adjList[cur]: if (cur, next) == (start, target) or (cur, next) == (target, start): continue cand = curDist + weight if cand < dist[next]: dist[next] = cand heappush(pq, (cand, next)) return INF if __name__ == "__main__": directed = int(input()) n, m = map(int, input().split()) edges = [] for _ in range(m): u, v, w = map(int, input().split()) u -= 1 v -= 1 edges.append((u, v, w)) res = minCostCycle(n, edges, directed == 1) if res == INF: res = -1 print(res)