题解

题意

给出一个无向图,求遍历所有点的最小花费

分析

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)的更多相关文章

  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 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...

  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. LeetCode 1091. Shortest Path in Binary Matrix

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

  9. [LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径

    We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...

随机推荐

  1. Pentaho BIServer Community Edtion 6.1 使用教程 第一篇 软件安装

    一.简介: Pentaho BI Server 分为企业版和社区版两个版本.其中 社区版 CE(community edtion) 为免费版本. 二.下载CE版(CentOS): 后台下载命令: no ...

  2. imagecopyresampled()改变图片大小后质量要比imagecopyresized()高。

    php程序中改变图片大小的函数大多数人都想到用imagecopyresized(),不过经过测试比较发现,使用imagecopyresampled()改变的图片质量更高. 下面我们来看看两者的比较结果 ...

  3. PAT 甲级 1116. Come on! Let's C (20) 【循环判断】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1116 思路 注意一个细节 如果没有本来 ID 的 后来又查了这个ID 不是输出 checked ...

  4. 不使用 spring-boot-starter-parent 构建 spring boot 应用

    创建 spring-boot 应用通用方法是配置 pom.xml,定义 为 spring-boot-start-parent.如下: <parent> <groupId>org ...

  5. 借助node.js + mysql 学习基础ajax~

    很多小白不知道ajax怎么学,所以就弄了个node后台模拟下基本的ajax请求. 环境要求是安装node~ 先上linkMysql.js var mysql = require('mysql') va ...

  6. 请求时token过期自动刷新token

    1.在开发过程中,我们都会接触到token,token的作用是什么呢?主要的作用就是为了安全,用户登陆时,服务器会随机生成一个有时效性的token,用户的每一次请求都需要携带上token,证明其请求的 ...

  7. EASYARM-IMX283 nfs启动内核和根文件系统

    EASYARM-IMX283(以下简称IMX283)默认采用从nand flash启动,但是在开发过程中因为要频繁的替换内核,我们更倾向于从nfs启动. 先看看IMX283中uboot中默认采用的启动 ...

  8. curl的安装与使用

    linux 下的curl扩展安装,记录一下(发现网上好多抄袭的也不检测一下能不能用) 1.下载curl安装包: https://curl.haxx.se/download.html 2.解压: 如 t ...

  9. css3-rotate实现超炫环形旋转特效

    css3-rotate实现超炫环形旋转特效,css3特效,环形旋转,圆形旋转,css3-rotate实现超炫环形旋转特效是一款采用css3 rotate实现的蓝色环形旋转特效代码. http://ww ...

  10. BZOJ 1616 [Usaco2008 Mar]Cow Travelling游荡的奶牛:dp【网格型】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1616 题意: 有一个n*m的网格. '.'表示平坦的草地,'*'表示挡路的树(不能走). ...