結果
問題 | No.748 yuki国のお財布事情 |
ユーザー |
👑 |
提出日時 | 2019-07-18 22:58:36 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 323 ms / 2,000 ms |
コード長 | 1,564 bytes |
コンパイル時間 | 230 ms |
コンパイル使用メモリ | 5,376 KB |
実行使用メモリ | 25,216 KB |
最終ジャッジ日時 | 2024-12-25 22:43:07 |
合計ジャッジ時間 | 3,913 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
local function uf_initialize(n)local parent = {}for i = 1, n do parent[i] = i endreturn parentendlocal function uf_findroot(idx, parent)while parent[idx] ~= idx doidx = parent[idx]endreturn idxendlocal function kruskal(parent, line, force)-- line.c, line.i1, line.i2table.sort(line, function(a, b) return a.c < b.c end)local totalcost = 0local linenum = #linefor i = 1, linenum dolocal c, i1, i2 = line[i].c, line[i].i1, line[i].i2local r1, r2 = uf_findroot(i1, parent), uf_findroot(i2, parent)parent[i1], parent[i2] = r1, r2if force or r1 ~= r2 thenparent[r2] = r1parent[i2] = r1totalcost = totalcost + cendendreturn totalcostendlocal n, m, k = io.read("*n", "*n", "*n")local a, b, c = {}, {}, {}local allcost = 0for i = 1, m doa[i], b[i], c[i] = io.read("*n", "*n", "*n")allcost = allcost + c[i]endlocal curpos = 1local forceline, line = {}, {}for i = 1, k dolocal e = io.read("*n")for j = curpos, e - 1 dolocal tmp = {}tmp.i1, tmp.i2, tmp.c = a[j], b[j], c[j]table.insert(line, tmp)endlocal tmp2 = {}tmp2.i1, tmp2.i2, tmp2.c = a[e], b[e], c[e]table.insert(forceline, tmp2)curpos = e + 1endfor i = curpos, m dolocal tmp = {}tmp.i1, tmp.i2, tmp.c = a[i], b[i], c[i]table.insert(line, tmp)endlocal parent = uf_initialize(n)local cost = 0if 0 < #forceline thencost = cost + kruskal(parent, forceline, true)endif 0 < #line thencost = cost + kruskal(parent, line, false)endprint(allcost - cost)