Maximum Weighted K-Edge Path - Problem

Maximum Weighted K-Edge Path in DAG

You're an explorer navigating through a Directed Acyclic Graph (DAG) representing a network of cities connected by weighted roads. Your mission is to find the most valuable route that satisfies specific constraints.

Given:

  • A DAG with n nodes (cities) labeled from 0 to n-1
  • An array edges where edges[i] = [u_i, v_i, w_i] represents a directed road from city u_i to city v_i with value w_i
  • Two integers: k (exact number of roads to travel) and t (strict upper bound on total value)

Your Goal: Find the maximum possible sum of edge weights for any path that:

  1. Contains exactly k edges
  2. Has total weight strictly less than t

Return the maximum sum, or -1 if no valid path exists.

Example: If you need exactly 3 roads with total value < 100, and you find paths with values [85, 92, 78], return 92 as it's the highest valid sum.

Input & Output

example_1.py — Basic Path Finding
$ Input: n = 4, edges = [[0,1,5],[1,2,3],[2,3,2]], k = 2, t = 10
Output: 8
💡 Note: The path 0→1→2 uses exactly 2 edges with total weight 5+3=8, which is less than t=10. This is the maximum possible sum meeting both constraints.
example_2.py — No Valid Path
$ Input: n = 3, edges = [[0,1,10],[1,2,15]], k = 2, t = 20
Output: -1
💡 Note: The only 2-edge path 0→1→2 has weight 10+15=25, which is not less than t=20. No valid path exists.
example_3.py — Multiple Valid Paths
$ Input: n = 4, edges = [[0,1,3],[0,2,7],[1,3,4],[2,3,1]], k = 2, t = 10
Output: 8
💡 Note: Two valid paths exist: 0→1→3 (weight=7) and 0→2→3 (weight=8). Return 8 as it's the maximum.

Constraints

  • 2 ≤ n ≤ 1000 (number of nodes)
  • 0 ≤ edges.length ≤ 2000 (number of edges)
  • edges[i].length == 3 (each edge has source, destination, weight)
  • 0 ≤ ui, vi ≤ n - 1 (valid node indices)
  • 1 ≤ wi, t ≤ 104 (positive weights and threshold)
  • 1 ≤ k ≤ 1000 (path length constraint)
  • The graph is a Directed Acyclic Graph (DAG)

Visualization

Tap to expand
Maximum Weighted K-Edge Path INPUT Directed Acyclic Graph (DAG) 0 1 2 3 w=5 w=3 w=2 Parameters: n = 4 (nodes) k = 2 (edges needed) t = 10 (weight limit) edges = [[0,1,5], [1,2,3], [2,3,2]] ALGORITHM STEPS 1 Initialize DP Table dp[node][edges] = max weight 2 Process Each Edge Update reachable states 3 Track Max Weight Keep weight strictly < t 4 Find Answer Max from dp[*][k] where < t DP Table: dp[node][edges] edges: 0 1 2 node 0: 0 - - node 1: 0 5 - node 2: 0 3 8 node 3: 0 2 5 Path 0-->1-->2: weight = 5+3 = 8 FINAL RESULT Optimal Path Found: 0 +5 1 +3 2 Edges used: 2 (k=2) [OK] Total Weight: 5 + 3 = 8 8 < 10 (t) [OK] Output: 8 Key Insight: Use DP where dp[node][edges] stores the maximum weight to reach 'node' using exactly 'edges' edges. For each edge (u,v,w), update: dp[v][e+1] = max(dp[v][e+1], dp[u][e] + w) if new weight < t. Answer is max(dp[*][k]) across all nodes, or -1 if no valid path exists with exactly k edges. TutorialsPoint - Maximum Weighted K-Edge Path | Dynamic Programming Approach
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22
31.7K Views
Medium Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen