Maximum Weighted K-Edge Path Problem
Description
LeetCode Problem 3543.
You are given an integer n and a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n - 1. This is represented by a 2D array edges, where edges[i] = [u_i, v_i, w_i] indicates a directed edge from node u_i to v_i with weight w_i.
You are also given two integers, k and t.
Your task is to determine the maximum possible sum of edge weights for any path in the graph such that:
- The path contains exactly
kedges. - The total sum of edge weights in the path is strictly less than
t.
Return the maximum possible sum of weights for such a path. If no such path exists, return -1.
Example 1:
Input: n = 3, edges = [[0,1,1],[1,2,2]], k = 2, t = 4
Output: 3
Explanation:

- The only path with k = 2 edges is 0 -> 1 -> 2 with weight 1 + 2 = 3 < t.
- Thus, the maximum possible sum of weights less than t is 3.
Example 2:
Input: n = 3, edges = [[0,1,2],[0,2,3]], k = 1, t = 3
Output: 2
Explanation:

- There are two paths with k = 1 edge:
- 0 -> 1 with weight 2 < t.
- 0 -> 2 with weight 3 = t, which is not strictly less than t.
- Thus, the maximum possible sum of weights less than t is 2.
Example 3:
Input: n = 3, edges = [[0,1,6],[1,2,8]], k = 1, t = 6
Output: -1
Explanation:

- There are two paths with k = 1 edge:
- 0 -> 1 with weight 6 = t, which is not strictly less than t.
- 1 -> 2 with weight 8 > t, which is not strictly less than t.
- Since there is no path with sum of weights strictly less than t, the answer is -1.
Constraints:
1 <= n <= 3000 <= edges.length <= 300edges[i] = [u_i, v_i, w_i]0 <= u_i, v_i < nu_i != v_i1 <= w_i <= 100 <= k <= 3001 <= t <= 600- The input graph is guaranteed to be a DAG.
- There are no duplicate edges.
Solution
Approach: DP over Edge Count and Weight Sum
The weights are at most 10 and t is at most 600, so we can track every achievable weight sum below t exactly.
Let cur[v][s] say whether some path with j edges ends at vertex v with total weight s. For j = 0 every vertex has the single sum 0. To go from j to j + 1 edges, look at each edge u -> v with weight w and set next[v][s + w] for every s with cur[u][s] set and s + w < t. Sums of t or more are dropped immediately, as they can never become valid.
After k rounds the answer is the largest s set on any vertex, or -1 if none is. (The Python solution stores each row cur[v] as one integer bitmask, so a round is a shift and an OR per edge.) The graph being acyclic is not even needed: paths are counted by edge number, not by visited vertices.
Complexity: O(k * m * t) time, where m is the number of edges, and O(n * t) extra space.
class Solution:
def maxWeight(self, n: int, edges: List[List[int]], k: int, t: int) -> int:
mask = (1 << t) - 1 # bit s is set when sum s (< t) is reachable
cur = [1] * n # paths with 0 edges end anywhere with sum 0
for _ in range(k):
nxt = [0] * n
for u, v, w in edges:
nxt[v] |= (cur[u] << w) & mask
cur = nxt
all_sums = 0
for x in cur:
all_sums |= x
return all_sums.bit_length() - 1 if all_sums else -1