結果
| 問題 |
No.748 yuki国のお財布事情
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 20:22:13 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 440 ms / 2,000 ms |
| コード長 | 1,832 bytes |
| コンパイル時間 | 374 ms |
| コンパイル使用メモリ | 82,352 KB |
| 実行使用メモリ | 128,204 KB |
| 最終ジャッジ日時 | 2025-03-20 20:24:25 |
| 合計ジャッジ時間 | 5,709 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
import sys
def main():
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr]); ptr += 1
M = int(input[ptr]); ptr += 1
K = int(input[ptr]); ptr += 1
edges = []
total_cost = 0
for _ in range(M):
a = int(input[ptr]) - 1; ptr += 1
b = int(input[ptr]) - 1; ptr += 1
c = int(input[ptr]); ptr += 1
edges.append((a, b, c))
total_cost += c
specified = set()
for _ in range(K):
e = int(input[ptr]) - 1; ptr += 1
specified.add(e)
# Union-Find setup
parent = list(range(N))
rank = [1] * N
def find(u):
while parent[u] != u:
parent[u] = parent[parent[u]]
u = parent[u]
return u
def union(u, v):
u_root = find(u)
v_root = find(v)
if u_root == v_root:
return False
if rank[u_root] < rank[v_root]:
parent[u_root] = v_root
else:
parent[v_root] = u_root
if rank[u_root] == rank[v_root]:
rank[u_root] += 1
return True
sum_kept = 0
# Process specified edges
for e_idx in specified:
a, b, c = edges[e_idx]
sum_kept += c
# Perform union without checking, to build the connected components
union(a, b)
# Collect remaining edges
remaining = []
for i in range(M):
if i not in specified:
a, b, c = edges[i]
remaining.append((c, a, b))
# Sort by cost ascending
remaining.sort()
# Process remaining edges in Kruskal's manner
for c, a, b in remaining:
if find(a) != find(b):
if union(a, b):
sum_kept += c
# The result is total_cost minus the sum of kept edges
print(total_cost - sum_kept)
if __name__ == "__main__":
main()
lam6er