[Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)
题解
题意
给出一个无向图,求遍历所有点的最小花费
分析
1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可
2.使用DP
代码
//BFS
class Solution {
public:
int dis[1<<12][12];
int shortestPathLength(vector<vector<int>>& graph) {
int n=graph.size();
if(n==0) return 0;
for(int i=0;i<(1<<n);++i)
for(int j=0;j<n;++j)
dis[i][j]=n*n;
queue<pair<int,int> >q;
for(int i=0;i<n;++i)
{
dis[1<<i][i]=0;
q.push({1<<i,i});
}
while(!q.empty())
{
pair<int,int>p=q.front();q.pop();
int x=q.front().first,y=q.front().second;
if(x+1==(1<<n)) return dis[x][y];
for(auto num:graph[y])
{
int d=dis[x][y];
int status=x|(1<<num);
if(d+1<dis[status][num])
{
dis[status][num]=d+1;
q.push({status,num});
}
}
}
return 0;
}
};
//DP
class Solution {
public:
int dp[1<<12][12];
int shortestPathLength(vector<vector<int>>& graph) {
int n=graph.size();
if(n==0) return 0;
for(int i=0;i<(1<<n);++i)
for(int j=0;j<n;++j)
dp[i][j]=(1<<j)==i?0:n*n;
for(int i=0;i<(1<<n);++i)
{
int repeat=true;
while(repeat)
{
repeat=false;
for(int head=0;head<n;++head)
{
for(auto nxt:graph[head])
{
int j=i|(1<<nxt);
if(dp[i][head]+1<dp[j][nxt])
{
dp[j][nxt]=dp[i][head]+1;
if(j==i) repeat=true;
}
}
}
}
}
int ans=n*n;
for(int i=0;i<n;++i) ans=min(ans,dp[(1<<n)-1][i]);
return ans;
}
};
[Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)的更多相关文章
- [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 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...
- 【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 问题描述: 问题求解: 本题要求是求遍历所有节点的最短路径,由于本题中是没有要求一个节点只能访问一次的,也就是说可以访问一个节点多次,但是如果表征两次节点状态呢 ...
- LeetCode 1091. Shortest Path in Binary Matrix
原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...
- [LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...
随机推荐
- 【题解】NOI2015软件包管理器
[题解][P2146 NOI2015]软件包管理器 实际上就是树链剖分板子题. 对于\(install\)操作,直接查询它到\(0\)节点有多少已经安装了的,再用总数减去它. 对于\(uninstal ...
- import org.marker.weixin.DefaultSession; import org.marker.weixin.HandleMessageAdapter; import org.marker.weixin.MySecurity; import org.marker.weixin.msg.*;
需要以下微信包可以添加我的微信公众号 回复“微信api”即可得到jar链接,以及maven添加本地jar方法,以及更改后的源代码 import org.marker.weixin.DefaultSes ...
- re.sub用法
re.sub功能是对于一个输入的字符串,利用正则表达式,来实现字符串替换处理的功能返回处理后的字符串 re.sub共有五个参数 三个必选参数pattern,repl,string 两个可选参数coun ...
- linux下更改文件夹名
mv 旧文件夹名 新文件夹名 mv /usr/bin/python_old /usr/bin/python_new
- 私有 npm 仓库的搭建
cnpm 是企业内部搭建 npm 镜像和私有 npm 仓库的开源方案,当企业业务逻辑相关的模块可能不适合开源.这部分私有的模块就可以放在私有 npm 仓库中来管理和维护. 以下为搭建私有 npm 的详 ...
- Linux--struct file结构体【转】
本文转载自:https://www.cnblogs.com/hanxiaoyu/p/5677677.html struct file(file结构体): struct file结构体定义在includ ...
- Too many open files解决方案及原理
以下是我解决Too many open files异常时学习的知识的理解和总结,如有不正确指出,敬请指出! 此问题中文搜索雷同,你可以尝试以下关键字:"file descriptor lea ...
- ios图文混编瀑布流
ios图文混编瀑布流,利用UICollectionView 实现图文混编的瀑布流,支持section内容伸缩 http://www.huiyi8.com/pubuliu/
- kvm初体验之七:attach usb storage device to a VM
1. virsh attach-disk vm1 /dev/sdb sdc 将host上的/dev/sdb挂载到vm1的/dev/sdc上 2. virsh detach-disk vm1 sdc 将 ...
- [原创]java在线打开PDF文档
步骤一:(涉及到的工具) 访问:http://www.zhuozhengsoft.com/dowm/,从官网下载PageOffice for Java. 步骤二:(配置工程) 1. 解压PageOff ...