Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- def max_sci_score(travel, point):
- # build reward map
- reward = {p[0]: int(p[1]) for p in point}
- # build graph
- graph = defaultdict(list)
- for src, cost, dest in travel:
- graph[src].append([dest, int(cost)])
- # memoization
- memo = {}
- # perform dfs
- def dfs(node):
- """max score: reward - cost"""
- # base case: end point - no outward going node
- if node not in graph:
- return reward.get(node, 0)
- # use memoization
- if node in memo:
- return memo[node]
- best = -float("inf")
- for nbr, cost in graph[node]:
- candidate = dfs(nbr) - cost
- best = max(candidate, best)
- memo[node] = reward.get(node, 0) + best
- return memo[node]
- return dfs("start")
- travel = [["start","3","A"], ["A","4","B"], ["B","5","END1"]]
- point = [["A","5"], ["B","6"], ["END1","3"]]
- print(max_sci_score(travel, point))
Advertisement
Add Comment
Please, Sign In to add comment