|
1 |
| -# [2714. 找到最短路径的 K 次跨越](https://leetcode.cn/problems/find-shortest-path-with-k-hops)[English Version](/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/README_EN.md)## 题目描述<!-- 这里写题目描述 --><p>现给定一个正整数 n ,它表示一个<strong> 索引从 0 开始的无向带权连接图</strong> 的节点数,以及一个 <strong>索引从 0 开始的二维数组 </strong><code>edges</code> ,其中 <code>edges[i] = [ui, vi, wi]</code> 表示节点 <code>ui</code> 和 <code>vi</code> 之间存在权重为 <code>wi</code> 的边。</p><p>还给定两个节点 <code>s</code> 和 <code>d</code> ,以及一个正整数 <code>k</code> ,你的任务是找到从 s 到 d 的 <strong>最短 </strong>路径,但你可以 <strong>最多</strong> 跨越 <code>k</code> 条边。换句话说,将 <strong>最多</strong> <code>k</code> 条边的权重设为 <code>0</code>,然后找到从 <code>s</code> 到 <code>d</code> 的 <strong>最短</strong> 路径。</p><p>返回满足给定条件的从 <code>s</code> 到 <code>d</code> 的 <strong>最短</strong> 路径的长度。</p><p> </p><p><strong class="example">示例 1:</strong></p><pre><b>输入:</b>n = 4, edges = [[0,1,4],[0,2,2],[2,3,6]], s = 1, d = 3, k = 2<b>输出:</b>2<b>解释:</b>在这个例子中,只有一条从节点 1(绿色节点)到节点 3(红色节点)的路径,即(1->0->2->3),其长度为 4 + 2 + 6 = 12。现在我们可以将两条边的权重设为 0,即将蓝色边的权重设为 0,那么路径的长度就变为 0 + 2 + 0 = 2。可以证明 2 是我们在给定条件下能够达到的最小路径长度。</pre><p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/1.jpg" style="width: 170px; height: 171px;" /></p><p><strong class="example">示例 2:</strong></p><pre><b>输入:</b>n = 7, edges = [[3,1,9],[3,2,4],[4,0,9],[0,5,6],[3,6,2],[6,0,4],[1,2,4]], s = 4, d = 1, k = 2<b>输出:</b>6<b>解释:</b>在这个例子中,有两条从节点 4(绿色节点)到节点 1(红色节点)的路径,分别是(4->0->6->3->2->1)和(4->0->6->3->1)。第一条路径的长度为 9 + 4 + 2 + 4 + 4 = 23,第二条路径的长度为 9 + 4 + 2 + 9 = 24。现在,如果我们将蓝色边的权重设为 0,那么最短路径的长度就变为 0 + 4 + 2 + 0 = 6。可以证明 6 是我们在给定条件下能够达到的最小路径长度。</pre><p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/2.jpg" style="width: 400px; height: 171px;" /></p><p><strong class="example">示例 3:</strong></p><pre><b>输入:</b>n = 5, edges = [[0,4,2],[0,1,3],[0,2,1],[2,1,4],[1,3,4],[3,4,7]], s = 2, d = 3, k = 1<b>输出:</b>3<b>解释:</b>在这个例子中,从节点 2(绿色节点)到节点 3(红色节点)有 4 条路径,分别是(2->1->3)、(2->0->1->3)、(2->1->0->4->3)和(2->0->4->3)。前两条路径的长度为 4 + 4 = 1 + 3 + 4 = 8,第三条路径的长度为 4 + 3 + 2 + 7 = 16,最后一条路径的长度为 1 + 2 + 7 = 10。现在,如果我们将蓝色边的权重设为 0,那么最短路径的长度就为 1 + 2 + 0 = 3。可以证明在给定条件下,3 是我们能够达到的最小路径长度。</pre><p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/3.jpg" style="width: 300px; height: 296px;" /></p><p> </p><p><strong>提示:</strong></p><ul> <li><code>2 <= n <= 500</code></li> <li><code>n - 1 <= edges.length <= n \* (n - 1) / 2</code></li> <li><code>edges[i].length = 3</code></li> <li><code>0 <= edges[i][0], edges[i][1] <= n - 1</code></li> <li><code>1 <= edges[i][2] <= 10<sup>6</sup></code></li> <li><code>0 <= s, d, k <= n - 1</code></li> <li><code>s != d</code></li> <li>输入的生成确保图是 <strong>连通</strong> 的,并且没有 <strong>重复的边</strong> 或 <strong>自环</strong>。</li></ul>## 解法<!-- 这里可写通用的实现逻辑 --><!-- tabs:start -->### **Python3**<!-- 这里可写当前语言的特殊实现逻辑 -->`python`### **Java**<!-- 这里可写当前语言的特殊实现逻辑 -->`java`### **C++**`cpp`### **Go**`go`### **...**``````<!-- tabs:end --> |
| 1 | +# [2714. 找到最短路径的 K 次跨越](https://leetcode.cn/problems/find-shortest-path-with-k-hops) |
| 2 | + |
| 3 | +[English Version](/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>现给定一个正整数 n ,它表示一个<strong> 索引从 0 开始的无向带权连接图</strong> 的节点数,以及一个 <strong>索引从 0 开始的二维数组 </strong><code>edges</code> ,其中 <code>edges[i] = [ui, vi, wi]</code> 表示节点 <code>ui</code> 和 <code>vi</code> 之间存在权重为 <code>wi</code> 的边。</p> |
| 10 | + |
| 11 | +<p>还给定两个节点 <code>s</code> 和 <code>d</code> ,以及一个正整数 <code>k</code> ,你的任务是找到从 s 到 d 的 <strong>最短 </strong>路径,但你可以 <strong>最多</strong> 跨越 <code>k</code> 条边。换句话说,将 <strong>最多</strong> <code>k</code> 条边的权重设为 <code>0</code>,然后找到从 <code>s</code> 到 <code>d</code> 的 <strong>最短</strong> 路径。</p> |
| 12 | + |
| 13 | +<p>返回满足给定条件的从 <code>s</code> 到 <code>d</code> 的 <strong>最短</strong> 路径的长度。</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | + |
| 17 | +<p><strong class="example">示例 1:</strong></p> |
| 18 | + |
| 19 | +<pre> |
| 20 | +<b>输入:</b>n = 4, edges = [[0,1,4],[0,2,2],[2,3,6]], s = 1, d = 3, k = 2 |
| 21 | +<b>输出:</b>2 |
| 22 | +<b>解释:</b>在这个例子中,只有一条从节点1(绿色节点)到节点3(红色节点)的路径,即(1->0->2->3),其长度为4 + 2 + 6 = 12。现在我们可以将两条边的权重设为 0,即将蓝色边的权重设为 0,那么路径的长度就变为 0 + 2 + 0 = 2。可以证明 2 是我们在给定条件下能够达到的最小路径长度。 |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/1.jpg" style="width: 170px; height: 171px;" /></p> |
| 26 | + |
| 27 | +<p><strong class="example">示例 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<b>输入:</b>n = 7, edges = [[3,1,9],[3,2,4],[4,0,9],[0,5,6],[3,6,2],[6,0,4],[1,2,4]], s = 4, d = 1, k = 2 |
| 31 | +<b>输出:</b>6 |
| 32 | +<b>解释:</b>在这个例子中,有两条从节点4(绿色节点)到节点1(红色节点)的路径,分别是(4->0->6->3->2->1)和(4->0->6->3->1)。第一条路径的长度为 9 + 4 + 2 + 4 + 4 = 23,第二条路径的长度为 9 + 4 + 2 + 9 = 24。现在,如果我们将蓝色边的权重设为 0,那么最短路径的长度就变为 0 + 4 + 2 + 0 = 6。可以证明 6 是我们在给定条件下能够达到的最小路径长度。 |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/2.jpg" style="width: 400px; height: 171px;" /></p> |
| 36 | + |
| 37 | +<p><strong class="example">示例 3:</strong></p> |
| 38 | + |
| 39 | +<pre> |
| 40 | +<b>输入:</b>n = 5, edges = [[0,4,2],[0,1,3],[0,2,1],[2,1,4],[1,3,4],[3,4,7]], s = 2, d = 3, k = 1 |
| 41 | +<b>输出:</b>3 |
| 42 | +<b>解释:</b>在这个例子中,从节点2(绿色节点)到节点3(红色节点)有4条路径,分别是(2->1->3)、(2->0->1->3)、(2->1->0->4->3)和(2->0->4->3)。前两条路径的长度为4 + 4 = 1 + 3 + 4 = 8,第三条路径的长度为4 + 3 + 2 + 7 = 16,最后一条路径的长度为1 + 2 + 7 = 10。现在,如果我们将蓝色边的权重设为 0,那么最短路径的长度就为1 + 2 + 0 = 3。可以证明在给定条件下,3 是我们能够达到的最小路径长度。 |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/images/3.jpg" style="width: 300px; height: 296px;" /></p> |
| 46 | + |
| 47 | +<p> </p> |
| 48 | + |
| 49 | +<p><strong>提示:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li><code>2 <= n <= 500</code></li> |
| 53 | + <li><code>n - 1 <= edges.length <= n * (n - 1) / 2</code></li> |
| 54 | + <li><code>edges[i].length = 3</code></li> |
| 55 | + <li><code>0 <= edges[i][0], edges[i][1] <= n - 1</code></li> |
| 56 | + <li><code>1 <= edges[i][2] <= 10<sup>6</sup></code></li> |
| 57 | + <li><code>0 <= s, d, k <= n - 1</code></li> |
| 58 | + <li><code>s != d</code></li> |
| 59 | + <li>输入的生成确保图是 <strong>连通</strong> 的,并且没有 <strong>重复的边</strong> 或 <strong>自环</strong>。</li> |
| 60 | +</ul> |
| 61 | + |
| 62 | +## 解法 |
| 63 | + |
| 64 | +<!-- 这里可写通用的实现逻辑 --> |
| 65 | + |
| 66 | +<!-- tabs:start --> |
| 67 | + |
| 68 | +### **Python3** |
| 69 | + |
| 70 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 71 | + |
| 72 | +```python |
| 73 | + |
| 74 | +``` |
| 75 | + |
| 76 | +### **Java** |
| 77 | + |
| 78 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 79 | + |
| 80 | +```java |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +### **C++** |
| 85 | + |
| 86 | +```cpp |
| 87 | + |
| 88 | +``` |
| 89 | + |
| 90 | +### **Go** |
| 91 | + |
| 92 | +```go |
| 93 | + |
| 94 | +``` |
| 95 | + |
| 96 | +### **...** |
| 97 | + |
| 98 | +``` |
| 99 | +
|
| 100 | +``` |
| 101 | + |
| 102 | +<!-- tabs:end --> |
0 commit comments