Maximum Path Quality of a Graph

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] = [uj, vj, timej] indicates that there is an undirected edge between nodes uj and vj, and it takes timej 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
Inputvalues = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49
Output75
One possible maximal path is 0 -> 1 -> 0 -> 3 -> 0, taking 40 seconds and visiting nodes 0, 1, and 3 for a quality of 75.
Example 2
Inputvalues = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30
Output25
One possible maximal path is 0 -> 3 -> 0, taking 20 seconds and visiting nodes 0 and 3 for a quality of 25.

Constraints

  • n == values.length
  • 1 <= n <= 1000
  • 0 <= values[i] <= 10^8
  • 0 <= edges.length <= 2000
  • edges[j].length == 3
  • 0 <= uj < vj <= n - 1
  • 10 <= timej, maxTime <= 100
  • All the pairs [uj, vj] are unique.
  • There are at most four edges connected to each node.
  • The graph may not be connected.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate