-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe107.lua
68 lines (61 loc) · 1.33 KB
/
e107.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function splitstr(line, sep)
local t={}
local i=1
for str in string.gmatch(line, "([^,]+)") do
t[i] = str
i = i + 1
end
return t
end
file = io.open("network.txt", "r")
io.input(file)
line = io.read()
network = {}
n = 0
row = 0
col = 0
total_weight = 0
while line ~= nil do
splitline = splitstr(line, ",")
col = 0
for e = 1, #splitline do
elem = splitline[e]
if elem ~= "-" then
if col > row then
network[n] = { tonumber(elem), row, col }
total_weight = total_weight + tonumber(elem)
n = n + 1
end
end
col = col + 1
end
row = row + 1
line = io.read()
end
-- Kruskal
union_find = {}
for n = 0, #network do
union_find[n] = n
end
function union(uf, a, b)
X = math.min(uf[a], uf[b])
Y = math.max(uf[a], uf[b])
for i = 0, #uf do
if uf[i] == Y then
uf[i] = X
end
end
end
function weightCompare(a, b)
return a[1] < b[1]
end
ans = 0
table.sort(network, weightCompare)
for e = 1, #network do
edge = network[e]
if union_find[edge[2]] ~= union_find[edge[3]] then
ans = ans + edge[1]
union(union_find, edge[2], edge[3])
end
end
print(total_weight - ans)