結果
問題 | No.114 遠い未来 |
ユーザー | Mao-beta |
提出日時 | 2024-03-08 15:22:59 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,289 ms / 5,000 ms |
コード長 | 4,968 bytes |
コンパイル時間 | 401 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 81,592 KB |
最終ジャッジ日時 | 2024-09-29 18:48:49 |
合計ジャッジ時間 | 19,451 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 256 ms
77,672 KB |
testcase_01 | AC | 2,559 ms
78,848 KB |
testcase_02 | AC | 607 ms
77,260 KB |
testcase_03 | AC | 217 ms
77,696 KB |
testcase_04 | AC | 45 ms
56,576 KB |
testcase_05 | AC | 119 ms
77,440 KB |
testcase_06 | AC | 1,562 ms
78,656 KB |
testcase_07 | AC | 58 ms
66,176 KB |
testcase_08 | AC | 59 ms
67,072 KB |
testcase_09 | AC | 124 ms
77,208 KB |
testcase_10 | AC | 923 ms
79,504 KB |
testcase_11 | AC | 2,447 ms
81,592 KB |
testcase_12 | AC | 3,289 ms
78,288 KB |
testcase_13 | AC | 51 ms
64,000 KB |
testcase_14 | AC | 1,589 ms
78,712 KB |
testcase_15 | AC | 55 ms
64,384 KB |
testcase_16 | AC | 790 ms
78,336 KB |
testcase_17 | AC | 824 ms
78,592 KB |
testcase_18 | AC | 871 ms
79,360 KB |
testcase_19 | AC | 498 ms
78,676 KB |
testcase_20 | AC | 259 ms
78,336 KB |
testcase_21 | AC | 183 ms
78,808 KB |
testcase_22 | AC | 185 ms
78,592 KB |
testcase_23 | AC | 134 ms
78,480 KB |
testcase_24 | AC | 127 ms
78,336 KB |
testcase_25 | AC | 107 ms
76,800 KB |
testcase_26 | AC | 48 ms
57,472 KB |
testcase_27 | AC | 49 ms
57,088 KB |
ソースコード
import sys import math import bisect from heapq import heapify, heappop, heappush from collections import deque, defaultdict, Counter from functools import lru_cache from itertools import accumulate, combinations, permutations, product sys.setrecursionlimit(1000000) MOD = 10 ** 9 + 7 MOD99 = 998244353 input = lambda: sys.stdin.readline().strip() NI = lambda: int(input()) NMI = lambda: map(int, input().split()) NLI = lambda: list(NMI()) SI = lambda: input() SMI = lambda: input().split() SLI = lambda: list(SMI()) EI = lambda m: [NLI() for _ in range(m)] def main(): N, M, T = NMI() ABC = EI(M) ABC = [[x-1, y-1, z] for x, y, z in ABC] V = [NI() for _ in range(T)] V = [x-1 for x in V] INF = 10 ** 15 if T <= 14: # 最小シュタイナー木 # ワーシャルフロイド D = [[INF]*N for _ in range(N)] for i in range(N): D[i][i] = 0 for a, b, c in ABC: D[a][b] = c D[b][a] = c for k in range(N): for i in range(N): for j in range(N): D[i][j] = min(D[i][j], D[i][k] + D[k][j]) # dp[i][S]: iを端点に持ち、Vの部分集合S(T-bit)を含むシュタイナー木の重み dp = [[INF] * (1<<T) for _ in range(N)] # 各vについて、端点がiのときの初期値 for vi in range(T): for i in range(N): dp[i][1<<vi] = D[i][V[vi]] dp[V[vi]][1<<vi] = 0 for i in range(N): dp[i][0] = 0 def gen_subset(S): s = (S-1) & S while s > 0: yield s s = (s-1) & S # O(3^T)の部分集合DP # トータルでO(N*3^T + N^2*2^T) for S in range(1, 1<<T): for i in range(N): for E in gen_subset(S): dp[i][S] = min(dp[i][S], dp[i][S-E] + dp[i][E]) for i in range(N): for j in range(N): dp[i][S] = min(dp[i][S], dp[j][S] + D[i][j]) ans = INF for i in range(N): for S in range(1<<T): ans = min(ans, dp[i][S] + dp[i][(1<<T)-1-S]) print(ans) else: # N-T <= 21 # 使わない頂点の集合を全探索してMST class UnionFind: def __init__(self, n): # 親要素のノード番号を格納 xが根のとき-(サイズ)を格納 self.par = [-1 for i in range(n)] self.n = n self.group_num = n def rebuild(self): for i in range(self.n): self.par[i] = -1 self.group_num = self.n def find(self, x): # 根ならその番号を返す if self.par[x] < 0: return x else: # 親の親は親 self.par[x] = self.find(self.par[x]) return self.par[x] def is_same(self, x, y): # 根が同じならTrue return self.find(x) == self.find(y) def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return # 木のサイズを比較し、小さいほうから大きいほうへつなぐ if self.par[x] > self.par[y]: x, y = y, x self.group_num -= 1 self.par[x] += self.par[y] self.par[y] = x def MST(N, edges, target, uf, cnt): """ 要UnionFind N頂点のうち、target[i]==1の点のみの最小全域木の長さ edges = [[u, v, cost], ....] (0-index) (sort済み) """ uf.rebuild() # edges.sort(key=lambda x: x[-1]) res = 0 for a, b, c in edges: if target[a] == 0 or target[b] == 0: continue if uf.is_same(a, b): continue else: res += c cnt -= 1 uf.unite(a, b) if cnt == 1: return res return INF ABC.sort(key=lambda x: x[-1]) Vbar = [i for i in range(N) if i not in V] Vbn = len(Vbar) target = [1] * N uf = UnionFind(N) ans = INF for case in range(1<<Vbn): cnt = T for i in range(Vbn): if (case >> i) & 1: target[Vbar[i]] = 1 cnt += 1 else: target[Vbar[i]] = 0 res = MST(N, ABC, target, uf, cnt) ans = min(ans, res) if ans == T-1: print(ans) return print(ans) if __name__ == "__main__": main()