結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | 草苺奶昔 |
提出日時 | 2023-04-02 00:59:50 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,248 bytes |
コンパイル時間 | 243 ms |
コンパイル使用メモリ | 81,760 KB |
実行使用メモリ | 152,140 KB |
最終ジャッジ日時 | 2023-10-24 20:07:43 |
合計ジャッジ時間 | 17,318 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 61 ms
67,732 KB |
testcase_01 | WA | - |
testcase_02 | AC | 60 ms
67,732 KB |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | WA | - |
testcase_08 | AC | 161 ms
80,048 KB |
testcase_09 | AC | 950 ms
130,076 KB |
testcase_10 | WA | - |
testcase_11 | AC | 725 ms
98,112 KB |
testcase_12 | AC | 279 ms
87,184 KB |
testcase_13 | AC | 440 ms
98,540 KB |
testcase_14 | AC | 91 ms
77,728 KB |
testcase_15 | AC | 181 ms
81,440 KB |
testcase_16 | AC | 216 ms
79,104 KB |
testcase_17 | AC | 114 ms
78,424 KB |
testcase_18 | AC | 104 ms
78,056 KB |
testcase_19 | WA | - |
testcase_20 | AC | 90 ms
77,568 KB |
testcase_21 | AC | 821 ms
109,204 KB |
testcase_22 | RE | - |
testcase_23 | WA | - |
testcase_24 | RE | - |
testcase_25 | AC | 62 ms
67,740 KB |
testcase_26 | RE | - |
testcase_27 | AC | 62 ms
67,740 KB |
testcase_28 | AC | 105 ms
78,036 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 456 ms
94,604 KB |
testcase_34 | AC | 261 ms
83,824 KB |
testcase_35 | WA | - |
testcase_36 | AC | 153 ms
78,564 KB |
testcase_37 | AC | 131 ms
78,184 KB |
testcase_38 | AC | 190 ms
78,940 KB |
testcase_39 | AC | 147 ms
78,348 KB |
testcase_40 | AC | 62 ms
67,740 KB |
testcase_41 | WA | - |
testcase_42 | RE | - |
testcase_43 | AC | 274 ms
85,980 KB |
testcase_44 | AC | 204 ms
82,132 KB |
testcase_45 | AC | 1,144 ms
152,140 KB |
testcase_46 | AC | 112 ms
78,348 KB |
testcase_47 | AC | 284 ms
87,624 KB |
testcase_48 | WA | - |
testcase_49 | WA | - |
testcase_50 | AC | 60 ms
67,740 KB |
testcase_51 | AC | 59 ms
67,740 KB |
testcase_52 | AC | 236 ms
80,588 KB |
testcase_53 | AC | 160 ms
78,828 KB |
testcase_54 | AC | 677 ms
98,348 KB |
testcase_55 | AC | 673 ms
98,220 KB |
testcase_56 | AC | 657 ms
97,108 KB |
testcase_57 | AC | 866 ms
114,812 KB |
testcase_58 | AC | 855 ms
115,100 KB |
testcase_59 | AC | 863 ms
114,236 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: """O(V*(V+E)*logV)求最小环权值之和,不存在返回INF.""" res = INF maxWeight = max(w for *_, w in edges) res = INF for i, e1 in enumerate(edges): from_, to, weight = e1 adjList = [[] for _ in range(n)] for j, e2 in enumerate(edges): if i != j: u, v, w = e2 adjList[u].append((v, w)) if not directed: adjList[v].append((u, w)) cand = _bfs01(adjList, from_, to) if maxWeight <= 1 else _dijkstra(adjList, from_, to) res = min(res, weight + 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]: 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]: 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)