def get_file(fn): with open(fn, 'r') as f: network = list(map(lambda x: list(map(lambda n: int(n), x.strip().replace('-', '0').split(','))), f.readlines())) return network, sum(map(lambda x: sum(x), network)) // 2 def remake(network, total): p_todo = list(range(1, len(network))) p_tree = [0] trace = [] while len(p_todo): least = (total, 0) for pick in p_todo: for in_tree in p_tree: dist = network[pick][in_tree] if not dist: continue if dist < least[0]: least = (dist, pick) if least[0] < total: trace.append(least[0]) p_tree.append(least[1]) p_todo.remove(least[1]) return sum(trace) def calc(fn): network, total = get_file(fn) return total - remake(network, total) print(calc('../resource/network.txt'))