結果

問題 No.1690 Power Grid
ユーザー ansainansain
提出日時 2021-06-22 12:27:43
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,483 bytes
コンパイル時間 1,086 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 81,228 KB
最終ジャッジ日時 2023-09-18 20:22:16
合計ジャッジ時間 7,478 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
76,488 KB
testcase_01 AC 111 ms
76,676 KB
testcase_02 AC 114 ms
76,404 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 115 ms
76,484 KB
testcase_06 AC 224 ms
79,848 KB
testcase_07 AC 214 ms
79,452 KB
testcase_08 AC 215 ms
79,496 KB
testcase_09 AC 217 ms
79,536 KB
testcase_10 AC 137 ms
78,712 KB
testcase_11 AC 118 ms
77,304 KB
testcase_12 AC 115 ms
76,356 KB
testcase_13 AC 144 ms
79,324 KB
testcase_14 AC 220 ms
79,976 KB
testcase_15 AC 122 ms
77,528 KB
testcase_16 AC 479 ms
80,900 KB
testcase_17 AC 339 ms
81,228 KB
testcase_18 AC 277 ms
81,188 KB
testcase_19 AC 456 ms
80,032 KB
testcase_20 AC 392 ms
80,992 KB
testcase_21 AC 125 ms
77,636 KB
testcase_22 AC 253 ms
80,536 KB
testcase_23 AC 130 ms
77,200 KB
testcase_24 AC 231 ms
80,596 KB
testcase_25 AC 175 ms
78,908 KB
testcase_26 AC 130 ms
78,008 KB
testcase_27 AC 113 ms
76,244 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