結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | ningenMe |
提出日時 | 2020-12-02 20:55:08 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,434 ms / 2,000 ms |
コード長 | 2,643 bytes |
コンパイル時間 | 147 ms |
コンパイル使用メモリ | 81,652 KB |
実行使用メモリ | 93,820 KB |
最終ジャッジ日時 | 2024-09-20 01:20:46 |
合計ジャッジ時間 | 16,617 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
53,292 KB |
testcase_01 | AC | 37 ms
53,428 KB |
testcase_02 | AC | 37 ms
54,256 KB |
testcase_03 | AC | 38 ms
53,284 KB |
testcase_04 | AC | 38 ms
53,004 KB |
testcase_05 | AC | 53 ms
70,456 KB |
testcase_06 | AC | 46 ms
65,628 KB |
testcase_07 | AC | 92 ms
76,468 KB |
testcase_08 | AC | 225 ms
77,524 KB |
testcase_09 | AC | 1,035 ms
88,676 KB |
testcase_10 | AC | 108 ms
76,796 KB |
testcase_11 | AC | 865 ms
85,660 KB |
testcase_12 | AC | 367 ms
79,404 KB |
testcase_13 | AC | 580 ms
82,460 KB |
testcase_14 | AC | 129 ms
76,784 KB |
testcase_15 | AC | 102 ms
76,460 KB |
testcase_16 | AC | 93 ms
76,184 KB |
testcase_17 | AC | 76 ms
76,760 KB |
testcase_18 | AC | 74 ms
76,424 KB |
testcase_19 | AC | 360 ms
78,612 KB |
testcase_20 | AC | 98 ms
76,664 KB |
testcase_21 | AC | 662 ms
79,860 KB |
testcase_22 | AC | 37 ms
53,012 KB |
testcase_23 | AC | 38 ms
53,468 KB |
testcase_24 | AC | 38 ms
53,468 KB |
testcase_25 | AC | 37 ms
53,292 KB |
testcase_26 | AC | 38 ms
54,052 KB |
testcase_27 | AC | 38 ms
52,948 KB |
testcase_28 | AC | 160 ms
77,096 KB |
testcase_29 | AC | 161 ms
77,400 KB |
testcase_30 | AC | 50 ms
64,880 KB |
testcase_31 | AC | 83 ms
76,196 KB |
testcase_32 | AC | 39 ms
54,868 KB |
testcase_33 | AC | 490 ms
81,052 KB |
testcase_34 | AC | 327 ms
78,848 KB |
testcase_35 | AC | 94 ms
76,216 KB |
testcase_36 | AC | 68 ms
71,232 KB |
testcase_37 | AC | 56 ms
65,860 KB |
testcase_38 | AC | 106 ms
76,336 KB |
testcase_39 | AC | 83 ms
76,208 KB |
testcase_40 | AC | 40 ms
54,124 KB |
testcase_41 | AC | 39 ms
54,152 KB |
testcase_42 | AC | 38 ms
53,160 KB |
testcase_43 | AC | 131 ms
77,156 KB |
testcase_44 | AC | 116 ms
76,692 KB |
testcase_45 | AC | 1,434 ms
93,820 KB |
testcase_46 | AC | 84 ms
76,400 KB |
testcase_47 | AC | 456 ms
81,588 KB |
testcase_48 | AC | 492 ms
81,308 KB |
testcase_49 | AC | 93 ms
76,512 KB |
testcase_50 | AC | 38 ms
54,044 KB |
testcase_51 | AC | 37 ms
53,404 KB |
testcase_52 | AC | 215 ms
77,792 KB |
testcase_53 | AC | 149 ms
76,920 KB |
testcase_54 | AC | 522 ms
80,036 KB |
testcase_55 | AC | 469 ms
79,908 KB |
testcase_56 | AC | 511 ms
80,296 KB |
testcase_57 | AC | 1,019 ms
89,892 KB |
testcase_58 | AC | 1,056 ms
89,312 KB |
testcase_59 | AC | 1,051 ms
89,344 KB |
ソースコード
import sys import heapq read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines inf = 1e15 def solve_undirected_graph(): N, M = map(int, readline().split()) edges = [[] for i in range(N)] for i in range(M): u, v, w = map(int, readline().split()) u-=1 v-=1 edges[u].append((v,w)) edges[v].append((u,w)) ans = inf for root in range(N): dist = [inf for i in range(N)] label = [-1 for i in range(N)] q = [0<<13 | root] dist[root] = 0 label[root] = root while len(q) > 0: cost,from_node = divmod(heapq.heappop(q),1 << 13) if cost > dist[from_node]: continue for to_node,w in edges[from_node]: new_cost = cost+w if dist[to_node] > new_cost: dist[to_node] = new_cost if from_node == root: label[to_node] = to_node else : label[to_node] = label[from_node] heapq.heappush(q,(new_cost<<13 | to_node)) for from_node in range(N): for to_node,w in edges[from_node]: if from_node == root or to_node == root: continue if label[from_node] == label[to_node]: continue if ans > dist[from_node] + dist[to_node] + w: ans = dist[from_node] + dist[to_node] + w return ans def solve_directed_graph(): N, M = map(int, readline().split()) edges = [[] for i in range(N)] for i in range(M): u, v, w = map(int, readline().split()) u-=1 v-=1 edges[u].append((v,w)) ans = inf for root in range(N): dist = [inf for i in range(N)] q = [0<<13 | root] dist[root] = 0 while len(q) > 0: cost,from_node = divmod(heapq.heappop(q),1 << 13) if cost > dist[from_node]: continue for to_node,w in edges[from_node]: new_cost = cost+w if dist[to_node] > new_cost: dist[to_node] = new_cost heapq.heappush(q,(new_cost<<13 | to_node)) if to_node == root and ans > dist[from_node] + w: ans = dist[from_node] + w return ans def main(): T = int(readline()) ans = inf if T == 0: ans = solve_undirected_graph() else: ans = solve_directed_graph() if ans == inf: ans = -1 print(ans) main()