Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. from math import sqrt, log10
  2. import sys
  3.  
  4. class Pipe:
  5.     def __init__(self, p, s, n, l, f=None):
  6.         self.p = p
  7.         self.s = s
  8.         self.f = f
  9.         self.n = n
  10.         self.l = l
  11.         self.children = []
  12.  
  13.     def __str__(self):
  14.         tabs = ""
  15.  
  16.         for i in range(1, self.l):
  17.             tabs += "\t"
  18.  
  19.         s = tabs + self.n + "(" + str(10**self.getFlow()) + ")" + "\n"
  20.  
  21.         for child in self.children:
  22.             s += str(child)
  23.  
  24.         return s
  25.  
  26.     def getFlow(self):
  27.         if self.f != None:
  28.             flow = self.f
  29.             if self.s:
  30.                 flow = flow / 2
  31.             return flow
  32.  
  33.         maxPipe = max(self.children, key=lambda pipe: pipe.getFlow() - pipe.p)
  34.         flow = maxPipe.getFlow() - maxPipe.p
  35.         if self.s:
  36.             flow = flow / 2
  37.         return flow
  38.  
  39. def readInput(input):
  40.     row_counter = 0
  41.     root = Pipe(1, 0, '1', 1)
  42.     level = {'1':1}
  43.  
  44.     pipes = { '1': root }
  45.  
  46.     for row in input:
  47.         if row_counter == 0:
  48.             nodes = int(row)
  49.         elif row_counter == 1 and nodes == 1:
  50.             print(row, end='')
  51.             quit()
  52.         elif row_counter == nodes:
  53.             data = row.split()
  54.  
  55.             for i in range(0, len(data)):
  56.                 flow = int(data[i])
  57.  
  58.                 if flow > 0:
  59.                     pipes[str(i+1)].f = log10(flow)
  60.         else:
  61.             data = row.split()
  62.             from_node, to_node, p, s = data[0], data[1], log10(float(data[2]) / 100), int(data[3])
  63.  
  64.             if from_node in level:
  65.                 level[to_node] = level[from_node] + 1
  66.  
  67.             pipe = Pipe(p, s, to_node, level[to_node])
  68.  
  69.             if from_node in pipes:
  70.                 pipes[from_node].children.append(pipe)
  71.  
  72.             pipes[to_node] = pipe
  73.  
  74.         row_counter += 1
  75.  
  76.     print('{0:0.4f}'.format(10**root.getFlow()))
  77.  
  78. input = []
  79. for i in sys.stdin:
  80.     i.strip('\n')
  81.     input.append(i)
  82.  
  83. readInput(input)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement