2
def maximalPathQuality(3
self, values: List[int], edges: List[List[int]], maxTime: int5
graph = defaultdict(list)8
graph[edge[0]].append((edge[1], edge[2]))9
graph[edge[1]].append((edge[0], edge[2]))12
q.append((0, 0, values[0], set([0])))17
currV, currTime, currPoints, currSet = q.popleft()19
# if vertex has been visited, and if the previousTime is20
# less or equal to current time but current points is lower?21
# then this path can't give us better quality so stop proceeding.22
prevTime, prevPoints = cache[currV]23
if prevTime <= currTime and prevPoints > currPoints:25
cache[currV] = (currTime, currPoints)26
# can't go over the maxTime limit27
if currTime > maxTime:29
# collect maxPoint only if current vertex is 031
maxPoint = max(maxPoint, currPoints)32
for neigh, neighTime in graph[currV]:33
newSet = currSet.copy()34
# collects quality only if not collected before35
if neigh not in currSet:37
newPoint = currPoints + values[neigh]40
q.append((neigh, currTime + neighTime, newPoint, newSet))