设计最短路径 用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 无向连通图遍历最短路径的更多相关文章

  1. [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 ...

  2. LeetCode 847. Shortest Path Visiting All Nodes

    题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...

  3. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  4. 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...

  5. 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 ...

  6. [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 ...

  7. 最短路径遍历所有的节点 Shortest Path Visiting All Nodes

    2018-10-06 22:04:38 问题描述: 问题求解: 本题要求是求遍历所有节点的最短路径,由于本题中是没有要求一个节点只能访问一次的,也就是说可以访问一个节点多次,但是如果表征两次节点状态呢 ...

  8. 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 ...

  9. LeetCode 1091. Shortest Path in Binary Matrix

    原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...

随机推荐

  1. 伪造ip头

    x-forwarded-for: 127.0.0.1x-remote-IP: 127.0.0.1x-remote-ip: 127.0.0.1x-client-ip: 127.0.0.1x-client ...

  2. spring boot过滤器FilterRegistrationBean

    有2种方式可以实现过滤器 1:通过FilterRegistrationBean实例注册 2:通过@WebFilter注解生效 这里选择第一种,因为第二种不能设置过滤器之间的优先级 为了演示优先级,这里 ...

  3. 18-1-函数中this的指向

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Linux vi和vim编辑器(1)

    1:vi和vim的三种常见模式  1.1正常模式 在正常模式下,我们可以使用快捷键: 以vim打开一个档案就直接进入一般模式了(这是默认的模式).在这个模式中,你可以使用[上下左右」按键来移动光标,你 ...

  5. gnome3 修改桌面背景图片模式

    修改背景图片,可以在 桌面右键 选择"修改壁纸",选择"background"(背景),这里没有设定背景图片模式. 可以在 “应用程序”->"工 ...

  6. Georgia and Bob

    Georgia and Bob 给出一个严格递增的正整数数列\(\{a_i\}\),每一次操作可以对于其中任意一个数减去一个正整数,但仍然要保证数列的严格递增性,现在两名玩家轮流操作,不能操作的玩家判 ...

  7. 如何使用C++获取 进程的 绝对路径

    DWORD GetProcessId(IN PCHAR szExeName) { DWORD dwRet = 0; DWORD dwCount = 0; HANDLE hSnapshot = Crea ...

  8. vue-resourse简单使用方法

    一.安装引用 安装: npm install vue-resource --save-dev 引用: /*引入Vue框架*/ import Vue from 'vue' /*引入资源请求插件*/ im ...

  9. mysql联表查询,使用phpStudy自带的

    一.内联结.外联结.左联结.右联结的含义及区别在SQL标准中规划的(Join)联结大致分为下面四种:1.内联结:将两个表中存在联结关系的字段符合联结关系的那些记录形成记录集的联结.2.外联结:分为外左 ...

  10. Android基础控件Button的使用

    1.相关属性 Android的按钮有Button和ImageButton(图像按钮),Button extends TextView, ImageButton extends ImageView! a ...