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, ...
随机推荐
- 14. TIM of STM32F103ZE
block diagram 14.3.1 Time-base unit 有三个基础的寄存器: 计数寄存器(TIMx_CNT) 预分配寄存器(TIMx_PSC), 自动重载寄存器(TIMx_ARR) 重 ...
- QString组合、拆分。
1.组合字符常用arg()函数 QString test=QString("_haha_%1_hehe%2") .arg("ee").arg("aa& ...
- 《DSP using MATLAB》Problem 8.33
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- <数据分析>初级入门
1.何为数据分析? 数据分析是指用适当的统计方法对收集来的大量数据进行分析,将它们加以汇总和理解消化,以求最大化地开发数据的功能,发挥数据的作用. 直接的理解:提炼杂乱无章的数据背后的信息,总结出研究 ...
- HTML中使用js的三种方式及优缺点介绍
1.内部js: 在直接在页面的<script></script>标签内写js代码 优点:相对于使用行内js,内部js代码较为集中,与页面结构的实现代码耦合度较低,比较便于维护 ...
- 数据库DQL、DML、DDL及DCL详解
目录 1. 数据查询语言(DQL,Data Query Language) 2. 数据操纵语言(DML,Data Manipulation Language) 3. 数据定义语言(DDL,Data D ...
- java实践经验几种常见数据库连接池的使用比较
经历的几个产品及项目中,包括了各种数据库及应用服务器,基本上几种常见的数据库连接池都用到了,根据使用的情况把这些连接池比较一下吧.(http://m.0834jl.com) 感觉在介绍之前有必要阐述一 ...
- LUOGU P4163 [SCOI2007]排列
传送门 解题思路 首先我们发现这道题s的长度很小,所以考虑点暴力的做法,状压dp或搜索.本蒟蒻搜索永远调不对,所以就写了个状压dp.因为所有s里的数都要出现一次,并且最后的答案是要求整除,那么我们设d ...
- 为什么要使用Vue.$set(target,key,value)
vue中不能检测到数组和对象的两种变化: 1.数组长度的变化 vm.arr.length = 4 2,数组通过索引值修改内容 vm.arr[1] = 'aa' Vue.$set(target,key, ...
- 自动安装php7(配置未优化版本)
#!/bin/bash #by dxd - #only suit for centos/aliyun os, and based on aliyun install script CURR_PATH= ...