結果

問題 No.1690 Power Grid
ユーザー mymelochanmymelochan
提出日時 2021-09-25 02:17:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,841 bytes
コンパイル時間 309 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 78,032 KB
最終ジャッジ日時 2023-09-18 22:46:04
合計ジャッジ時間 4,527 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,308 KB
testcase_01 AC 76 ms
71,080 KB
testcase_02 AC 77 ms
71,432 KB
testcase_03 AC 75 ms
71,468 KB
testcase_04 AC 78 ms
71,440 KB
testcase_05 AC 78 ms
71,404 KB
testcase_06 AC 147 ms
77,300 KB
testcase_07 AC 144 ms
77,392 KB
testcase_08 AC 148 ms
77,456 KB
testcase_09 AC 148 ms
77,308 KB
testcase_10 AC 96 ms
76,516 KB
testcase_11 AC 87 ms
75,992 KB
testcase_12 AC 77 ms
71,400 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 86 ms
76,416 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class dsu:

    def __init__(self,n):
        self.N = n
        self.cnt=[1]*self.N
        self.root=list(range(self.N))
        self.components = self.N

    def unite(self,x,y):
        x=self.leader(x)
        y=self.leader(y)
        if x!=y:
            self.components -= 1
            if self.cnt[x]<self.cnt[y]:
                x,y=y,x
            self.cnt[x]+=self.cnt[y]
            self.root[y]=x
            return x
        return None

    def leader(self,x):
        if self.root[x]==x:
            return x
        self.root[x]=self.leader(self.root[x])
        return self.root[x]

    def count_components(self):
        return self.components

N,M,K = map(int,input().split())
A = list(map(int,input().split()))
G = [[] for _ in range(N)]

for _ in range(M):
    x,y,z = map(int,input().split())
    x,y = x-1,y-1
    G[x].append((y,z))
    G[y].append((x,z))

def wf(G):
    N = len(G)
    INF = 10**20
    cost = [[INF]*N for _ in range(N)]
    for i in range(N):
        cost[i][i] = 0
    for s in range(N):
        for t,c in G[s]:
            cost[s][t] = c
            cost[t][s] = c
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if cost[i][k]!=INF and cost[k][j]!=INF:
                    cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
    return cost

cost = wf(G)
E = []
for i in range(N):
    for j in range(i+1,N):
        E.append((i,j,cost[i][j]))

from itertools import combinations

mi = 10**20
for V in combinations(range(N),K):
    tmp = 0
    f = [0]*N
    for v in V:
        tmp += A[v]
        f[v] = 1
    uft = dsu(N)
    cnt = K
    for x,y,z in E:
        if f[x] and f[y]:
            if not uft.unite(x,y) is None:
                cnt -= 1
                tmp += z
        if cnt == 1:
            break
    mi = min(mi,tmp)
print(mi)
0