Maximum Path Quality of a Graph Problem
Description
LeetCode Problem 2065.
There is an undirected graph with n nodes numbered from 0 to n - 1 (inclusive). You are given a 0-indexed integer array values where values[i] is the value of the i^th node. You are also given a 0-indexed 2D integer array edges, where each edges[j] = [u_j, v_j, time_j] indicates that there is an undirected edge between the nodes u_j and v_j,and it takes time_j seconds to travel between the two nodes. Finally, you are given an integer maxTime.
A valid path in the graph is any path that starts at node 0, ends at node 0, and takes at most maxTime seconds to complete. You may visit the same node multiple times. The quality of a valid path is the sum of the values of the unique nodes visited in the path (each node's value is added at most once to the sum).
Return the maximum quality of a valid path.
Note: There are at most four edges connected to each node.
Example 1:

Input: values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49
Output: 75
Explanation:
One possible path is 0 -> 1 -> 0 -> 3 -> 0. The total time taken is 10 + 10 + 10 + 10 = 40 <= 49.
The nodes visited are 0, 1, and 3, giving a maximal path quality of 0 + 32 + 43 = 75.
Example 2:

Input: values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30
Output: 25
Explanation:
One possible path is 0 -> 3 -> 0. The total time taken is 10 + 10 = 20 <= 30.
The nodes visited are 0 and 3, giving a maximal path quality of 5 + 20 = 25.
Example 3:

Input: values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50
Output: 7
Explanation:
One possible path is 0 -> 1 -> 3 -> 1 -> 0. The total time taken is 10 + 13 + 13 + 10 = 46 <= 50.
The nodes visited are 0, 1, and 3, giving a maximal path quality of 1 + 2 + 4 = 7.
Constraints:
n == values.length1 <= n <= 10000 <= values[i] <= 10^80 <= edges.length <= 2000edges[j].length == 30 <= u_j< v_j <= n - 110 <= time_j, maxTime <= 100- All the pairs
[u_j, v_j]are unique. - There are at most four edges connected to each node.
- The graph may not be connected.
Solution
Approach: DFS
Every edge takes at least 10 time units and maxTime is at most 100, so a valid path uses at most 10 edges. Each node has at most four edges, so plain backtracking over all walks that start at node 0 is fast enough.
Build an adjacency list, then run dfs(u, cost, value), where cost is the time spent so far and value is the sum of values of the distinct nodes visited. A vis array marks visited nodes, with node 0 marked at the start.
- When we stand on node 0, record
valueas a candidate answer. - For each edge
(u, v, t)withcost + t <= maxTime: ifvwas already visited, continue tovwithout adding its value; otherwise mark it, continue withvalue + values[v], and unmark it on return.
The answer is the best value seen at node 0, starting from dfs(0, 0, values[0]).
Complexity: O(n + m + 4^(maxTime / min(time))) time, which is at most about 4^10 walks; O(n + m + maxTime / min(time)) space.
class Solution:
def maximalPathQuality(
self, values: List[int], edges: List[List[int]], maxTime: int
) -> int:
def dfs(u: int, cost: int, value: int):
if u == 0:
nonlocal ans
ans = max(ans, value)
for v, t in g[u]:
if cost + t <= maxTime:
if vis[v]:
dfs(v, cost + t, value)
else:
vis[v] = True
dfs(v, cost + t, value + values[v])
vis[v] = False
n = len(values)
g = [[] for _ in range(n)]
for u, v, t in edges:
g[u].append((v, t))
g[v].append((u, t))
vis = [False] * n
vis[0] = True
ans = 0
dfs(0, 0, values[0])
return ans