-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
42 lines (28 loc) · 785 Bytes
/
Copy pathtest.py
File metadata and controls
42 lines (28 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from collections import defaultdict
def solve(N, src, dest):
graph = defaultdict(list)
tx = defaultdict(int)
for i in range(N - 1):
c1, c2, goods, tax = dest[i]
graph[c1].append((-1 * goods, tax, c2))
tx[c2] = tax
route = []
def dfs(city):
route.append(city)
for n in sorted(graph[city]):
dfs(n[2])
route.append(city)
dfs(src)
total_tax = 0
for c in route[1:]:
total_tax += tx[c]
return route, total_tax
N = int(input())
cons = []
for _ in range(N-1):
l = input()
ls = l.split()
cons.append((ls[0], ls[1], int(ls[2]), int(ls[3])))
ans, t = solve(N, cons[0][0], cons)
print("-".join(ans))
print(t, end="")