From: Skullheadx <704277@pdsb.net> Date: Thu, 10 Nov 2022 03:24:49 +0000 (-0500) Subject: 2010 S4 wip X-Git-Url: http://git.skullheadx.com/nixos/static/about.html?a=commitdiff_plain;h=cb50130b95669ae0dabfa60c53fa3a50fbe65f69;p=CCC.git 2010 S4 wip --- diff --git a/Main/Python/2010/S4.py b/Main/Python/2010/S4.py new file mode 100644 index 0000000..20bc12a --- /dev/null +++ b/Main/Python/2010/S4.py @@ -0,0 +1,116 @@ +from queue import Queue, LifoQueue + +fences = dict() +tree = dict() + +M = int(input()) # num pens +for i in range(M): + description = tuple(map(int,input().split())) + edges = description[0] + corners = [] + for j in range(edges): + corners.append(description[j+1]) + costs = [] + for j in range(edges): + costs.append(description[j+1+edges]) + for j,k in enumerate(corners): + if j == len(corners) -1: + next_corner = corners[0] + else: + next_corner = corners[j+1] + + fences[(k, next_corner)] = costs[j] + if k in tree: + tree[k].add(next_corner) + else: + tree[k] = {next_corner} + if next_corner in tree: + tree[next_corner].add(k) + else: + tree[next_corner] = {k} + +print(fences) +print(tree) + +def deep_copy(dictionary): + out = dict() + for key,ele in zip(dictionary.keys(),dictionary.values()): + out[key] = ele.copy() + return out + +q = Queue() +for i in fences: + q.put(([i], tree)) + + + + +while not q.empty(): + # current is list of fences removed, pen is the resulting tree + current, pen = q.get() + # check if this is solution + # if we form a loop when dfs then that is closed. + # we want maximum 1 (all animals in same pen) + # or 0 where animals break out then all must be 0 loops + is_solution = True + broken_out = False + node_loops = dict() + for i in pen.keys(): + loops = -1 + q2 = LifoQueue() + q2.put((i,set())) + global_seen = set() + + while not q2.empty(): + c,seen = q2.get() + if c == i: + loops += 1 + if loops > 1: + is_solution = False + break + + if c in seen: + continue + else: + seen.add(c) + global_seen.add(c) + + for j in pen[c]: + q2.put((j,seen.copy())) + # print(c,j,seen, loops) + + + if loops == 0: + broken_out = True + + if loops <= 1 and len(global_seen) == len(pen.keys()): # make sure there aren't isolated loops + node_loops[i] = loops + + if broken_out: + for i in node_loops.values(): + if i != 0: + is_solution = False + + if is_solution: + output = 0 + for i in current: + output += fences[i] + print(output, current, node_loops) + quit() + + + + + + + + + # find next + for fence in fences: + if fence not in current and (fence[1],fence[0]) not in current: + new_pen = deep_copy(pen) + new_pen[fence[0]].remove(fence[1]) + new_pen[fence[1]].remove(fence[0]) + new_current = current[:] + new_current.append(fence) + q.put((new_current, new_pen)) diff --git a/Main/test.py b/Main/test.py index 95b58d3..982de39 100644 --- a/Main/test.py +++ b/Main/test.py @@ -1,18 +1,36 @@ -test = {"A":'a', "B":'b'} +# test = {"A":'a', "B":'b'} +# +# if "A" in test: +# print("1") +# +# if "a" in test: +# print("2") +# +# test['C'] = 'c' +# +# test[(0,0)] = 'd' +# +# print(test) +# +# test["A"] = 'L' +# +# for i in test.values(): +# print(i) +# +# print("--------------------") +# +# a = [1,2,3] +# print(a[:-1]) -if "A" in test: - print("1") +test = {"A":{1,2,3}, 1:[1,1]} -if "a" in test: - print("2") +test["A"].remove(1) -test['C'] = 'c' -test[(0,0)] = 'd' +test2 = test.copy() -print(test) - -test["A"] = 'L' +test2["B"] = 1 +test2[1].append(2) -for i in test.values(): - print(i) +print(test) +print(test2)