結果

問題 No.1690 Power Grid
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-02-19 02:57:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 386 ms / 3,000 ms
コード長 2,409 bytes
コンパイル時間 323 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 76,592 KB
最終ジャッジ日時 2024-02-19 02:57:30
合計ジャッジ時間 6,730 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 38 ms
53,460 KB
testcase_02 AC 52 ms
66,404 KB
testcase_03 AC 35 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 41 ms
53,460 KB
testcase_06 AC 315 ms
75,808 KB
testcase_07 AC 290 ms
75,808 KB
testcase_08 AC 312 ms
75,808 KB
testcase_09 AC 299 ms
75,808 KB
testcase_10 AC 208 ms
75,836 KB
testcase_11 AC 179 ms
75,884 KB
testcase_12 AC 43 ms
53,460 KB
testcase_13 AC 73 ms
74,024 KB
testcase_14 AC 132 ms
76,344 KB
testcase_15 AC 167 ms
75,644 KB
testcase_16 AC 337 ms
76,456 KB
testcase_17 AC 260 ms
76,592 KB
testcase_18 AC 191 ms
76,404 KB
testcase_19 AC 359 ms
76,368 KB
testcase_20 AC 386 ms
76,496 KB
testcase_21 AC 199 ms
75,700 KB
testcase_22 AC 293 ms
76,588 KB
testcase_23 AC 203 ms
75,716 KB
testcase_24 AC 247 ms
76,480 KB
testcase_25 AC 221 ms
76,224 KB
testcase_26 AC 188 ms
75,916 KB
testcase_27 AC 37 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1690

class UnionFind:
    """
    UnionFindの基本的な処理を実装したクラス
    """

    def __init__(self, size):
        self.root = [i for i in range(size)]
        self.size = [1] * size

    def get_root(self, v):
        if v == self.root[v]:
            return v
        else:
            old_root = self.root[v]
            new_root = self.get_root(old_root)
            self.root[v] = new_root
            return new_root

    def merge(self, u, v):
        root_u = self.get_root(u)
        root_v = self.get_root(v)
        if root_u == root_v:
            return False

        if self.size[root_u] >= self.size[root_v]:
            self.size[root_u] += self.size[root_v]
            self.root[root_v] = root_u
            self.root[v] = root_u
        else:
            self.size[root_v] += self.size[root_u]
            self.root[root_u] = root_v
            self.root[u] = root_v
        return True
    

def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    edges = []
    for _ in range(M):
        x, y, z = map(int, input().split())
        edges.append((x - 1, y - 1, z))

    # 2点間の距離を求める
    dist_matrix = [[float("inf")] * N for _ in range(N)]
    for i in range(N):
        dist_matrix[i][i] = 0
    for x, y, z in edges:
        dist_matrix[x][y] = z
        dist_matrix[y][x] = z
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dist_matrix[i][j] = min(dist_matrix[i][j], dist_matrix[i][k] + dist_matrix[k][j])
    
    dist_array = []
    for i in range(N):
        for j in range(i + 1, N):
            dist_array.append((i, j, dist_matrix[i][j]))
    dist_array.sort(key=lambda x: x[2])


    answer_cost = float("inf")
    for bit in range(2 ** N):
        count = 0
        array = []
        cost = 0
        for i in range(N):
            if bit >> i & 1:
                cost += A[i]
                count += 1
                array.append(i)
        
        uf = UnionFind(N)
        if count == K:
            for i, j, z in dist_array:
                bi = (1 << i) | (1 << j)
                if bi & bit == bi:
                    if uf.merge(i, j):
                        cost += z
            answer_cost = min(answer_cost, cost)
    print(answer_cost)

        
    



if __name__ == "__main__":
    main()
0