Tap here to switch tabs
Problem
Submissions

Problem: Maximum Weighted K-Edge Path

med
30 min
Try to solve the Maximum Weighted K-Edge Path problem.

Statement

You are given an integer n representing the number of vertices in a directed acyclic graph, labeled from 00 to n −1- 1. You are also given a 22D integer array edges, where edges[i] = [u_i, v_i, w_i] denotes a directed edge from vertex u_i to vertex v_i with weight w_i.

Additionally, you are given two integers k and t.

Find the maximum possible sum of edge weights for any path in the graph that contains exactly k edges such that the total sum of weights is strictly less than t.

Return the maximum sum of weights among all valid paths. If no such path exists, return $-1$.

Constraints:

  • 1≤1 \leq n ≤300\leq 300

  • 0≤0 \leq edges.length ≤300\leq 300

  • edges[i] = [u_i, v_i, w_i]

  • 0≤0 \leq u_i, v_i << n

  • u_i ≠\neq v_i

  • 1≤1 \leq w_i ≤10\leq 10

  • 0≤0 \leq k ≤300\leq 300

  • 1≤1 \leq t ≤600\leq 600

  • The input graph is guaranteed to be a DAG.

  • There are no duplicate edges.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Weighted K-Edge Path

med
30 min
Try to solve the Maximum Weighted K-Edge Path problem.

Statement

You are given an integer n representing the number of vertices in a directed acyclic graph, labeled from 00 to n −1- 1. You are also given a 22D integer array edges, where edges[i] = [u_i, v_i, w_i] denotes a directed edge from vertex u_i to vertex v_i with weight w_i.

Additionally, you are given two integers k and t.

Find the maximum possible sum of edge weights for any path in the graph that contains exactly k edges such that the total sum of weights is strictly less than t.

Return the maximum sum of weights among all valid paths. If no such path exists, return $-1$.

Constraints:

  • 1≤1 \leq n ≤300\leq 300

  • 0≤0 \leq edges.length ≤300\leq 300

  • edges[i] = [u_i, v_i, w_i]

  • 0≤0 \leq u_i, v_i << n

  • u_i ≠\neq v_i

  • 1≤1 \leq w_i ≤10\leq 10

  • 0≤0 \leq k ≤300\leq 300

  • 1≤1 \leq t ≤600\leq 600

  • The input graph is guaranteed to be a DAG.

  • There are no duplicate edges.