結果

問題 No.1690 Power Grid
ユーザー ansainansain
提出日時 2021-06-22 12:27:43
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,483 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 79,052 KB
最終ジャッジ日時 2024-07-05 09:48:13
合計ジャッジ時間 5,095 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
60,732 KB
testcase_01 AC 52 ms
61,272 KB
testcase_02 AC 52 ms
60,908 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 50 ms
60,976 KB
testcase_06 AC 169 ms
77,340 KB
testcase_07 AC 161 ms
77,568 KB
testcase_08 AC 160 ms
77,296 KB
testcase_09 AC 168 ms
77,568 KB
testcase_10 AC 84 ms
73,984 KB
testcase_11 AC 57 ms
63,872 KB
testcase_12 AC 51 ms
61,144 KB
testcase_13 AC 91 ms
76,928 KB
testcase_14 AC 164 ms
78,288 KB
testcase_15 AC 57 ms
64,708 KB
testcase_16 AC 418 ms
78,336 KB
testcase_17 AC 279 ms
78,972 KB
testcase_18 AC 217 ms
78,768 KB
testcase_19 AC 403 ms
78,604 KB
testcase_20 AC 328 ms
79,052 KB
testcase_21 AC 65 ms
66,560 KB
testcase_22 AC 188 ms
77,876 KB
testcase_23 AC 70 ms
68,276 KB
testcase_24 AC 169 ms
78,344 KB
testcase_25 AC 123 ms
77,176 KB
testcase_26 AC 69 ms
69,508 KB
testcase_27 AC 50 ms
61,056 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
        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