题解

题意

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

分析

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. 使用qt+ros调用摄像头遇到的问题

    当使用摄像头遇到如下问题:[usb_cam-1] process has died [pid 12288, exit code 127, cmd /opt/ros/indigo/lib/usb_cam ...

  2. Java for LeetCode 083 Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. 微信小程序开发:学习笔记[2]——WXML模板

    微信小程序开发:学习笔记[2]——WXML模板 快速开始 介绍 WXML 全称是 WeiXin Markup Language,是小程序框架设计的一套标签语言,结合小程序的基础组件.事件系统,可以构建 ...

  4. HDU - 2102 A计划 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 思路 题目有两个坑点 0.Output 说 能在T时刻 找到公主 就输出 YES 但实际上 只要 ...

  5. 【Leetcode-easy】Palindrome Number

    思路:除和求余 取得首位和末尾 比较是否相等. public boolean isPalindrome(int x){ if(x<0){ return false; } int div=1; w ...

  6. GPIO设备虚拟文件结点的创建【转】

    本文转载自:http://blog.csdn.net/dwyane_zhang/article/details/6742066 所谓GPIO设备虚拟文件结点,就是方便用户在应用程序直接操纵GPIO的值 ...

  7. Too many open files解决方案及原理

    以下是我解决Too many open files异常时学习的知识的理解和总结,如有不正确指出,敬请指出! 此问题中文搜索雷同,你可以尝试以下关键字:"file descriptor lea ...

  8. FastJson 输出值 首字母大小写问题

    解决方案: 1. 如果你的项目由多个模块且为分布式部署, 则可考虑使用设置System.property 2. 一般只是极少数的代码出现此情况, 那么建议直接在你的单例Service初始化时, 在静态 ...

  9. IDEAL葵花宝典:java代码开发规范插件:GsonFormat插件将JSONObject格式的String 解析成实体

    前言: GsonFormat插件主要用于使用Gson库将JSONObject格式的String 解析成实体,该插件可以加快开发进度,使用非常方便,效率高. 这个教程主要是学习IntelliJ IDEA ...

  10. JavaScript中函数的无限次运算问题

    开博客有一段时间了,一直没动笔,原因是确实没看到什么值得写的内容.直到今天在司徒正美的博客里看到一个问题. http://www.cnblogs.com/rubylouvre/archive/2012 ...