LeetCode 847. Shortest Path Visiting All Nodes
题目链接: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的更多相关文章
- [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 ...
- [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)
题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...
- leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径
设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...
- 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
- 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 ...
- [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 ...
- 最短路径遍历所有的节点 Shortest Path Visiting All Nodes
2018-10-06 22:04:38 问题描述: 问题求解: 本题要求是求遍历所有节点的最短路径,由于本题中是没有要求一个节点只能访问一次的,也就是说可以访问一个节点多次,但是如果表征两次节点状态呢 ...
- LeetCode 1091. Shortest Path in Binary Matrix
原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...
- [LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...
随机推荐
- zookeeper命令行练习,熟练节点权限, 数组越界异常解决方案
https://www.cnblogs.com/qlqwjy/p/10517231.html zookeeper 创建持久有序节点时候报错,我的版本号是3.4.12, 亲测有效 https://blo ...
- Android系统编程入门系列之应用环境及开发环境介绍
作为移动端操作系统,目前最新的Android 11.0已经发展的比较完善了,现在也到了系统的整理一番的时间,接下来的系列文章将以Android开发者为中心,争取用归纳总结的态度对初级入门者所应 ...
- 我的QT学习路线(目前)
qt基础->qt事件->qt绘图->qt文件IO->qt网络通信->qt多线程->qt数据库操作.
- 一、Nginx的安装
1.下载nginx软件 http://nginx.org/download/ 2.安装依赖包 [root@client ~]# useradd -s /sbin/nologin nginx 创建ngi ...
- 『动善时』JMeter基础 — 46、使用Badboy工具录制JMeter脚本
目录 1.使用Badboy录制JMeter脚本 2.使用Badboy参数化配置 3.解决"当前页面的脚本发生错误"提示框 4.总结 1.使用Badboy录制JMeter脚本 打开B ...
- 1.5w字 + 24张图肝翻 TCP。
TCP 是一种面向连接的单播协议,在 TCP 中,并不存在多播.广播的这种行为,因为 TCP 报文段中能明确发送方和接受方的 IP 地址. 在发送数据前,相互通信的双方(即发送方和接受方)需要建立一条 ...
- 【曹工杂谈】Mysql客户端上,时间为啥和本地差了整整13个小时,就离谱
瞎扯一点非技术 本来今天上午就打算写的,结果中途被别的事吸引了注意力,公司和某保险公司合作推了一个医疗保险,让我们给父母买,然后我研究了半天条款:又想起来之前买的支付宝那个好医保,也买了两年多了,但是 ...
- 冷备搭建DG
1.主库开启归档 SQL> archive log list;(查询当前归档状态) SQL> shutdown immediate; SQL> startup mount;(启动到m ...
- Oracle11g RAC详解
升级变化: Oracle10g,高版本都是由低版本升级得到的.要装10.2.0.4,必须先安装database10.2.0.1,然后给这个ORACLE_HOME打补丁p6180189 ...
- 在vue项目中使用echarts
1.安装echarts依赖npm install echarts --save 2.在要使用的页面引入import echarts from 'echarts'v5之后使用 import * echa ...