結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | 草苺奶昔 |
提出日時 | 2023-04-02 00:59:50 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,248 bytes |
コンパイル時間 | 236 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 152,740 KB |
最終ジャッジ日時 | 2024-09-24 14:45:32 |
合計ジャッジ時間 | 16,649 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 57 ms
67,020 KB |
testcase_01 | WA | - |
testcase_02 | AC | 59 ms
67,232 KB |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | WA | - |
testcase_08 | AC | 156 ms
80,244 KB |
testcase_09 | AC | 890 ms
129,512 KB |
testcase_10 | WA | - |
testcase_11 | AC | 684 ms
98,376 KB |
testcase_12 | AC | 272 ms
87,276 KB |
testcase_13 | AC | 432 ms
99,364 KB |
testcase_14 | AC | 88 ms
78,092 KB |
testcase_15 | AC | 178 ms
81,692 KB |
testcase_16 | AC | 208 ms
79,492 KB |
testcase_17 | AC | 109 ms
78,796 KB |
testcase_18 | AC | 99 ms
78,848 KB |
testcase_19 | WA | - |
testcase_20 | AC | 89 ms
78,040 KB |
testcase_21 | AC | 769 ms
109,876 KB |
testcase_22 | RE | - |
testcase_23 | WA | - |
testcase_24 | RE | - |
testcase_25 | AC | 59 ms
68,540 KB |
testcase_26 | RE | - |
testcase_27 | AC | 59 ms
67,312 KB |
testcase_28 | AC | 101 ms
78,516 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 457 ms
94,900 KB |
testcase_34 | AC | 267 ms
84,180 KB |
testcase_35 | WA | - |
testcase_36 | AC | 150 ms
78,904 KB |
testcase_37 | AC | 127 ms
78,548 KB |
testcase_38 | AC | 184 ms
79,264 KB |
testcase_39 | AC | 142 ms
78,936 KB |
testcase_40 | AC | 60 ms
68,760 KB |
testcase_41 | WA | - |
testcase_42 | RE | - |
testcase_43 | AC | 267 ms
86,124 KB |
testcase_44 | AC | 196 ms
82,208 KB |
testcase_45 | AC | 1,121 ms
152,740 KB |
testcase_46 | AC | 110 ms
78,832 KB |
testcase_47 | AC | 285 ms
87,800 KB |
testcase_48 | WA | - |
testcase_49 | WA | - |
testcase_50 | AC | 59 ms
68,032 KB |
testcase_51 | AC | 58 ms
68,820 KB |
testcase_52 | AC | 227 ms
81,004 KB |
testcase_53 | AC | 158 ms
79,480 KB |
testcase_54 | AC | 651 ms
99,020 KB |
testcase_55 | AC | 666 ms
98,660 KB |
testcase_56 | AC | 651 ms
97,508 KB |
testcase_57 | AC | 813 ms
115,284 KB |
testcase_58 | AC | 809 ms
115,156 KB |
testcase_59 | AC | 832 ms
116,064 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)