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 != iis in the listgraph[i]exactly once, if and only if nodesiandjare 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 <= 120 <= 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 ...
随机推荐
- MySql中循环的使用
一.while循环 语法:WHILE [条件] DO [逻辑] END WHILE; delimiter $$ DROP FUNCTION IF EXISTS `fn_findCharCount` $ ...
- Django基础教程
实例练习1-提交数据并展示 1.app_01下的views.py info_list=[] def userInfor(req): if req.method=="POST": u ...
- mysql批量数据导入探究
最近工作碰到一个问题,如何将大量数据(100MB+)导入到远程的mysql server上. 尝试1: Statement执行executeBatch的方法.每次导入1000条记录.时间为12s/10 ...
- axios 设置拦截器 全局设置带默认参数(发送 token 等)
应用场景: 1,每个请求都带上的参数,比如token,时间戳等. 2,对返回的状态进行判断,比如token是否过期 代码如下: [javascript] view plain copy axios.i ...
- java实现从url路径中下载pdf文档到本地
package com.cellstrain.icell.util; import java.io.*;import java.net.*; public class DownloadPdf { /* ...
- 31. The New Bread Earners 挣钱养家的新军
31. The New Bread Earners 挣钱养家的新军 ① They call them the new bread earners.They are women,and they are ...
- 右值引用和std::move函数(c++11)
1.对象移动 1)C++11新标准中的一个最主要的特性就是移动而非拷贝对象的能力 2)优势: 在某些情况下,从旧内存拷贝到新内存是不必要的,此时对对象进行移动而非拷贝可以提升性能 有些类如IO类或un ...
- 多网卡下对ServerSocket以TCP协议绑定IP和端口的测试
一.引言:之前开发TCP协议的程序(C#里是Socket为主)都是基于主机上只有一个IP的,后来项目里涉及到了主机需要同时连接内外和外网的情况,在该主机上部署着一套WCS系统和一套WMS系统:WCS系 ...
- python 按行读取判断是否为空
for line in fr.readlines(): try: # print(len(line)) if(len(line)==1): continue else: fw.write(matche ...
- 20155202 2016-2017-2 《Java程序设计》第6周学习总结
20155202 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 输入输出 数据从来源取出:输入串流 java.io.InputStream 写入目的的:输出 ...