結果
問題 | No.1690 Power Grid |
ユーザー | qib |
提出日時 | 2022-12-19 00:11:01 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 698 ms / 3,000 ms |
コード長 | 1,601 bytes |
コンパイル時間 | 328 ms |
コンパイル使用メモリ | 82,292 KB |
実行使用メモリ | 77,312 KB |
最終ジャッジ日時 | 2024-11-18 00:04:20 |
合計ジャッジ時間 | 6,665 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 31 ms
52,480 KB |
testcase_01 | AC | 31 ms
52,608 KB |
testcase_02 | AC | 30 ms
52,352 KB |
testcase_03 | AC | 31 ms
51,968 KB |
testcase_04 | AC | 31 ms
52,352 KB |
testcase_05 | AC | 30 ms
52,480 KB |
testcase_06 | AC | 400 ms
77,096 KB |
testcase_07 | AC | 394 ms
77,312 KB |
testcase_08 | AC | 388 ms
77,056 KB |
testcase_09 | AC | 399 ms
77,056 KB |
testcase_10 | AC | 39 ms
60,544 KB |
testcase_11 | AC | 48 ms
68,224 KB |
testcase_12 | AC | 31 ms
52,224 KB |
testcase_13 | AC | 59 ms
74,624 KB |
testcase_14 | AC | 158 ms
77,052 KB |
testcase_15 | AC | 41 ms
61,056 KB |
testcase_16 | AC | 682 ms
77,248 KB |
testcase_17 | AC | 407 ms
77,256 KB |
testcase_18 | AC | 222 ms
77,084 KB |
testcase_19 | AC | 677 ms
77,260 KB |
testcase_20 | AC | 698 ms
77,288 KB |
testcase_21 | AC | 32 ms
52,864 KB |
testcase_22 | AC | 354 ms
77,312 KB |
testcase_23 | AC | 39 ms
60,928 KB |
testcase_24 | AC | 129 ms
77,048 KB |
testcase_25 | AC | 90 ms
76,544 KB |
testcase_26 | AC | 53 ms
70,656 KB |
testcase_27 | AC | 31 ms
52,608 KB |
ソースコード
class UnionFind: def __init__(self, n): self.node = [-1 for _ in range(n)] def root(self, v): if self.node[v] < 0: return v st = [] while self.node[v] >= 0: st.append(v) v = self.node[v] for u in st: self.node[u] = v return v def size(self, v): v = self.root(v) return (- self.node[v]) def same(self, u, v): return self.root(u) == self.root(v) def unite(self, u, v): ru = self.root(u) rv = self.root(v) if ru == rv: return du = self.node[ru] dv = self.node[rv] if du <= dv: self.node[rv] = ru self.node[ru] += dv else: self.node[ru] = rv self.node[rv] += du from itertools import combinations INF = 1 << 60 n, m, k = map(int, input().split()) a = list(map(int, input().split())) if k == 1: print(min(a)) exit() dist = [[INF for _ in range(n)] for _ in range(n)] for u in range(n): dist[u][u] = 0 for _ in range(m): x, y, z = map(int, input().split()) x -= 1 y -= 1 dist[x][y] = z dist[y][x] = z for t in range(n): for u in range(n): for v in range(n): if dist[u][t] + dist[t][v] < dist[u][v]: dist[u][v] = dist[u][t] + dist[t][v] ans = INF for vs in combinations([u for u in range(n)], k): edges = [] for i in range(len(vs) - 1): for j in range(i + 1, len(vs)): edges.append((dist[vs[i]][vs[j]], i, j)) edges.sort(key=lambda x: x[0]) uf = UnionFind(k) cnt = sum(a[v] for v in vs) for w, u, v in edges: if not uf.same(u, v): uf.unite(u, v) cnt += w ans = min(ans, cnt) print(ans)