結果

問題 No.748 yuki国のお財布事情
ユーザー 双六双六
提出日時 2020-08-04 23:24:18
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 530 ms / 2,000 ms
コード長 1,390 bytes
コンパイル時間 353 ms
コンパイル使用メモリ 86,868 KB
実行使用メモリ 99,400 KB
最終ジャッジ日時 2023-10-13 01:15:58
合計ジャッジ時間 9,072 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
71,716 KB
testcase_01 AC 89 ms
71,576 KB
testcase_02 AC 87 ms
71,716 KB
testcase_03 AC 91 ms
71,464 KB
testcase_04 AC 88 ms
71,760 KB
testcase_05 AC 89 ms
71,708 KB
testcase_06 AC 89 ms
71,396 KB
testcase_07 AC 88 ms
71,388 KB
testcase_08 AC 88 ms
71,684 KB
testcase_09 AC 90 ms
71,744 KB
testcase_10 AC 89 ms
71,328 KB
testcase_11 AC 89 ms
71,692 KB
testcase_12 AC 90 ms
71,412 KB
testcase_13 AC 135 ms
80,492 KB
testcase_14 AC 200 ms
82,464 KB
testcase_15 AC 151 ms
80,712 KB
testcase_16 AC 264 ms
85,728 KB
testcase_17 AC 412 ms
94,960 KB
testcase_18 AC 511 ms
97,436 KB
testcase_19 AC 439 ms
96,608 KB
testcase_20 AC 376 ms
93,628 KB
testcase_21 AC 488 ms
96,688 KB
testcase_22 AC 90 ms
71,588 KB
testcase_23 AC 91 ms
71,668 KB
testcase_24 AC 90 ms
71,900 KB
testcase_25 AC 314 ms
92,080 KB
testcase_26 AC 530 ms
99,400 KB
testcase_27 AC 474 ms
97,596 KB
testcase_28 AC 262 ms
89,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class UnionFind:
	def __init__(self, N):
		self.par = [i for i in range(N)]
		self.rank = [0] * N

	def find(self, x):
		if self.par[x] == x:
			return x
		else:
			self.par[x] = self.find(self.par[x])
			return self.par[x]

	def same_check(self, x, y):
		return self.find(x) == self.find(y)

	def union(self, x, y):
		x = self.find(x); y = self.find(y)
		if self.rank[x] < self.rank[y]:
			self.par[x] = y
		else:
			self.par[y] = x
			if self.rank[x] == self.rank[y]:
				self.rank[x] += 1

class Kruskal:
	def __init__(self, E, N, M, UF, cost):
		self.uf = UF
		self.cost = cost
		for w, u, v, itr in E:
			if self.uf.same_check(u, v) != True:
				self.uf.union(u, v)
				self.cost += w

	def mincost(self):
		return self.cost

#処理内容
def main():
	N, M, K = getlist()
	E = []
	totalcost = 0
	for i in range(M):
		a, b, c = getlist()
		a -= 1; b -= 1
		totalcost += c
		E.append((c, a, b, i))

	UF = UnionFind(N)
	cost = 0
	for i in range(K):
		e = int(input())
		e -= 1
		c, a, b, itr = E[e]
		UF.union(a, b)
		cost += c

	E.sort(key = lambda x:x[0])
	K = Kruskal(E, N, M, UF, cost)

	finalcost = K.mincost()
	print(totalcost - finalcost)

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