Minimum Time to Reach Target With Limited Power Problem
Description
LeetCode Problem 3977.
You are given a directed weighted graph with n nodes labeled from 0 to n - 1.
The graph is represented by a 2D integer array edges, where edges[i] = [u_i, v_i, t_i] indicates a directed edge from node u_i to node v_i that takes t_i seconds to traverse.
You are also given an integer power representing the initial available power, and an integer array cost of length n, where cost[u] represents the power required to forward the signal from node u through any one of its outgoing edges.
You are given two integers source and target.
The signal starts at source at time 0 with power units of power and follows these rules:
- The signal may traverse a directed edge from node
uonly if the remaining power is at leastcost[u]. - No power is consumed when the signal arrives at a node, unless it later leaves that node by traversing another edge.
- When the signal is forwarded from node
u, the remaining power is decreased bycost[u]units. - Traversing an edge
edges[i] = [u_i, v_i, t_i]increases the total time byt_iseconds.
Return an integer array answer of size 2, where:
answer[0]is the minimum time required for the signal to reach nodetarget.answer[1]is the maximum remaining power among all paths that achieveanswer[0].
If the signal cannot reach target, return [-1, -1].
Example 1:

Input: n = 5, edges = [[0,1,1],[1,4,1],[0,2,1],[2,3,1],[3,4,1]], power = 4, cost = [2,3,1,1,1], source = 0, target = 4
Output: [3,0]
Explanation:
- The signal starts at node 0 with 4 units of power.
- The path 0 -> 1 -> 4 is not valid, because after leaving node 0, the signal has 2 units of power remaining, which is less than cost[1] = 3.
- The valid path 0 -> 2 -> 3 -> 4 takes a total time of 3.
- The total power consumed along this path is cost[0] + cost[2] + cost[3] = 4, leaving 0 remaining power.
- Hence, the answer is [3, 0].
Example 2:

Input: n = 3, edges = [[0,1,2],[1,2,2],[2,0,2]], power = 3, cost = [1,1,1], source = 1, target = 1
Output: [0,3]
Explanation:
- Since the source and target are the same node, no traversal is required.
- Hence, the minimum total time taken is 0, and no power is consumed.
- Therefore, the answer is [0, 3].
Example 3:

Input: n = 4, edges = [[0,1,3],[2,3,4]], power = 3, cost = [1,1,1,1], source = 0, target = 3
Output: [-1,-1]
Explanation:
There is no valid path from source to target, therefore return [-1, -1].
Constraints:
1 <= n <= 10000 <= edges.length <= 1000edges[i] = [u_i, v_i, t_i]0 <= u_i, v_i <= n - 11 <= t_i <= 10^91 <= power <= 1000cost.length == n1 <= cost[i] <= 20000 <= source, target <= n - 1
Solution
Approach: Dijkstra on (node, remaining power)
The cheapest route in time may not be the one that keeps the most power, and a route may only continue while power lasts. So a node alone is not enough: the state is (u, p), the signal is at node u and has p units of power left. dist[u][p] stores the least time found to reach that state, and the start is dist[source][power] = 0.
From (u, p), the signal can leave u only if p >= cost[u]. After paying, it has p - cost[u] power, and each outgoing edge (u, v, t) leads to the state (v, p - cost[u]) at time d + t. Edge times are positive, so Dijkstra's algorithm with a min-heap works.
The order of the heap decides the answer: compare by time first, and among equal times prefer more remaining power (push -p and compare it as the second key). Then the first state with u = target that we pop has the minimum time and, among all paths with that time, the maximum remaining power. If the heap runs out first, the target is unreachable and the answer is [-1, -1]. If source == target, the very first state is popped immediately and gives [0, power].
Complexity: O((n + m) * power * log(n * power)) time and O(n * power) space, where m is the number of edges.
from heapq import heappop, heappush
from math import inf
class Solution:
def minTimeMaxPower(
self,
n: int,
edges: List[List[int]],
power: int,
cost: List[int],
source: int,
target: int,
) -> List[int]:
g = [[] for _ in range(n)]
for u, v, t in edges:
g[u].append((v, t))
dist = [[inf] * (power + 1) for _ in range(n)]
dist[source][power] = 0
pq = [(0, -power, source)]
while pq:
d, p, u = heappop(pq)
p = -p
if u == target:
return [d, p]
if d > dist[u][p] or p < cost[u]:
continue
p -= cost[u]
for v, t in g[u]:
nd = d + t
if nd < dist[v][p]:
dist[v][p] = nd
heappush(pq, (nd, -p, v))
return [-1, -1]