847. Shortest Path Visiting All Nodes
An undirected, connected graph of N nodes (labeled
0, 1, 2, ..., N-1
) is given asgraph
.
graph.length = N
, andj != i
is in the listgraph[i]
exactly once, if and only if nodesi
andj
are connected.Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.
Example 1:
Input: [[1,2,3],[0],[0],[0]]
Output: 4
Explanation: One possible path is [1,0,2,0,3]Example 2:
Input: [[1],[0,2,4],[1,3,4],[2],[1,2]]
Output: 4
Explanation: One possible path is [0,1,4,2,3]
Note:
1 <= graph.length <= 12
0 <= graph[i].length < graph.length
Approach #1: BFS + Bit. [C++]
class Solution {
public:
int shortestPathLength(vector<vector<int>>& graph) {
const int n = graph.size();
const int kAns = (1 << n) - 1;
queue<pair<int, int>> q;
vector<vector<int>> visited(n, vector<int>(1<<n));
for (int i = 0; i < n; ++i)
q.push({i, 1 << i});
int steps = 0; while (!q.empty()) {
int s = q.size();
while (s--) {
auto p = q.front();
q.pop();
int node = p.first;
int state = p.second;
if (state == kAns) return steps;
if (visited[node][state]) continue;
visited[node][state] = 1;
for (int next : graph[node])
q.push({next, state | (1 << next)});
}
++steps;
} return -1;
}
};
Reference:
http://zxi.mytechroad.com/blog/graph/leetcode-847-shortest-path-visiting-all-nodes/
847. Shortest Path Visiting All Nodes的更多相关文章
- [LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
- 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
- [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)
题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...
- leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径
设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...
- LeetCode 847. Shortest Path Visiting All Nodes
题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...
- [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
- 最短路径遍历所有的节点 Shortest Path Visiting All Nodes
2018-10-06 22:04:38 问题描述: 问题求解: 本题要求是求遍历所有节点的最短路径,由于本题中是没有要求一个节点只能访问一次的,也就是说可以访问一个节点多次,但是如果表征两次节点状态呢 ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
随机推荐
- 【原创】VB超强游戏外挂帮助类,封装了很多方法
''' <summary> ''' a very nice file that can be used on other projects ''' </summary> ''' ...
- redhat配置java环境
1.检查是否有旧版本的jdk java -version java version "1.4.2" gij (GNU libgcj) version (Red Hat -) Cop ...
- composer install 时,提示:Package yiisoft/yii2-codeception is abandoned, you should avoid using it. Use codeception/codeception instead.的解决
由 SHUIJINGWAN · 2017/11/24 1.composer install 时,提示:Package yiisoft/yii2-codeception is abandoned, yo ...
- vue.js vue-cli 中解决 axios 跨域调用的问题
修改 /config/index.js 文件如下: proxyTable: { '/api': { target: 'http://chifan.local', changeOrigin: true, ...
- 移动文件流的读写指针---fseek
函数原型:int fseek(FILE *stream,long offset,int origin) stream:文件指针, offset:偏移量,正数表示正向偏移,负数表示负向偏移.origin ...
- 库函数方式文件编程----fopen
使用fopen等文件访问库函数编写应用程序,该应用程序实现文件的复制功能. 1.打开文件---fopen 函数功能:打开文件 头文件:#include<stdio.h> 函数原型:FILE ...
- 二进制搭建kubernetes多master集群【三、配置k8s master及高可用】
前面两篇文章已经配置好了etcd和flannel的网络,现在开始配置k8s master集群. etcd集群配置参考:二进制搭建kubernetes多master集群[一.使用TLS证书搭建etcd集 ...
- js 获取时间不能大于当前系统时间
var dataDate=$.trim($("#dataDate").val()); if(dataDate.length==0){ $("#dataDateTip&qu ...
- vivado用法
声明为”DEBUG”,即使没有连接到其他模块,也不会被优化掉.但并不是所有的信号都是在声明为“debug”属性之后就不会优化掉. (2)同一个bank中能设置一个电平. (3)
- spring boot docker 初尝试
Docker服务中进程间通信通过/var/run/docker.sock实现,默认服务不提供监听端口,因此使用docker remote api 需要手动绑定端口. 在centos7.2下,可以进行这 ...