leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径
设计最短路径 用bfs 天然带最短路径
每一个状态是 当前的阶段 和已经访问过的节点
下面是正确但是超时的代码
class Solution:
def shortestPathLength(self, graph):
"""
:type graph: List[List[int]]
:rtype: int
"""
N=len(graph)
Q=collections.deque([(1 << x, x) for x in range(N)])
D=collections.defaultdict(lambda:N*N)
for i in range(N):
D[1<<i,i]=0
mask=0
#listr= [i for i in range(N)]
#random.shuffle(listr)
for i in range(N):
mask=mask|1<<i
while Q:
a,h=Q.popleft()
d=D[a,h]
if(a==mask):
return d
for child in graph[h]:
new_a=a |(1<<child)
Q.append((new_a,child))
D[new_a,child]=min(d+1,D[new_a,child])
下面的代码稍微有优化,但是这种优化是非常好的。(值得思考的优化)
归根结底是因为没有考虑好一个状态是指什么。
在这里一个状态指的是, 以前便利过的节点 和 当前的节点。
有了这个节点状态
就不用把(到达这个节点的距离作为节点的一部分加入节点中,加入会引起性能损失)
到达这个节点的距离 单独存入一个数组中做优化 如果同一个状态后来的距离反而更长 那就丢弃。
class Solution:
def shortestPathLength(self, graph):
N=len(graph)
Q=collections.deque([(1 << x, x) for x in range(N)])
D=collections.defaultdict(lambda:N*N)
for i in range(N):
D[1<<i,i]=0
mask=0
for i in range(N):
mask=mask|1<<i
while Q:
cover,head=Q.popleft()
d=D[cover,head]
if(cover==mask):
return d
for child in graph[head]:
new_a=cover |(1<<child)
if(d+1<D[new_a,child]):
D[new_a,child]=d+1
Q.append((new_a,child))
动态规划算法
一个数组
state[ cover ][ head ] 表示当前状态所能达到的最小距离
head 维数的遍历顺序随意
cover 的遍历循序是从小到大
因为 new_cover=cover| child 这样的话内在隐含着一个内在的顺序
这样看来 这个题目有意卡掉了那些没有注意到这种顺序的方法
"relaxation step" (for those familiar with the Bellman-Ford algorithm)
复习 Bellman-Ford algorithm#
O(V*E)
1 初始化
2 n-1 次循环 每一个循环遍历每一个边 做 relaxtion step
3 额外判断是否每一条边都 存在优化空间
第i次循环 实际上是在寻找 单源最短路径
如果n-1 次循环后还能做 relaxtion step 那么说明图中存在 负权回路
leetcode 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
题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
- 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 ...
- [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 问题描述: 问题求解: 本题要求是求遍历所有节点的最短路径,由于本题中是没有要求一个节点只能访问一次的,也就是说可以访问一个节点多次,但是如果表征两次节点状态呢 ...
- AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)
题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...
- LeetCode 1091. Shortest Path in Binary Matrix
原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...
随机推荐
- LightOJ-1259-Goldbach`s Conjecture-素数打表+判断素数对数
Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathemat ...
- ELK5.2+kafka+zookeeper+filebeat集群部署
架构图 考虑到日志系统的可扩展性以及目前的资源(部分功能复用),整个ELK架构如下: 架构解读 : (整个架构从左到右,总共分为5层) 第一层.数据采集层 最左边的是业务服务器集群,上面安装了file ...
- SpringBoot--Banner的定制和关闭
SpringBoot项目启动的时候控制台会打印如下信息: 上面红色框框内的“SPRING BOOT”被称为Banner,意为横幅,默认会开启并在控制台打印,其实我们可以修改它的内容和样式,即定制:并选 ...
- Django之单表查询——神奇的双下划线
1.filter中的单表查询 # 查询id>1且id<4的结果 ret = models.Person.objects.filter(id__gt=1,id__lt=4) print(re ...
- 第二周课堂笔记1th
1. 三元运算 + 2. for循环 for为有限循环,while为无限循环 可迭代对象:是字符串,数字不可以 数字不可以迭代但是可以用range函数 for i in range(1 ...
- 面试系列25 dubbo的spi思想是什么
spi,简单来说,就是service provider interface,说白了是什么意思呢,比如你有个接口,现在这个接口有3个实现类,那么在系统运行的时候对这个接口到底选择哪个实现类呢?这就需要s ...
- mysql简单的操作
启动数据库服务 net start mysql 停止数据库服务 net stop mysql 退出数据库 exit 保存操作及结果 将在命令行窗口中 ...
- C++开发系列-友元函数 友元类
友元函数 默认一个类的私有属性只能在该类的内部可以直接访问.友元函数申明在内的内部,实现在类的外部可以直接访问类的私有属性. class A1 { public: A1() { a1 = 100; a ...
- spring:AOP面向切面编程02
参考: https://blog.csdn.net/jeffleo/article/details/54136904 一.AOP的核心概念AOP(Aspect Oriented Programming ...
- centos7 搭建 php7 + nginx (2)
安装php php下载地址 # 避免出错,先安装下面 yum install libzip libzip-devel libxml2-devel openssl openssl-devel bzip2 ...