题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/

题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1,每个点可能经过多次。

这道题写的时候想简单了,把它当成树的直径来做了,求出一条最长路径len(len上的点只经过一次),2*(点数-1)-len即为答案,竟然过了,后来看了看讨论区发现这不是正解,而且我也没办法证明,感觉是蒙对的。贴下代码:

class Solution {
public:
void dfs(bool &f,int x, int sum, vector<vector<int>>& graph, int &ans, int vis[]) {
if (f)
return ;
if (sum == graph.size()-1) { //不加这个剪枝还会TLE,加了以后快的飞起,因为存在全连接图
f = 1;
ans = sum;
return ;
}
vis[x] = 1;
for (int i = 0;i < graph[x].size();i++) {
if (vis[graph[x][i]] == 0) {
dfs( f,graph[x][i], sum + 1, graph, ans, vis);
vis[graph[x][i]] = 0;
}
}
ans = max(sum, ans); //ans为图中的最长路径(路径上的点只经过一次)
}
int shortestPathLength(vector<vector<int>>& graph) {
if (graph[0].size() == 0)
return 0;
int vis[15];
memset(vis, 0, sizeof(vis));
int ans = 0;
bool f = 0;
for (int i = graph.size()-1;i>=0&&!f;i--) {
memset(vis, 0, sizeof(vis));
dfs(f,i, 0, graph, ans, vis);
} cout << ans << -1 << endl;
return (graph.size() - 1) * 2 - ans;
}
}; 

正解应该是状压dp:

dp[i][j]表示当前在第i个节点,且已经走过的节点集合为j用二进制表示时1的位置,从u到v,$dp[v][j|(1<<v)] = min(dp[v][j],dp[u][j]+1);$(dp[u][j]为在u点的状态),bfs不断更新即可

class Solution {
public:
int shortestPathLength(vector<vector<int>>& graph) {
vector<vector<int> > dp(graph.size(),vector<int>((1<<graph.size()),1e9) );
queue<pair<int,int> > q; //使用队列存储dp值
for(int i=0;i<graph.size();i++){
dp[i][1<<i]=0;
q.push(make_pair(i,1<<i));
}
while(!q.empty()){
pair p = q.front();
q.pop();
for(int i=0;i<graph[p.first].size();i++){
int v=graph[p.first][i];
if(dp[v][p.second|(1<<v)]>dp[p.first][p.second]+1){ //比当前优则入队
dp[v][p.second|(1<<v)] = dp[p.first][p.second]+1;
q.push(make_pair(v,p.second|(1<<v)));
}
}
}
int ans=1e9;
for(int i=0;i<graph.size();i++)
ans=min(ans,dp[i][(1<<graph.size())-1]);
return ans;
}
};

  

LeetCode 847. Shortest Path Visiting All Nodes的更多相关文章

  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(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  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. AlexeyAB DarkNet YOLOv3框架解析与应用实践(一)

    AlexeyAB DarkNet YOLOv3框架解析与应用实践(一) Darknet:  C语言中的开源神经网络 Darknet是一个用C和CUDA编写的开源神经网络框架.它速度快,易于安装,支持C ...

  2. 用NVIDIA Tensor Cores和TensorFlow 2加速医学图像分割

    用NVIDIA Tensor Cores和TensorFlow 2加速医学图像分割 Accelerating Medical Image Segmentation with NVIDIA Tensor ...

  3. 用java实现一个ATM机系统(2.0版)

    用java实现一个ATM机系统(2.0版) java实现银行ATM自助取款机,实现功能:用户登录.余额查询.存钱.取钱.转账.修改密码.退出系统. 文章目录 用java实现一个ATM机系统(2.0版) ...

  4. ffmpeg实战-音视频合成案例

    转发自白狼栈:查看原文 很多小伙伴私下里留言说,之前没接触过音视频,对于ffmpeg可以做什么还是有些懵. 今天我们一起看下我们究竟可以用 ffmpeg 做什么? 很多小伙伴应该都玩过抖音,你在&qu ...

  5. 【NX二次开发】获取指定矩阵标识的矩阵值

    函数:UF_CSYS_ask_matrix_values () 函数说明:获取指定矩阵标识的矩阵值. 用法: #include <uf.h> #include <uf_csys.h& ...

  6. 「模拟8.19 A嚎叫..(set) B主仆..(DFS) C征程..(DP+堆优化)」

    为啥这一套题目背景感到很熟悉. T1  嚎叫响彻在贪婪的厂房 考试一个小时没调出来,自闭了.......... 正解很好想,最后实在打不出来了只好暴力骗分了... 联想到以前做的题:序列(涉及质因数分 ...

  7. Java知识复习(四)

    最近准备跳槽,又要好好复习基本知识了.过了个年,前面刚接触的springboot也只能先放放了.就先把自己复习了哪些罗列出来吧. Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用== ...

  8. ES6、ES7的一些新特性

    1.常见的就是let 和 const 命令 let 只在命令所在的代码块内有效 const声明一个只读的常量 2.变量的赋值 let [a, b, c] = [1, 2, 3]; 这样输出的话a=1, ...

  9. 玩转STM32MP157- 使用fbtft驱动 lcd ili9341

    之前使用了 fbtft 成功驱动了lcd st7735r,现在尝试下驱动 ili9341, 配置 跟之前用 fbtft 驱动 st7735r 一样,先用 make menuconfig 配置内核,添加 ...

  10. js笔记6

    1.函数都有返回值,人为return,返回什么就是什么,否则,他的返回值就是undefined 而方法的本质也是函数,所以也有返回值 document.getElementById()返回的是获取的标 ...