Advertisement
huberemanuel

2666.py

Oct 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. class Node:
  2.   def __init__(self, w):
  3.     self.w = w
  4.     self.visited = False
  5.     self.dad = None
  6.  
  7. class Edge:
  8.   def __init__(self, to, w):
  9.     self.to = to
  10.     self.w = w
  11.  
  12. def dfs(origin, destiny, E, nodes):
  13.   path = []
  14.   E[origin].dad = None
  15.   stack = list()
  16.   stack.append(origin)
  17.   actual = 0
  18.   visiteds = []
  19.   while len(stack) > 0:
  20.     actual = stack[-1]
  21.     del stack[-1]
  22.     if actual == destiny:
  23.       break;
  24.     for edge in nodes[actual]:
  25.       if edge.to not in visiteds:
  26.         # print("Nó atual: " + str(actual) + " vizinho: " + str(edge.to))
  27.         visiteds.append(edge.to)
  28.         visiteds.append(actual)
  29.         stack.append(edge.to)
  30.         E[edge.to].dad = actual
  31.   while actual != None:
  32.     E[origin]
  33.     path.append(actual)
  34.     actual = E[actual].dad
  35.   return list(reversed(path))
  36.  
  37. N, C = [int(x) for x in input().split(" ")]
  38.  
  39. E = dict()
  40.  
  41. aux = [x for x in input().split(" ")]
  42.  
  43. for i in range(1, N + 1):
  44.     E[i] = Node(int(aux[i - 1]))
  45.  
  46. nodes = [[] for i in range(N + 1)]
  47.  
  48. for i in range(N - 1):
  49.     origin, destiny, v = [int(x) for x in input().split(" ")]
  50.     nodes[origin].append(Edge(destiny, v))
  51.     nodes[destiny].append(Edge(origin, v))
  52.  
  53. total = 0
  54.  
  55. for i in range(1, N + 1):
  56.   aux = 0
  57.   path = dfs(i, 1, E, nodes)
  58.   print("Nó atual: " + str(i) + " path: ")
  59.   print(path)
  60.   origin = path[0]
  61.   for j in range(len(path) - 1):
  62.     node = path[j]
  63.     if not E[node].visited and E[node].w + aux <= C:
  64.       aux += E[node].w
  65.       print("aux: " + str(aux))
  66.       E[node].visited = True
  67.     for e in nodes[origin]:
  68.       print("\tSou: " + str(node) + " indo para: " + str(e.to))
  69.       if e.to == path[j + 1]:
  70.         print("entrei")
  71.         origin = path[j + 1]
  72.         total += e.w
  73.  
  74. print(total * 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement