結果
問題 | No.1690 Power Grid |
ユーザー | qib |
提出日時 | 2022-12-19 00:05:21 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,601 bytes |
コンパイル時間 | 409 ms |
コンパイル使用メモリ | 81,976 KB |
実行使用メモリ | 72,568 KB |
最終ジャッジ日時 | 2024-11-18 00:03:38 |
合計ジャッジ時間 | 2,969 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
52,608 KB |
testcase_01 | AC | 38 ms
52,608 KB |
testcase_02 | AC | 39 ms
52,224 KB |
testcase_03 | AC | 38 ms
52,736 KB |
testcase_04 | AC | 39 ms
51,968 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | AC | 70 ms
68,608 KB |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | AC | 39 ms
52,736 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 67 ms
70,272 KB |
testcase_27 | AC | 40 ms
52,864 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 k in range(n): for u in range(n): for v in range(n): if dist[u][k] + dist[k][v] < dist[u][v]: dist[u][v] = dist[u][k] + dist[k][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)