3970. Shortest Path With At Most K Consecutive Identical Characters
SourceWeekly Contest 507 Q3DifficultyMediumRating1840
Description
You are given an integer n representing the number of nodes in a directed weighted graph, numbered from 0 to n - 1. This is represented by a 2D integer array edges, where edges[i] = [ui, vi, wi] represents a directed edge from node ui to node vi with weight wi.
You are also given a string labels of length n, where labels[i] is the character assigned to node i, and an integer k.
Return the minimum total edge weight of a path from node 0 to node n - 1 such that the concatenation of the labels of the nodes along the path contains at most k consecutive identical characters. If no valid path exists, return -1.
Example 1:
Input: n = 3, edges = [[0,1,1],[1,2,1],[0,2,3]], labels = "aab", k = 1
Output: 3
Explanation:
The optimal valid path from node 0 to node 2 is as follows:
- Use
edges[2] = [0, 2, 3]to reach node 2 with a weightwi = 3.
"ab", which satisfies at most k = 1 consecutive identical characters. Thus, the answer is 3.Example 2:
Input: n = 3, edges = [[0,1,1],[1,2,1],[0,2,3]], labels = "aab", k = 2
Output: 2
Explanation:
The optimal valid path from node 0 to node 2 is as follows:
- Use
edges[0] = [0, 1, 1]to reach node 1 with weightwi = 1. - Use
edges[1] = [1, 2, 1]to reach node 2 with weightwi = 1.
"aab", which satisfies at most k = 2 consecutive identical characters. Thus, the answer is 2.Example 3:
Input: n = 3, edges = [[0,1,1],[1,2,1]], labels = "aaa", k = 2
Output: -1
Explanation:
There is no valid path from node 0 to node 2 that satisfies at most k = 2 consecutive identical characters. Thus, the answer is -1.
Constraints:
1 <= n == labels.length <= 5 * 1040 <= edges.length <= 5 * 104edges[i] == [ui, vi, wi]0 <= ui, vi <= n - 1ui != vi1 <= wi <= 104labelsconsists of lowercase English letters1 <= k <= 50
Solutions
Solution 1: Dijkstra
Thinking
Edge weights are nonnegative, so a shortest path from node \(0\) to \(n - 1\) is a Dijkstra problem, except the concatenated labels may contain at most \(k\) identical characters in a row. The unconstrained shortest path can be exactly the one that overruns a run, while a costlier path stays valid by changing labels along the way. With \(n\) and the number of edges both up to \(5 \times 10^4\), enumerating paths is not viable.
Reaching the same node with different trailing run lengths leaves different room for another identical label, so one distance per node drops legal transitions. The run depends only on the current label and how long it has already lasted: the next label increments it when equal and resets it to \(1\) otherwise, states past \(k\) are dropped, and the start label itself counts as a run of \(1\).
The state is \((u, \textit{cur})\), and \(\textit{dist}[u][\textit{cur}]\) stores the minimum edge-weight sum to reach it. Nonnegative weights let a binary heap expand states by distance, and the first time the destination leaves the heap is the answer.
Let \(\textit{dist}[u][\textit{cur}]\) be the minimum total edge weight of a path from node \(0\) to node \(u\) whose trailing run of identical labels has length \(\textit{cur}\).
Build an adjacency list from \(\textit{edges}\). The start label is a run of length \(1\), so \(\textit{dist}[0][1] = 0\). Push \((0, 0, 1)\) into a min-heap. The triple is the distance, the node, and the run length.
Pop the triple \((d, u, \textit{cur})\) with the smallest distance. If \(u = n - 1\), nonnegative weights make this the best way to reach the destination, so return \(d\). If \(d > \textit{dist}[u][\textit{cur}]\), the state is stale and is skipped.
Otherwise scan each outgoing edge \((v, w)\). The next run length is \(\textit{nxt} = 1\) when the labels differ and \(\textit{nxt} = \textit{cur} + 1\) when they match. The new distance is \(nd = d + w\). Update \(\textit{dist}[v][\textit{nxt}]\) and push the state only when \(\textit{nxt} \le k\) and \(nd < \textit{dist}[v][\textit{nxt}]\).
If the heap empties before the destination is reached, return \(-1\).
The time complexity is \(O(k(n + m) \log (nk))\), and the space complexity is \(O(nk + m)\), where \(n\) and \(m\) are the number of nodes and edges.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
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 29 30 31 32 33 34 35 36 37 38 39 | |
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 29 30 31 32 33 34 35 | |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
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 29 30 31 32 33 | |