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] = [ui, vi, wi] indicates a directed edge from node ui to vi with weight wi.
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 exactlyk edges.
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.
Input: n =3, edges =[[0,1,1],[1,2,2]], k =2, t =4Output: 3Explanation:
* 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`is3.
Input: n =3, edges =[[0,1,2],[0,2,3]], k =1, t =3Output: 2Explanation:
* 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`is2.
Input: n =3, edges =[[0,1,6],[1,2,8]], k =1, t =6Output: -1Explanation:
* 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.
Since the graph is a DAG, we can use dynamic programming to compute the maximum sum for each node and edge count. By processing nodes in topological order, we ensure all dependencies are resolved before updating the DP table.
classSolution {
public:int maximumWeightedKEdgePath(int n, vector<vector<int>>& edges, int k, int t) {
vector<vector<pair<int,int>>> g(n);
vector<int> indeg(n);
for (auto& e : edges) {
g[e[0]].emplace_back(e[1], e[2]);
indeg[e[1]]++;
}
vector<vector<int>> dp(n, vector<int>(k+1, -1));
for (int i =0; i < n; ++i) dp[i][0] =0;
queue<int> q;
for (int i =0; i < n; ++i) if (indeg[i] ==0) q.push(i);
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto& [v, w] : g[u]) {
for (int e =0; e < k; ++e) {
if (dp[u][e] !=-1&& dp[u][e] + w < t) {
dp[v][e+1] = max(dp[v][e+1], dp[u][e] + w);
}
}
if (--indeg[v] ==0) q.push(v);
}
}
int ans =-1;
for (int i =0; i < n; ++i) ans = max(ans, dp[i][k]);
return ans;
}
};
classSolution {
publicintmaximumWeightedKEdgePath(int n, int[][] edges, int k, int t) {
List<List<int[]>> g =new ArrayList<>();
for (int i = 0; i < n; ++i) g.add(new ArrayList<>());
int[] indeg =newint[n];
for (int[] e : edges) {
g.get(e[0]).add(newint[]{e[1], e[2]});
indeg[e[1]]++;
}
int[][] dp =newint[n][k+1];
for (int i = 0; i < n; ++i) Arrays.fill(dp[i], -1);
for (int i = 0; i < n; ++i) dp[i][0]= 0;
Queue<Integer> q =new LinkedList<>();
for (int i = 0; i < n; ++i) if (indeg[i]== 0) q.offer(i);
while (!q.isEmpty()) {
int u = q.poll();
for (int[] vw : g.get(u)) {
int v = vw[0], w = vw[1];
for (int e = 0; e < k; ++e) {
if (dp[u][e]!=-1 && dp[u][e]+ w < t) {
dp[v][e+1]= Math.max(dp[v][e+1], dp[u][e]+ w);
}
}
if (--indeg[v]== 0) q.offer(v);
}
}
int ans =-1;
for (int i = 0; i < n; ++i) ans = Math.max(ans, dp[i][k]);
return ans;
}
}
classSolution {
funmaximumWeightedKEdgePath(n: Int, edges: Array<IntArray>, k: Int, t: Int): Int {
val g = Array(n) { mutableListOf<Pair<Int,Int>>() }
val indeg = IntArray(n)
for (e in edges) {
g[e[0]].add(e[1] to e[2])
indeg[e[1]]++ }
val dp = Array(n) { IntArray(k+1) { -1 } }
for (i in0 until n) dp[i][0] = 0val q = ArrayDeque<Int>()
for (i in0 until n) if (indeg[i] ==0) q.add(i)
while (q.isNotEmpty()) {
val u = q.removeFirst()
for ((v, w) in g[u]) {
for (e in0 until k) {
if (dp[u][e] != -1&& dp[u][e] + w < t) {
dp[v][e+1] = maxOf(dp[v][e+1], dp[u][e] + w)
}
}
indeg[v]--if (indeg[v] ==0) q.add(v)
}
}
var ans = -1for (i in0 until n) ans = maxOf(ans, dp[i][k])
return ans
}
}
defmaximum_weighted_k_edge_path(n: int, edges: list[list[int]], k: int, t: int) -> int:
from collections import deque, defaultdict
g: dict[int, list[tuple[int,int]]] = defaultdict(list)
indeg = [0] * n
for u, v, w in edges:
g[u].append((v, w))
indeg[v] +=1 dp = [[-1]*(k+1) for _ in range(n)]
for i in range(n):
dp[i][0] =0 q = deque(i for i in range(n) if indeg[i] ==0)
while q:
u = q.popleft()
for v, w in g[u]:
for e in range(k):
if dp[u][e] !=-1and dp[u][e] + w < t:
dp[v][e+1] = max(dp[v][e+1], dp[u][e] + w)
indeg[v] -=1if indeg[v] ==0:
q.append(v)
ans = max(dp[i][k] for i in range(n))
return ans if ans !=-1else-1