847:访问所有节点的最短路径

This commit is contained in:
huangge1199@hotmail.com 2021-08-06 22:33:12 +08:00
parent d8832d3cac
commit a68f1a6fbc
4 changed files with 134 additions and 2 deletions

View File

@ -0,0 +1,91 @@
//存在一个由 n 个节点组成的无向连通图图中的节点按从 0 n - 1 编号
//
// 给你一个数组 graph 表示这个图其中graph[i] 是一个列表由所有与节点 i 直接相连的节点组成
//
// 返回能够访问所有节点的最短路径的长度你可以在任一节点开始和停止也可以多次重访节点并且可以重用边
//
//
//
//
//
//
// 示例 1
//
//
//输入graph = [[1,2,3],[0],[0],[0]]
//输出4
//解释一种可能的路径为 [1,0,2,0,3]
//
// 示例 2
//
//
//
//
//输入graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
//输出4
//解释一种可能的路径为 [0,1,4,2,3]
//
//
//
//
// 提示
//
//
// n == graph.length
// 1 <= n <= 12
// 0 <= graph[i].length < n
// graph[i] 不包含 i
// 如果 graph[a] 包含 b 那么 graph[b] 也包含 a
// 输入的图总是连通图
//
// Related Topics 位运算 广度优先搜索 动态规划 状态压缩
// 👍 203 👎 0
package leetcode.editor.cn;
import java.util.LinkedList;
import java.util.Queue;
//847:访问所有节点的最短路径
class ShortestPathVisitingAllNodes {
public static void main(String[] args) {
//测试代码
Solution solution = new ShortestPathVisitingAllNodes().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int shortestPathLength(int[][] graph) {
int n = graph.length;
Queue<int[]> queue = new LinkedList<int[]>();
boolean[][] seen = new boolean[n][1 << n];
for (int i = 0; i < n; ++i) {
queue.offer(new int[]{i, 1 << i, 0});
seen[i][1 << i] = true;
}
int ans = 0;
while (!queue.isEmpty()) {
int[] tuple = queue.poll();
int u = tuple[0], mask = tuple[1], dist = tuple[2];
if (mask == (1 << n) - 1) {
ans = dist;
break;
}
// 搜索相邻的节点
for (int v : graph[u]) {
// mask 的第 v 位置为 1
int maskV = mask | (1 << v);
if (!seen[v][maskV]) {
queue.offer(new int[]{v, maskV, dist + 1});
seen[v][maskV] = true;
}
}
}
return ans;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,41 @@
<p>存在一个由 <code>n</code> 个节点组成的无向连通图,图中的节点按从 <code>0</code><code>n - 1</code> 编号。</p>
<p>给你一个数组 <code>graph</code> 表示这个图。其中,<code>graph[i]</code> 是一个列表,由所有与节点 <code>i</code> 直接相连的节点组成。</p>
<p>返回能够访问所有节点的最短路径的长度。你可以在任一节点开始和停止,也可以多次重访节点,并且可以重用边。</p>
<p>&nbsp;</p>
<ol>
</ol>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/12/shortest1-graph.jpg" style="width: 222px; height: 183px;" />
<pre>
<strong>输入:</strong>graph = [[1,2,3],[0],[0],[0]]
<strong>输出:</strong>4
<strong>解释:</strong>一种可能的路径为 [1,0,2,0,3]</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/05/12/shortest2-graph.jpg" style="width: 382px; height: 222px;" /></p>
<pre>
<strong>输入:</strong>graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
<strong>输出:</strong>4
<strong>解释:</strong>一种可能的路径为 [0,1,4,2,3]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == graph.length</code></li>
<li><code>1 &lt;= n &lt;= 12</code></li>
<li><code>0 &lt;= graph[i].length &lt;&nbsp;n</code></li>
<li><code>graph[i]</code> 不包含 <code>i</code></li>
<li>如果 <code>graph[a]</code> 包含 <code>b</code> ,那么 <code>graph[b]</code> 也包含 <code>a</code></li>
<li>输入的图总是连通图</li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li><li>广度优先搜索</li><li></li><li>动态规划</li><li>状态压缩</li></div></div>\n<div><li>👍 203</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long