結果
| 問題 |
No.748 yuki国のお財布事情
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-05 09:43:29 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 1,891 ms / 2,000 ms |
| コード長 | 1,117 bytes |
| コンパイル時間 | 133 ms |
| コンパイル使用メモリ | 12,928 KB |
| 実行使用メモリ | 84,356 KB |
| 最終ジャッジ日時 | 2024-10-15 16:31:56 |
| 合計ジャッジ時間 | 38,942 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
| 外部呼び出し有り |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
import sys
from collections import defaultdict
import numpy as np
from scipy.sparse.csgraph import connected_components, minimum_spanning_tree
from scipy.sparse import csr_matrix
input = sys.stdin.buffer.readline
MAX = 10 ** 10
N, M, K = map(int, input().split())
if not M: print(0);exit()
ABC = np.array([tuple(map(int, input().split())) for _ in range(M)], dtype=np.int64)
ABC[:, :2] -= 1
ans = ABC[:, 2].sum()
if K:
E = np.array([int(input()) - 1 for _ in range(K)])
frm = ABC[E, 0]
to = ABC[E, 1]
ans -= ABC[E, 2].sum()
connection = csr_matrix(([1] * K, (frm, to)), shape=(N, N))
compress_N, labels = connected_components(connection)
ABC[:, :2] = labels[ABC[:, :2]]
else:
compress_N = N
d = defaultdict(lambda: MAX)
for a, b, c in ABC:
if a == b:
continue
if a > b: a, b = b, a
d[(a, b)] = min(d[(a, b)], c)
length, frm, to = [], [], []
for (a, b), c in d.items():
length.append(c)
frm.append(a)
to.append(b)
matr = csr_matrix((length, (frm, to)), shape=(compress_N, compress_N))
T = minimum_spanning_tree(matr)
ans -= int(T.sum())
print(ans)