結果

問題 No.1690 Power Grid
ユーザー zkouzkou
提出日時 2021-09-24 23:21:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,870 ms / 3,000 ms
コード長 5,081 bytes
コンパイル時間 550 ms
コンパイル使用メモリ 87,164 KB
実行使用メモリ 80,292 KB
最終ジャッジ日時 2023-09-18 22:23:23
合計ジャッジ時間 15,945 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,444 KB
testcase_01 AC 74 ms
71,240 KB
testcase_02 AC 73 ms
71,472 KB
testcase_03 AC 73 ms
71,224 KB
testcase_04 AC 73 ms
71,232 KB
testcase_05 AC 72 ms
71,436 KB
testcase_06 AC 965 ms
79,564 KB
testcase_07 AC 994 ms
79,584 KB
testcase_08 AC 933 ms
79,604 KB
testcase_09 AC 981 ms
79,580 KB
testcase_10 AC 90 ms
76,664 KB
testcase_11 AC 107 ms
77,652 KB
testcase_12 AC 73 ms
71,452 KB
testcase_13 AC 112 ms
77,684 KB
testcase_14 AC 339 ms
78,388 KB
testcase_15 AC 88 ms
76,576 KB
testcase_16 AC 1,870 ms
80,132 KB
testcase_17 AC 970 ms
79,720 KB
testcase_18 AC 463 ms
78,244 KB
testcase_19 AC 1,803 ms
79,596 KB
testcase_20 AC 1,823 ms
80,292 KB
testcase_21 AC 84 ms
76,508 KB
testcase_22 AC 862 ms
79,552 KB
testcase_23 AC 86 ms
76,508 KB
testcase_24 AC 264 ms
78,648 KB
testcase_25 AC 159 ms
78,132 KB
testcase_26 AC 113 ms
77,576 KB
testcase_27 AC 74 ms
71,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class dsu:
    """Data structures and algorithms for disjoint set union problems.

    Given an undirected graph, it processes the following queries in O(alpha(n)) time (amortized).

    >   Edge addition

    >   Deciding whether given two vertices are in the same connected component

    Each connected component internally has a representative vertex.

    When two connected components are merged by edge addition, 
    one of the two representatives of these connected components becomes the representative of the new connected component.
    """

    __slots__ = ["n", "parent_or_size"]

    def __init__(self, n):
        """It creates an undirected graph with n vertices and 0 edges.

        Constraints
        -----------

        >   0 <= n <= 10 ** 8

        Complexity
        ----------

        >   O(n)
        """
        self.n = n
        self.parent_or_size = [-1] * n

    def merge(self, a, b):
        """It adds an edge (a, b).

        If the vertices a and b were in the same connected component, 
        it returns the representative of this connected component. 
        Otherwise, it returns the representative of the new connected component.

        Constraints
        -----------

        >   0 <= a < n 

        >   0 <= b < n

        Complexity
        ----------

        >   O(alpha(n)) amortized
        """
        # assert 0 <= a < self.n
        # assert 0 <= b < self.n
        x = self.leader(a)
        y = self.leader(b)
        if x == y:
            return x
        if self.parent_or_size[y] < self.parent_or_size[x]:
            x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return x

    def same(self, a, b):
        """It returns whether the vertices a and b are in the same connected component.

        Constraints
        -----------

        >   0 <= a < n

        >   0 <= b < n

        Complexity
        ----------

        >   O(alpha(n)) amortized
        """
        # assert 0 <= a < self.n
        # assert 0 <= b < self.n
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        """It returns the representative of the connected component that contains the vertex a.

        Constraints
        -----------

        >   0 <= a < n

        Complexity
        ----------

        >   O(alpha(n)) amortized
        """
        # assert 0 <= a < self.n
        path = []
        while self.parent_or_size[a] >= 0:
            path.append(a)
            a = self.parent_or_size[a]
        for child in path:
            self.parent_or_size[child] = a
        return a

    def size(self, a):
        """It returns the size of the connected component that contains the vertex a.

        Constraints
        -----------

        >   0 <= a < n

        Complexity
        ----------

        >   O(alpha(n)) amortized
        """
        # assert 0 <= a < self.n
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        """It divides the graph into connected components and returns the list of them.

        More precisely, it returns the list of the "list of the vertices in a connected component". 
        Both of the orders of the connected components and the vertices are undefined.

        Complexity
        ----------

        >   O(n)
        """
        result = [[] for _ in range(self.n)]
        for i in range(self.n):
            result[self.leader(i)].append(i)
        return [g for g in result if g]


N, M, K = map(int, input().split())
As = list(map(int, input().split()))
XYZs = [tuple(map(int, input().split())) for _ in range(M)]

INF = 10 ** 18

adj = [[INF] * N for _ in range(N)]
for X, Y, Z in XYZs:
    X -= 1; Y -= 1
    adj[X][Y] = adj[Y][X] = min(adj[X][Y], Z)

for k in range(N):
    for i in range(N):
        for j in range(N):
            adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j])

def calc(indices):
    t = []
    for idx_i, i in enumerate(indices):
        for idx_j, j in enumerate(indices):
            if idx_i == idx_j:
                continue
            t.append((adj[i][j], idx_i, idx_j))
    t.sort(key=lambda t: t[0])
    uf = dsu(K)
    ret = 0
    for c, i, j in t:
        if not uf.same(i, j):
            uf.merge(i, j)
            ret += c
    return ret 


def popcnt(n):
    "https://atcoder.jp/contests/abc152/submissions/9619555"
    c = (n & 0x5555555555555555) + ((n >> 1) & 0x5555555555555555)
    c = (c & 0x3333333333333333) + ((c >> 2) & 0x3333333333333333)
    c = (c & 0x0f0f0f0f0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f0f0f0f0f)
    c = (c & 0x00ff00ff00ff00ff) + ((c >> 8) & 0x00ff00ff00ff00ff)
    c = (c & 0x0000ffff0000ffff) + ((c >> 16) & 0x0000ffff0000ffff)
    c = (c & 0x00000000ffffffff) + ((c >> 32) & 0x00000000ffffffff)
    return c


answer = INF
for s in range(1 << N):
    if popcnt(s) != K:
        continue
    indices = []
    tmp = 0
    for i in range(N):
        if s & (1 << i):
            indices.append(i)
            tmp += As[i]
    tmp += calc(indices)
    answer = min(answer, tmp)

print(answer)
0