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, "@& ...
随机推荐
- 达芬奇架构NPU
达芬奇架构NPU 达芬奇架构的核心优势是什么?如何更好地赋能麒麟990? 达芬奇架构,是华为自研的面向AI计算特征的全新计算架构,具备高算力.高能效.灵活可裁剪的特性,是实现万物智能的重要基础.具体来 ...
- TensorRT Analysis Report分析报告
TensorRT Analysis Report 一.介绍 TensorRT是一个高性能的深度学习推理(Inference)优化器,可以为深度学习应用提供低延迟.高吞吐率的部署推理.TensorRT可 ...
- 【VBA】读取配置文件存入字典型变量中
配置文件: 源码: Dim Co As Object '设为全局变量 Function 读取cfg() As Boolean Dim strcfg As String strcfg = " ...
- MySQL进阶:主主复制+Keepalived高可用
Blog:博客园 个人 概述 mysql主主复制 所谓主主复制,即双主备份,或者叫互作主从复制,每台master既是master,又是slave.这种方案,既做到了访问量的压力分流,同时也解决了单点故 ...
- Golang Heap 源码剖析
堆原理解析 堆一般指二叉堆.是使用完全二叉树这种数据结构构建的一种实际应用.通过它的特性,分为最大堆和最小堆两种. 如上图可知,最小堆就是在这颗二叉树中,任何一个节点的值比其所在子树的任意一个节点都要 ...
- Linux Oracle 中文乱码解决
1.Linux操作系统Oracle11g设置别名的时候发现中文乱码 2.直接修改环境变量 添加 export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK 3.执行命令使环境变 ...
- Java知识复习(一)
Java面向对象的三大特性: 封装.继承.多态. super()与this()的区别? This():当前类的实例,一个类,如果继承了父类,那么通过this既可以访问当前类的属性和方法,也可以访问父类 ...
- npm ERR! Unexpected end of JSON input while parsing near '...'解决方法
npm install时出现npm err! Unexpected end of JSON input while parsing near'...'错误 输入 npm cache clean -- ...
- canvas绘制动画的技巧
我们拿下图中的沿着线段轨迹移动的原点来举例,怎么来实现这个动画! 1)定义路径集合Path,里面规定关键坐标点如startPoint和endPoint,设置从startPoint移动到endPoint ...
- Mongo集群搭建
1.集群角色及架构 在搭建集群之前,需要首先了解几个概念:路由,分片.副本集.配置服务器等 mongos,数据库集群请求的入口,所有的请求都通过mongos进行协调,不需要在应用程序添加一个路由选择器 ...