結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | maspy |
提出日時 | 2020-12-09 18:11:04 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,328 bytes |
コンパイル時間 | 179 ms |
コンパイル使用メモリ | 81,824 KB |
実行使用メモリ | 80,620 KB |
最終ジャッジ日時 | 2024-09-20 01:21:07 |
合計ジャッジ時間 | 12,454 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
52,992 KB |
testcase_01 | AC | 39 ms
52,736 KB |
testcase_02 | AC | 39 ms
52,608 KB |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | AC | 69 ms
76,160 KB |
testcase_08 | AC | 85 ms
76,672 KB |
testcase_09 | AC | 552 ms
79,416 KB |
testcase_10 | AC | 84 ms
76,464 KB |
testcase_11 | AC | 498 ms
79,388 KB |
testcase_12 | AC | 188 ms
77,648 KB |
testcase_13 | AC | 320 ms
78,280 KB |
testcase_14 | AC | 51 ms
68,096 KB |
testcase_15 | AC | 89 ms
76,748 KB |
testcase_16 | AC | 163 ms
77,068 KB |
testcase_17 | AC | 58 ms
73,088 KB |
testcase_18 | AC | 57 ms
72,704 KB |
testcase_19 | AC | 401 ms
78,892 KB |
testcase_20 | AC | 49 ms
63,488 KB |
testcase_21 | AC | 522 ms
79,004 KB |
testcase_22 | RE | - |
testcase_23 | AC | 38 ms
52,992 KB |
testcase_24 | RE | - |
testcase_25 | AC | 39 ms
52,992 KB |
testcase_26 | RE | - |
testcase_27 | AC | 38 ms
52,608 KB |
testcase_28 | AC | 55 ms
71,040 KB |
testcase_29 | AC | 152 ms
76,928 KB |
testcase_30 | AC | 52 ms
63,488 KB |
testcase_31 | AC | 89 ms
75,888 KB |
testcase_32 | AC | 39 ms
52,992 KB |
testcase_33 | AC | 309 ms
77,880 KB |
testcase_34 | AC | 191 ms
77,084 KB |
testcase_35 | AC | 133 ms
77,028 KB |
testcase_36 | AC | 129 ms
76,608 KB |
testcase_37 | AC | 95 ms
76,032 KB |
testcase_38 | AC | 157 ms
76,888 KB |
testcase_39 | AC | 107 ms
76,244 KB |
testcase_40 | AC | 40 ms
52,864 KB |
testcase_41 | AC | 39 ms
52,992 KB |
testcase_42 | RE | - |
testcase_43 | AC | 123 ms
77,440 KB |
testcase_44 | AC | 111 ms
77,696 KB |
testcase_45 | AC | 814 ms
80,172 KB |
testcase_46 | AC | 78 ms
76,304 KB |
testcase_47 | AC | 153 ms
77,116 KB |
testcase_48 | AC | 370 ms
79,412 KB |
testcase_49 | AC | 95 ms
76,416 KB |
testcase_50 | AC | 36 ms
53,108 KB |
testcase_51 | AC | 41 ms
52,992 KB |
testcase_52 | AC | 174 ms
76,928 KB |
testcase_53 | AC | 138 ms
77,076 KB |
testcase_54 | AC | 552 ms
79,936 KB |
testcase_55 | AC | 551 ms
79,572 KB |
testcase_56 | AC | 542 ms
79,428 KB |
testcase_57 | AC | 552 ms
80,092 KB |
testcase_58 | AC | 562 ms
80,228 KB |
testcase_59 | AC | 549 ms
80,620 KB |
ソースコード
import sys import bisect from heapq import * read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines INF = 1 << 60 T = int(readline()) N, M = map(int, readline().split()) G = [] for e in range(M): u, v, c = map(int, readline().split()) G.append((u, v, c)) if T == 0: G.append((v, u, c)) G.sort() frm, to, cost = zip(*G) idx = [bisect.bisect_left(frm, v) for v in range(N + 2)] def dijkstra(S, T): dist = [INF] * (N + 1) dist[S] = 0 q = [0 << 15 | S] while q: dv, v = divmod(heappop(q), 1 << 15) if dv > dist[v]: continue if v == T: return dv for e in range(idx[v], idx[v + 1]): _, w, c = G[e] if (v, w) == (S, T) or (w, v) == (T, S): continue dw = dv + c if dist[w] > dw: dist[w] = dw heappush(q, dw << 15 | w) return INF def main(): done = set() ans = INF for u, v, c in G: if (u, v) in done: continue done.add((u, v)) done.add((v, u)) x = dijkstra(v, u) # v から u に、直接ではない行く x = x + c # cycle length ans = min(ans, x) if ans == INF: ans = -1 return ans print(main())