Daily DSA: Maximum Path Energy in a Directed Network (Medium)
Problem Description
You are managing an energy network represented as a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n - 1.
You are given:
- An integer
n(the number of nodes). - A 2D array
edgeswhereedges[i] = [u, v, cost]represents a directed edge from nodeuto nodevwith an energy traversal cost ofcost. - An array
gainsof sizen, wheregains[i]is the energy bonus added to your pool upon visiting nodei. - An integer
initialEnergy, your starting energy before entering node0.
When you start at node 0, you immediately collect its energy bonus: energy = initialEnergy + gains[0].
To traverse a directed edge u -> v with cost C:
- Your current energy at node
umust be at leastC(energy >= C). - Upon reaching node
v, your energy becomesenergy - C + gains[v].
Return the maximum possible energy you can possess upon reaching node n - 1. If node n - 1 is unreachable under these conditions, return -1.
Examples
Example 1:
Input: n = 4, edges = [[0,1,3],[0,2,2],[1,3,5],[2,3,1]], gains = [2,8,1,3], initialEnergy = 3
Output: 8
Explanation:
- Start at node 0: Energy = 3 + gains[0] = 3 + 2 = 5.
- Path 1 (0 -> 1 -> 3):
- Move 0 -> 1 (cost 3 <= 5): Energy = 5 - 3 + gains[1] = 2 + 8 = 10.
- Move 1 -> 3 (cost 5 <= 10): Energy = 10 - 5 + gains[3] = 5 + 3 = 8.
- Path 2 (0 -> 2 -> 3):
- Move 0 -> 2 (cost 2 <= 5): Energy = 5 - 2 + gains[2] = 3 + 1 = 4.
- Move 2 -> 3 (cost 1 <= 4): Energy = 4 - 1 + gains[3] = 3 + 3 = 6.
The maximum energy on reaching node 3 is max(8, 6) = 8.
Example 2:
Input: n = 3, edges = [[0,1,10]], gains = [1,2,3], initialEnergy = 5
Output: -1
Explanation:
- Start at node 0: Energy = 5 + 1 = 6.
- Edge 0 -> 1 requires cost 10, but available energy is only 6. Node 1 cannot be reached.
- Node 2 is not connected, so node 2 (n - 1) is unreachable. Return -1.
Constraints
1 <= n <= 10^50 <= edges.length <= 2 * 10^5edges[i] = [u, v, cost]where0 <= u, v < nandu != v1 <= cost <= 10^90 <= gains[i] <= 10^90 <= initialEnergy <= 10^9- The given graph is guaranteed to be a Directed Acyclic Graph (DAG).
Approach
Since the graph is a DAG (Directed Acyclic Graph), we can process nodes in Topological Order using Kahn’s Algorithm (BFS) to compute the maximum possible energy reaching each node.
-
Dynamic Programming State: Let
dp[u]be the maximum energy achievable at nodeuafter collectinggains[u]. Initializedparray with-1for all nodes exceptdp[0] = initialEnergy + gains[0]. - Topological Ordering:
- Compute the in-degrees of all nodes.
- Push all nodes with
in_degree == 0into a queue.
- State Transition:
- For each node
upopped from the queue:- If
dp[u] != -1(i.e., nodeuis reachable):- For each neighbor
vvia edge[u, v, cost]:- Check if current energy is sufficient:
dp[u] >= cost. - If valid, update
dp[v] = max(dp[v], dp[u] - cost + gains[v]).
- Check if current energy is sufficient:
- For each neighbor
- Decrement
in_degree[v]. Ifin_degree[v] == 0, pushvto the queue.
- If
- For each node
- Result:
Return
dp[n - 1]. If it remains-1, noden - 1is unreachable.
C++ Solution
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
class Solution {
public:
long long maxPathEnergy(int n, vector<vector<int>>& edges, vector<int>& gains, int initialEnergy) {
// Adjacency list storing pairs of {neighbor, cost}
vector<vector<pair<int, int>>> adj(n);
vector<int> inDegree(n, 0);
for (const auto& edge : edges) {
int u = edge[0];
int v = edge[1];
int cost = edge[2];
adj[u].push_back({v, cost});
inDegree[v]++;
}
// dp[i] stores maximum energy upon reaching node i (after adding gains[i])
vector<long long> dp(n, -1);
dp[0] = (long long)initialEnergy + gains[0];
queue<int> q;
for (int i = 0; i < n; ++i) {
if (inDegree[i] == 0) {
q.push(i);
}
}
while (!q.empty()) {
int u = q.front();
q.pop();
if (dp[u] != -1) {
for (const auto& [v, cost] : adj[u]) {
if (dp[u] >= cost) {
long long nextEnergy = dp[u] - cost + gains[v];
dp[v] = max(dp[v], nextEnergy);
}
}
}
for (const auto& [v, cost] : adj[u]) {
inDegree[v]--;
if (inDegree[v] == 0) {
q.push(v);
}
}
}
return dp[n - 1];
}
};
Complexity Analysis
- Time Complexity: $\mathcal{O}(V + E)$ where $V = n$ and $E = \text{edges.length}$. Each vertex and edge is processed a constant number of times during topological sorting.
- Space Complexity: $\mathcal{O}(V + E)$ to store the adjacency list, in-degree array, queue, and DP table.