Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sqrt, log10
- import sys
- class Pipe:
- def __init__(self, p, s, n, l, f=None):
- self.p = p
- self.s = s
- self.f = f
- self.n = n
- self.l = l
- self.children = []
- def __str__(self):
- tabs = ""
- for i in range(1, self.l):
- tabs += "\t"
- s = tabs + self.n + "(" + str(10**self.getFlow()) + ")" + "\n"
- for child in self.children:
- s += str(child)
- return s
- def getFlow(self):
- if self.f != None:
- flow = self.f
- if self.s:
- flow = flow / 2
- return flow
- maxPipe = max(self.children, key=lambda pipe: pipe.getFlow() - pipe.p)
- flow = maxPipe.getFlow() - maxPipe.p
- if self.s:
- flow = flow / 2
- return flow
- def readInput(input):
- row_counter = 0
- root = Pipe(1, 0, '1', 1)
- level = {'1':1}
- pipes = { '1': root }
- for row in input:
- if row_counter == 0:
- nodes = int(row)
- elif row_counter == 1 and nodes == 1:
- print(row, end='')
- quit()
- elif row_counter == nodes:
- data = row.split()
- for i in range(0, len(data)):
- flow = int(data[i])
- if flow > 0:
- pipes[str(i+1)].f = log10(flow)
- else:
- data = row.split()
- from_node, to_node, p, s = data[0], data[1], log10(float(data[2]) / 100), int(data[3])
- if from_node in level:
- level[to_node] = level[from_node] + 1
- pipe = Pipe(p, s, to_node, level[to_node])
- if from_node in pipes:
- pipes[from_node].children.append(pipe)
- pipes[to_node] = pipe
- row_counter += 1
- print('{0:0.4f}'.format(10**root.getFlow()))
- input = []
- for i in sys.stdin:
- i.strip('\n')
- input.append(i)
- readInput(input)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement