結果
| 問題 |
No.748 yuki国のお財布事情
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2019-07-18 22:46:23 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,473 bytes |
| コンパイル時間 | 143 ms |
| コンパイル使用メモリ | 5,248 KB |
| 実行使用メモリ | 24,704 KB |
| 最終ジャッジ日時 | 2024-12-25 22:42:48 |
| 合計ジャッジ時間 | 5,383 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 7 WA * 15 RE * 4 |
ソースコード
local function uf_initialize(n)
local parent = {}
for i = 1, n do parent[i] = i end
return parent
end
local function uf_findroot(idx, parent)
while parent[idx] ~= idx do
idx = parent[idx]
end
return idx
end
local function kruskal(parent, line, force)
-- line.c, line.i1, line.i2
table.sort(line, function(a, b) return a.c < b.c end)
local totalcost = 0
local linenum = #line
for i = 1, linenum do
local c, i1, i2 = line[i].c, line[i].i1, line[i].i2
local r1, r2 = uf_findroot(i1, parent), uf_findroot(i2, parent)
parent[i1], parent[i2] = r1, r2
if force or r1 ~= r2 then
parent[r2] = r1
parent[i2] = r1
totalcost = totalcost + c
end
end
return totalcost
end
local n, m, k = io.read("*n", "*n", "*n")
local a, b, c = {}, {}, {}
local allcost = 0
for i = 1, m do
a[i], b[i], c[i] = io.read("*n", "*n", "*n")
allcost = allcost + c[i]
end
local curpos = 1
local forceline, line = {}, {}
for i = 1, k do
local e = io.read("*n")
local tmp = {}
for j = curpos, e - 1 do
tmp.i1, tmp.i2, tmp.c = a[j], b[j], c[j]
table.insert(line, tmp)
end
tmp.i1, tmp.i2, tmp.c = a[e], b[e], c[e]
table.insert(forceline, tmp)
curpos = e + 1
end
for i = curpos, n do
local tmp = {}
tmp.i1, tmp.i2, tmp.c = a[i], b[i], c[i]
table.insert(line, tmp)
end
local parent = uf_initialize(n)
local cost = kruskal(parent, forceline, true)
cost = cost + kruskal(parent, line, false)
print(allcost - cost)