题解

题意

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

分析

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. 近年来世界各地ICO的花式骗局盘点

    很多人说区块链是骗局,其实不然,区块链是一种安全的互联网技术,可以解决当下很多行业的痛点,但也确实存在一些不法分子利用区块链进行行骗,下面整理了世界各地的一些ICO骗局,一起来看看吧. 案例一:越南I ...

  2. 【windows】如何让一个程序开机自启动

    windows的开机自启动也是将一个程序放在文件夹下即可,将应用程序或者快捷方式放在如下文件夹下,即可实现开机自启动 C:\ProgramData\Microsoft\Windows\Start Me ...

  3. linux下chrome和chromedriver的安装

    1.安装chrome 用下面的命令安装最新的 Google Chrome yum install https://dl.google.com/linux/direct/google-chrome-st ...

  4. linux下编译安装python

    从官网下载指定的源码包 https://www.python.org/downloads/source/ 把源码文件以二进制方式上传到linux服务器 安装python需要用到gcc工具,首先查看gc ...

  5. Android 进程增加存活率

    引用 : http://geek.csdn.net/news/detail/68515

  6. POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS   Memory Limit: 10000K Total ...

  7. 数据库,序列化数据为json字符串

    create PROCEDURE [dbo].[usp_SerializeJSON] @ParameterSQL as varchar(max) AS BEGIN declare @SQL nvarc ...

  8. 阿里巴巴fastjson源码阅读(待完成)

    git地址:https://github.com/alibaba/fastjson.git

  9. Java_泛型_01_T与?

    二.参考文档 1.JAVA泛型通配符T,E,K,V区别,T以及Class<T>,Class<?>的区别

  10. Jmeter-聚合报告

    线程组右键--添加--监听器--聚合报告 Aggreagete Report:jmeter最常用的一个Listener,“聚合报告”. Label:每个jmeter的element(例如HTTP Re ...