题目链接: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. .NET平台系列14 .NET5中的新增功能

    系列目录     [已更新最新开发文章,点击查看详细] .NET5中不包含的内容 尽管 .NET5 框架中提供了一组重要 API,但它并不包括过去20年左右开发的所有 API,但是.NET Stand ...

  2. 性能工具之linux三剑客awk、grep、sed详解

    前言 linux 有很多工具可以做文本处理,例如:sort, cut, split, join, paste, comm, uniq, column, rev, tac, tr, nl, pr, he ...

  3. expdp数据泵导出数据汇总

    [oracle@enmo1 ~]$ mkdir datadump[oracle@enmo1 ~]$ cd datadump/[oracle@enmo1 datadump]$ pwd/home/orac ...

  4. WPF中选择文件和选择文件夹的方法

    最近从winform转WPF,遇到了各种各样的问题.然而网上的关于WPF的资料少之又少,甚至连基本的文件选择操作,百度搜索的首页都没有一个比较好的方法.所以,踩了几个坑之后,我把我得到的方法分享给大家 ...

  5. dos脚本语法学习

    一个dos批处理脚本,通过关键字搜索注册表并删除,坑很多,语法也很怪异,详情看注释 @echo off ::声明采用UTF-8编码,避免中文乱码问题,>NUL可以吞掉chcp输出的内容 chcp ...

  6. Go语言Slice作为函数参数详解

    Go语言Slice作为函数参数详解 前言 首先要明确Go语言中实质只有值传递,引用传递和指针传递是相对于参数类型来说. 个人认为上诉的结论不对,把引用类型看做对指针的封装,一般封装为结构体,结构体是值 ...

  7. 美化terminal时碰到的问题- Set-Theme

    报错: 1 Set-Theme Set-Theme: The term 'Set-Theme' is not recognized as a name of a cmdlet, function, s ...

  8. 聊一聊Unity协程背后的实现原理

    Unity开发不可避免的要用到协程(Coroutine),协程同步代码做异步任务的特性使程序员摆脱了曾经异步操作加回调的编码方式,使代码逻辑更加连贯易读.然而在惊讶于协程的好用与神奇的同时,因为不清楚 ...

  9. 生产环境部署Django项目

    生产环境部署Django项目 1.  部署架构 IP地址 安装服务 172.16.1.251 nginx uwsgi(sock方式) docker mysql5.7 redis5 Nginx 前端We ...

  10. JS刷新窗口的几种方式

    浮层内嵌iframe及frame集合窗口,刷新父页面的多种方法   <script language=JavaScript>       parent.location.reload(); ...