結果
| 問題 |
No.748 yuki国のお財布事情
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-10-31 22:55:06 |
| 言語 | Ruby (3.4.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,020 bytes |
| コンパイル時間 | 857 ms |
| コンパイル使用メモリ | 7,680 KB |
| 実行使用メモリ | 45,476 KB |
| 最終ジャッジ日時 | 2024-11-19 13:43:27 |
| 合計ジャッジ時間 | 34,512 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 TLE * 9 |
コンパイルメッセージ
Syntax OK
ソースコード
# クラスカル 法で「最小全域木」を求める
N, M, K = gets.split.map(&:to_i)
loads = Array.new(M)
M.times{ |i|
loads[i] = gets.split.map(&:to_i)
}
K.times{
i = gets.to_i - 1
loads[i][2] = 0 # 必ず選択しないといけないloadはコスト0とする。
}
# コスト順にソート
loads = loads.sort_by{ |load| load[2]}
forest = []
NOT_FOUND = -1
ignored_cost = 0
for path in loads do
from, to, c = path
f_tree = NOT_FOUND
t_tree = NOT_FOUND
forest.size.times{ |i|
# from, toの属するtreeを探す
if forest[i].include?(from) then
f_tree = i
end
if forest[i].include?(to) then
t_tree = i
end
if f_tree != NOT_FOUND && t_tree != NOT_FOUND then
break
end
}
if f_tree != NOT_FOUND && t_tree == NOT_FOUND then # fromがあるtreeに属し、toがどのtreeにも属していない場合
# f_treeにtoを追加し、そのloadを採用する
forest[f_tree].push(to)
elsif t_tree != NOT_FOUND && f_tree == NOT_FOUND then # toがあるtreeに属し、fromがどのtreeにも属していない場合
# t_treeにfromを追加し、そのloadを採用する
forest[t_tree].push(from)
elsif t_tree != NOT_FOUND && f_tree != NOT_FOUND && t_tree != f_tree then # from, toどちらも異なるtreeに属している場合
# 2つのtreeを結合し、そのloadを採用する
from_tree = forest[f_tree]
to_tree = forest[t_tree]
forest = forest.select{ |t| t != to_tree && t != from_tree}.push(from_tree + to_tree)
elsif t_tree == NOT_FOUND && f_tree == NOT_FOUND then # from, toどちらもどのtreeにも属していない場合
# 新たなtreeとしてforestに追加し、そのloadを採用する
forest.push([from, to])
else
# from, toどちらも同じtreeに属する場合、そのloadは破棄する
ignored_cost += c
end
end
puts ignored_cost