Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, w):
- self.w = w
- self.visited = False
- self.dad = None
- class Edge:
- def __init__(self, to, w):
- self.to = to
- self.w = w
- def dfs(origin, destiny, E, nodes):
- path = []
- E[origin].dad = None
- stack = list()
- stack.append(origin)
- actual = 0
- visiteds = []
- while len(stack) > 0:
- actual = stack[-1]
- del stack[-1]
- if actual == destiny:
- break;
- for edge in nodes[actual]:
- if edge.to not in visiteds:
- # print("Nó atual: " + str(actual) + " vizinho: " + str(edge.to))
- visiteds.append(edge.to)
- visiteds.append(actual)
- stack.append(edge.to)
- E[edge.to].dad = actual
- while actual != None:
- E[origin]
- path.append(actual)
- actual = E[actual].dad
- return list(reversed(path))
- N, C = [int(x) for x in input().split(" ")]
- E = dict()
- aux = [x for x in input().split(" ")]
- for i in range(1, N + 1):
- E[i] = Node(int(aux[i - 1]))
- nodes = [[] for i in range(N + 1)]
- for i in range(N - 1):
- origin, destiny, v = [int(x) for x in input().split(" ")]
- nodes[origin].append(Edge(destiny, v))
- nodes[destiny].append(Edge(origin, v))
- total = 0
- for i in range(1, N + 1):
- aux = 0
- path = dfs(i, 1, E, nodes)
- print("Nó atual: " + str(i) + " path: ")
- print(path)
- origin = path[0]
- for j in range(len(path) - 1):
- node = path[j]
- if not E[node].visited and E[node].w + aux <= C:
- aux += E[node].w
- print("aux: " + str(aux))
- E[node].visited = True
- for e in nodes[origin]:
- print("\tSou: " + str(node) + " indo para: " + str(e.to))
- if e.to == path[j + 1]:
- print("entrei")
- origin = path[j + 1]
- total += e.w
- print(total * 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement