結果
問題 | No.1690 Power Grid |
ユーザー | mkawa2 |
提出日時 | 2022-03-01 00:19:34 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 722 ms / 3,000 ms |
コード長 | 2,372 bytes |
コンパイル時間 | 253 ms |
コンパイル使用メモリ | 82,360 KB |
実行使用メモリ | 77,696 KB |
最終ジャッジ日時 | 2024-07-07 03:30:29 |
合計ジャッジ時間 | 7,049 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
53,736 KB |
testcase_01 | AC | 32 ms
52,864 KB |
testcase_02 | AC | 34 ms
53,804 KB |
testcase_03 | AC | 32 ms
53,104 KB |
testcase_04 | AC | 32 ms
53,920 KB |
testcase_05 | AC | 34 ms
53,080 KB |
testcase_06 | AC | 413 ms
77,356 KB |
testcase_07 | AC | 428 ms
77,112 KB |
testcase_08 | AC | 410 ms
76,904 KB |
testcase_09 | AC | 443 ms
77,020 KB |
testcase_10 | AC | 45 ms
64,160 KB |
testcase_11 | AC | 54 ms
69,148 KB |
testcase_12 | AC | 32 ms
53,416 KB |
testcase_13 | AC | 59 ms
73,592 KB |
testcase_14 | AC | 181 ms
77,292 KB |
testcase_15 | AC | 41 ms
61,648 KB |
testcase_16 | AC | 720 ms
77,572 KB |
testcase_17 | AC | 425 ms
77,412 KB |
testcase_18 | AC | 260 ms
77,464 KB |
testcase_19 | AC | 688 ms
77,396 KB |
testcase_20 | AC | 722 ms
77,696 KB |
testcase_21 | AC | 40 ms
61,664 KB |
testcase_22 | AC | 381 ms
77,352 KB |
testcase_23 | AC | 44 ms
63,428 KB |
testcase_24 | AC | 158 ms
77,304 KB |
testcase_25 | AC | 96 ms
76,744 KB |
testcase_26 | AC | 55 ms
70,760 KB |
testcase_27 | AC | 33 ms
53,256 KB |
ソースコード
import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 # md = 10**9+7 md = 998244353 class UnionFind: def __init__(self, n): self.state = [-1]*n self.size_table = [1]*n # cntはグループ数 self.cnt = n def root(self, u): stack = [] while self.state[u] >= 0: stack.append(u) u = self.state[u] for v in stack: self.state[v] = u return u def same(self, u, v): return self.root(u) == self.root(v) def merge(self, u, v): u = self.root(u) v = self.root(v) if u == v: return False du = -self.state[u] dv = -self.state[v] if du < dv: u, v = v, u if du == dv: self.state[u] -= 1 self.state[v] = u self.cnt -= 1 self.size_table[u] += self.size_table[v] return True # グループの要素数 def size(self, u): return self.size_table[self.root(u)] from itertools import combinations def cost(uu): res = sum(aa[u] for u in uu) ee = [] for i, u in enumerate(uu): for v in uu[:i]: ee.append((dist[u][v], u, v)) ee.sort(key=lambda x: x[0]) uf = UnionFind(n) for d, u, v in ee: if uf.same(u, v): continue uf.merge(u, v) res += d return res n, m, k = LI() aa = LI() dist = [[inf]*n for _ in range(n)] for u in range(n): dist[u][u] = 0 for _ in range(m): u, v, z = LI() u, v = u-1, v-1 dist[u][v] = dist[v][u] = z for v in range(n): for u in range(n): for w in range(n): dist[u][w] = min(dist[u][w], dist[u][v]+dist[v][w]) ans = inf for uu in combinations(range(n), k): ans = min(ans, cost(uu)) print(ans)