题解

题意

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

分析

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. 扫盲-wpf依赖属性

    一.什么是依赖属性 依赖属性就是一种自己可以没有值,并且可以通过绑定从其他数据源获取值.依赖属性可支持WPF中的样式设置.数据绑定.继承.动画及默认值. 将所有的属性都设置为依赖属性并不总是正确的解决 ...

  2. LeetCode:划分字母区间【763】

    LeetCode:划分字母区间[763] 题目描述 字符串 S 由小写字母组成.我们要把这个字符串划分为尽可能多的片段,同一个字母只会出现在其中的一个片段.返回一个表示每个字符串片段的长度的列表. 示 ...

  3. Automator 实例:使用快捷键 实现 快速在当前路径 打开 iTerm

    1. 在 finder -> 应用程序 或 通过 Spotlight 打开:Automator.app 2. 选择新建 “服务” 3. 设置服务,见下图,设置完成之后,command + s 保 ...

  4. sudo -i和sudo -s

    sudo -i,加载用户变量,并跳转到目标用户home目录:sudo -s,不加载用户变量,不跳转目录: sudo : 暂时切换到超级用户模式以执行超级用户权限,提示输入密码时该密码为当前用户的密码, ...

  5. Android Weekly Notes Issue #275

    Android Weekly Issue #275 September 17, 2017 Android Weekly Issue #275 本期内容包括给Google Map实现一个Marker A ...

  6. 《C prime plus (第五版)》 ---第12章 存储类.链接和内存管理

    12-1:存储类: 1.作用域: 代码块作用域,函数原型作用域和文件作用域. 2.链接:分为外部链接,内部链接和空链接.代码块作用域和函数原型作用域都是空连接,意味着是私有的.而文件作用域的变量可能是 ...

  7. Contiki-Timer 概述

    Contiki有一个clock模块和一系列timer模块:timer,stimer,ctimer,etimer,和rtimer. 一.clock模块 clock模块提供一些处理系统时间的函数,还有一些 ...

  8. linux应用之tomcat安装(centos)

    安装方式:源码安装 软件:apache-tomcat-7.0.29.tar.gz下载地址:http://tomcat.apache.org/download-70.cgi 安装前提 系统必须已经安装配 ...

  9. SpringBoot_Exception_01_No plugin found for prefix 'spring-boto' in the current project

    一.异常现象 spingbott项目在eclipse中执行maven命令:spring-boot:run, 出现异常: No plugin found for prefix 'spring-boto' ...

  10. Java微信公众平台开发_03_消息管理之被动回复消息

    GitHub源码:https://github.com/shirayner/weixin_gz 一.本节要点 1.回调url 上一节,我们启用服务器配置的时候,填写了一个服务器地址(url),如下图, ...