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, ...
随机推荐
- python环境变量配置 - CSDN博客
一.下载: 1.官网下载python3.0系列(https://www.python.org/) 2.下载后图标为: 二.安装: Window下: 1.安装路径: 默认安装路径:C:\python35 ...
- Python collection模块与深浅拷贝
collection模块是对Python的通用内置容器:字典.列表.元组和集合的扩展,它包含一些专业的容器数据类型: Counter(计数器):dict子类,用于计算可哈希性对象的个数. Ordere ...
- Mysql之DQL------基础查询
#笔记内容来自于B站尚硅谷教学视频(av49181542)use myemployees; 查询表中的单个字段 SELECT last_name FROM employees; 查询表中的多个字段 # ...
- python collections模块 之 defaultdict
defaultdict 是 dict 的子类,因此 defaultdict 也可被当成 dict 来使用,dict 支持的功能,defaultdict 基本都支持.但它与 dict 最大的区别在于,如 ...
- Loadrunner 性能测试工具笔记
性能的是的基础知识 什么是负载? 系统实际用户:可能会有很多人使用同一个系统,但并不是所有用户都回同时使用该系统,所以系统的实际用户是一个容量问题,而不是负载的问题 系统在线用户:当系统用户对系统进行 ...
- C# 封装首页、上一页、下一月、尾页处理器
public void BtnPageClickEvent(object sender,string focusForeground,string lostFocusForeground) { But ...
- spring:ApplicationContext的三个实现类
* ApplicationContest的三个常用实现类* ClassPathXmlApplicationContext:它可以加载类路径的配置文件,要求配置文件必须在类路径下,如果不在则加载不了* ...
- .NET中DataTable的常用操作
一.目的 在各种.NET开发中,DataTable都是一个非常常见且重要的类型,在与数据打交道的过程中可以说是必不可少的对象. 它功能强大,属性与功能也是相当丰富,用好的话,使我们在处理数据时,减少很 ...
- art-template官方文档
http://aui.github.io/art-template/zh-cn/docs/
- Android 开发 MediaRecorder视频录制入门
前言 MediaRecorder是Android SDK提供用于录制音视频,关于音频的录制在我另一篇博客里已经介绍.传送门: https://www.cnblogs.com/guanxinjing/p ...