結果
| 問題 |
No.1690 Power Grid
|
| コンテスト | |
| ユーザー |
ansain
|
| 提出日時 | 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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 23 WA * 2 |
ソースコード
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()
ansain