結果

問題 No.1690 Power Grid
ユーザー tassei903tassei903
提出日時 2021-09-24 22:54:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,074 ms / 3,000 ms
コード長 2,644 bytes
コンパイル時間 447 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 79,976 KB
最終ジャッジ日時 2023-09-18 21:57:53
合計ジャッジ時間 12,319 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
71,780 KB
testcase_01 AC 100 ms
71,472 KB
testcase_02 AC 101 ms
76,136 KB
testcase_03 AC 95 ms
71,832 KB
testcase_04 AC 94 ms
71,556 KB
testcase_05 AC 98 ms
71,868 KB
testcase_06 AC 910 ms
78,228 KB
testcase_07 AC 930 ms
78,232 KB
testcase_08 AC 943 ms
78,280 KB
testcase_09 AC 948 ms
78,128 KB
testcase_10 AC 122 ms
77,720 KB
testcase_11 AC 120 ms
77,568 KB
testcase_12 AC 99 ms
71,596 KB
testcase_13 AC 132 ms
77,600 KB
testcase_14 AC 276 ms
78,704 KB
testcase_15 AC 112 ms
77,436 KB
testcase_16 AC 1,044 ms
79,520 KB
testcase_17 AC 608 ms
79,328 KB
testcase_18 AC 394 ms
78,904 KB
testcase_19 AC 1,045 ms
79,264 KB
testcase_20 AC 1,074 ms
79,976 KB
testcase_21 AC 111 ms
77,572 KB
testcase_22 AC 375 ms
78,872 KB
testcase_23 AC 111 ms
77,760 KB
testcase_24 AC 211 ms
78,808 KB
testcase_25 AC 161 ms
78,436 KB
testcase_26 AC 120 ms
77,696 KB
testcase_27 AC 95 ms
71,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0