🔗 🔴 3977. Minimum Time to Reach Target With Limited Power

Problem Statement

題目簡述

給定一張 nn 個節點(編號 00 到 n−1n - 1)的有向帶權圖,邊 [ui,vi,ti][u_i, v_i, t_i] 表示從 uiu_i 走到 viv_i 需要 tit_i 秒。訊號在時間 00 從 source\text{source} 出發,初始電量為 power\text{power}。

從節點 uu 沿任一出邊轉發訊號時,剩餘電量必須至少為 cost[u]\text{cost}[u],轉發後剩餘電量會減少 cost[u]\text{cost}[u];抵達節點本身不消耗電量(除非後續再離開該節點)。

請回傳 [最短抵達時間, 所有最短時間路徑中的最大剩餘電量][\text{最短抵達時間},\ \text{所有最短時間路徑中的最大剩餘電量}]。若無法抵達 target\text{target},回傳 [−1,−1][-1, -1]。

Constraints

約束條件

  • 1≤n≤10001 \le n \le 1000
  • 0≤edges.length≤10000 \le \text{edges.length} \le 1000
  • edges[i]=[ui,vi,ti]\text{edges}[i] = [u_i, v_i, t_i]
  • 0≤ui,vi≤n−10 \le u_i, v_i \le n - 1
  • 1≤ti≤1091 \le t_i \le 10^9
  • 1≤power≤10001 \le \text{power} \le 1000
  • cost.length=n\text{cost.length} = n
  • 1≤cost[i]≤20001 \le \text{cost}[i] \le 2000
  • 0≤source,target≤n−10 \le \text{source}, \text{target} \le n - 1

思路:分層圖 + Dijkstra

一般最短路只記錄「到達節點 uu 的最短時間」,但這題有兩點限制:耗時較短的路徑可能耗電較多,導致後續無力轉發;而在最短時間相同時,還得挑剩餘電量最多的路徑。觀察約束條件,power\text{power} 上限只有 10001000,因此可以把「累計消耗電量」併入狀態,將原圖展開成分層圖。

定義 dist(u,e)\textit{dist}(u, e) 為從 source\text{source} 出發、累計消耗 ee 單位電量到達節點 uu 的最短時間(0≤e≤power0 \le e \le \text{power})。初始時 dist(source,0)=0\textit{dist}(\text{source}, 0) = 0,其餘狀態皆為 ∞\infty。

從節點 uu 沿任一出邊 (u,v,w)(u, v, w)(耗時 ww 秒)轉發訊號時,都會固定扣除 cost[u]\text{cost}[u] 的電量。只要滿足 e+cost[u]≤powere + \text{cost}[u] \le \text{power},就能從 (u,e)(u, e) 轉移到 (v, e+cost[u])(v,\ e + \text{cost}[u]):

dist(v, e+cost[u])←min⁡(dist(v, e+cost[u]), dist(u,e)+w)\textit{dist}\big(v,\ e + \text{cost}[u]\big) \leftarrow \min\Big(\textit{dist}\big(v,\ e + \text{cost}[u]\big),\ \textit{dist}(u, e) + w\Big)

由於每條邊的耗時 ww 皆為正數,可以直接在這張分層圖上跑 Dijkstra 演算法。

同時兼顧最短時間與最大剩餘電量

答案要求先讓抵達時間最小,再讓剩餘電量最大;因為初始電量固定,剩餘電量最大等價於累計耗電量 ee 最小。將最小堆(min-heap)中的元素設為 (d,e,u)(d, e, u),狀態就會依照 (d,e)(d, e) 的字典序由小到大彈出。

關鍵結論

第一次從最小堆彈出 target\text{target} 時,對應的 dd 就是最短抵達時間,ee 就是該時間下的最小耗電量,直接回傳 [d, power−e][d,\ \text{power} - e] 即可。

這之所以成立,是因為每條邊的耗時 w≥1w \ge 1,所有能到達時間 dd 的前驅狀態耗時都嚴格小於 dd,早在處理到時間 dd 之前就已全部鬆弛完畢並入堆;輪到時間 dd 時,最小堆自然會將這些狀態按 ee 由小到大逐一取出。

實作細節

  • 先判終點,再扣電量:抵達 target\text{target} 後無須再轉發訊號,因此彈出狀態後要先檢查是否為 target\text{target},再判斷 e+cost[u]e + \text{cost}[u] 是否超過 power\text{power}。這也一併涵蓋了 source=target\text{source} = \text{target} 的情況,起點彈出時會直接回傳 [0,power][0, \text{power}]。
  • 跳過過期紀錄:若彈出的狀態滿足 d>dist(u,e)d > \textit{dist}(u, e),代表同一狀態 (u,e)(u, e) 已有更短時間的紀錄先被處理過,直接略過即可。

複雜度分析

令 mm 為邊數,P=powerP = \text{power}。

  • 時間複雜度:O(nP+mPlog⁡(mP))\mathcal{O}(nP + mP \log(mP))。初始化 dist\textit{dist} 表需 O(nP)\mathcal{O}(nP);分層圖最多有 O(mP)\mathcal{O}(mP) 條邊,每條邊最多觸發一次鬆弛與入堆操作。
  • 空間複雜度:O(nP+mP)\mathcal{O}(nP + mP),分別用於 dist\textit{dist} 表與最小堆。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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, w in edges:
g[u].append((v, w))

# dist[u][e]:累計消耗 e 單位電量到達 u 的最短時間
dist = [[inf] * (power + 1) for _ in range(n)]
dist[source][0] = 0

hp = [(0, 0, source)] # (時間, 已消耗電量, 節點)
while hp:
d, e, u = heappop(hp)

if u == target:
return [d, power - e]

ne = e + cost[u] # 從 u 轉發後的累計耗電量
if d > dist[u][e] or ne > power:
continue

for v, w in g[u]:
nd = d + w
if nd < dist[v][ne]:
dist[v][ne] = nd
heappush(hp, (nd, ne, v))

return [-1, -1]

寫在最後

Cover Image Credit

The cover image features an artwork created by @⚠️AI-Generated. All rights belong to the original artist.

If you are the copyright holder and prefer it not to be used here, please reach out via email or leave a comment, and I will remove it promptly.