結果

問題 No.1690 Power Grid
ユーザー ansainansain
提出日時 2021-07-01 19:56:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 382 ms / 3,000 ms
コード長 3,556 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 79,104 KB
最終ジャッジ日時 2024-07-05 09:49:32
合計ジャッジ時間 4,727 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
60,800 KB
testcase_01 AC 47 ms
60,800 KB
testcase_02 AC 50 ms
60,928 KB
testcase_03 AC 49 ms
60,672 KB
testcase_04 AC 50 ms
60,544 KB
testcase_05 AC 51 ms
60,544 KB
testcase_06 AC 158 ms
77,440 KB
testcase_07 AC 152 ms
77,440 KB
testcase_08 AC 149 ms
77,184 KB
testcase_09 AC 148 ms
77,440 KB
testcase_10 AC 75 ms
73,856 KB
testcase_11 AC 54 ms
63,616 KB
testcase_12 AC 47 ms
61,312 KB
testcase_13 AC 84 ms
77,312 KB
testcase_14 AC 153 ms
78,208 KB
testcase_15 AC 56 ms
65,024 KB
testcase_16 AC 381 ms
78,052 KB
testcase_17 AC 254 ms
79,104 KB
testcase_18 AC 199 ms
78,508 KB
testcase_19 AC 382 ms
78,220 KB
testcase_20 AC 300 ms
78,504 KB
testcase_21 AC 59 ms
66,560 KB
testcase_22 AC 176 ms
78,208 KB
testcase_23 AC 65 ms
68,480 KB
testcase_24 AC 158 ms
78,204 KB
testcase_25 AC 113 ms
77,568 KB
testcase_26 AC 67 ms
69,888 KB
testcase_27 AC 48 ms
60,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict, Counter, deque
from itertools import permutations, combinations, product, combinations_with_replacement, groupby, accumulate
import operator
from math import sqrt, gcd, factorial
from copy import deepcopy
# from math import isqrt, prod,comb  # python3.8用(notpypy)
#from bisect import bisect_left,bisect_right
#from functools import lru_cache,reduce
#from heapq import heappush,heappop,heapify,heappushpop,heapreplace
#import numpy as np
#import networkx as nx
#from networkx.utils import UnionFind
#from numba import njit, b1, i1, i4, i8, f8
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, NegativeCycleError
# numba例 @njit(i1(i4[:], i8[:, :]),cache=True) 引数i4配列、i8 2次元配列,戻り値i1
def input(): return sys.stdin.readline().rstrip()
def divceil(n, k): return 1+(n-1)//k  # n/kの切り上げを返す
def yn(hantei, yes='Yes', no='No'): print(yes if hantei else no)

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group_num = 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

        self.group_num -= 1
        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 self.group_num

    def all_group_members(self):
        self.all_group_member = defaultdict(list)
        for i in range(self.n):
            self.all_group_member[self.find(i)].append(i)
        return self.all_group_member

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def main():
    mod = 10**9+7
    mod2 = 998244353
    n, m, k = map(int, input().split())
    A = list(map(int, input().split()))
    cost = [[float("inf")]*n for i in range(n)]
    for i in range(m):
        x, y, z = map(int, input().split())
        x -= 1
        y -= 1
        cost[x][y] = z
        cost[y][x] = z

    for i in range(n):
        cost[i][i] = 0

    for kk in range(n):
        for ii in range(n):
            for jj in range(n):
                cost[ii][jj] = min(cost[ii][jj], cost[ii][kk] + cost[kk][jj])
    edges = [[i, j, cost[i][j]]
             for i, j in product(range(n), repeat=2) if i < j]
    edges.sort(key=lambda x:x[2])

    ans=10**20
    for uses in combinations(range(n),k):
        tmpans=sum(A[use] for use in uses)
        u=set(uses)
        uf=UnionFind(n)
        cnt=0
        if cnt==k-1:
            ans=min(ans,tmpans)
            continue
        for x,y,z in edges:
            if x in u and y in u and not uf.same(x,y):
                uf.union(x,y)
                tmpans+=z
                cnt+=1
            if cnt==k-1:
                ans=min(ans,tmpans)
                break
    print(ans)
        

    
    

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