結果
問題 | No.1690 Power Grid |
ユーザー | tassei903 |
提出日時 | 2021-09-24 22:54:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 906 ms / 3,000 ms |
コード長 | 2,644 bytes |
コンパイル時間 | 340 ms |
コンパイル使用メモリ | 82,092 KB |
実行使用メモリ | 77,824 KB |
最終ジャッジ日時 | 2024-07-05 11:08:49 |
合計ジャッジ時間 | 9,309 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,744 KB |
testcase_01 | AC | 43 ms
54,312 KB |
testcase_02 | AC | 44 ms
56,160 KB |
testcase_03 | AC | 43 ms
54,528 KB |
testcase_04 | AC | 42 ms
54,872 KB |
testcase_05 | AC | 42 ms
55,052 KB |
testcase_06 | AC | 777 ms
77,008 KB |
testcase_07 | AC | 797 ms
77,092 KB |
testcase_08 | AC | 785 ms
76,972 KB |
testcase_09 | AC | 743 ms
76,900 KB |
testcase_10 | AC | 61 ms
69,632 KB |
testcase_11 | AC | 59 ms
68,864 KB |
testcase_12 | AC | 42 ms
54,528 KB |
testcase_13 | AC | 81 ms
76,608 KB |
testcase_14 | AC | 190 ms
77,184 KB |
testcase_15 | AC | 52 ms
63,616 KB |
testcase_16 | AC | 891 ms
77,692 KB |
testcase_17 | AC | 512 ms
77,824 KB |
testcase_18 | AC | 308 ms
77,184 KB |
testcase_19 | AC | 822 ms
77,056 KB |
testcase_20 | AC | 906 ms
77,568 KB |
testcase_21 | AC | 53 ms
64,512 KB |
testcase_22 | AC | 305 ms
77,184 KB |
testcase_23 | AC | 54 ms
64,512 KB |
testcase_24 | AC | 145 ms
77,056 KB |
testcase_25 | AC | 100 ms
76,812 KB |
testcase_26 | AC | 64 ms
71,296 KB |
testcase_27 | AC | 47 ms
54,400 KB |
ソースコード
import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) sys.setrecursionlimit(10**7) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) from itertools import combinations n,m,K = na() a = na() INF = 10**18 ans = 10**18 cost = [[INF for j in range(n)]for i in range(n)] for i in range(m): x,y,z = na() x-=1 y-=1 cost[x][y] = z cost[y][x] = z V = n # cost[i][j]: 頂点v_iから頂点v_jへ到達するための辺コストの和 for k in range(V): for i in range(V): for j in range(V): if cost[i][k]!=INF and cost[k][j]!=INF: cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]) e = [] for i in range(n): for j in range(i+1,n): e.append((cost[i][j],i,j)) e = sorted(e) for cm in combinations(range(n), K): s = sum([a[i] for i in range(n) if i in cm]) uf = UnionFind(n) for z,x,y in e: if not(x in cm and y in cm): continue if uf.same(x,y):continue uf.union(x,y) s += z if uf.size(cm[0])!=K: print("!") else: ans = min(ans, s) print(ans) #print(ans)