The network that cannot be joined
transcript
Connect every zone at the least total cost. Sort the lanes by cost. Take a lane if it joins two groups. Return the total at the end. Done?
Three zones. One lane, between zones zero and one. The cost of that lane is five. It is the only lane there is. The function returns five. That is wrong.
Zones zero and one are one group. Zone two stands alone. No lane touches it. Two groups remain, not one. So the network cannot be connected.
No set of lanes can connect them. The answer is minus one. Five is the cost of a partial network. Check the group count.
Start with one group per zone. Subtract one for each join you take. At the end, one group means success. Otherwise, report minus one.
A total alone proves nothing. Count the groups at the end. Then say minus one when needed. Full breakdown in the lecture.