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, "@& ...
随机推荐
- H.264/H265码流解析
H.264/H265码流解析 一.H.264码流解析 一个原始的H.264 NALU 单元常由 [StartCode] [NALU Header] [NALU Payload] 三部分组成 一个原始的 ...
- Docker Buildx插件
Docker Buildx插件 Overview Docker Buildx是一个CLI插件,它扩展了Docker命令,完全支持Moby BuildKit builder toolkit提供的功能.它 ...
- 从简单示例看对象的创建过程, 为什么双重检查的单例模式,分析Volatile关键字不能少
编译指令 :javac Test.java 反编译指令: javap -v Test 代码 public class ObjectTest { int m = 8; public static voi ...
- Java面试必知必会:基础
面试考察的知识点多而杂,要完全掌握需要花费大量的时间和精力.但是面试中经常被问到的知识点却没有多少,你完全可以用 20% 的时间去掌握 80% 常问的知识点. 一.基础 包括: 杂七杂八 面向对象 数 ...
- C++ QT安装教程2021
第一步 去官网下载 https://download.qt.io/archive/qt/ 第二步 next 然后 我是注册的账号 注意密码的格式,要求至少7位,包含大小写字母和数字 第三步 点击下一步 ...
- [源码解析] 深度学习分布式训练框架 horovod (4) --- 网络基础 & Driver
[源码解析] 深度学习分布式训练框架 horovod (4) --- 网络基础 & Driver 目录 [源码解析] 深度学习分布式训练框架 horovod (4) --- 网络基础 & ...
- 【ElasticSearch】给ElasticSearch数据库配置慢查询日志
给ElasticSearch引擎配置慢查询日志,可以实时监控搜索过慢的日志.虽然ElasticSearch以快速搜索而出名,但随着数据量的进一步增大或是服务器的一些性能问题,会有可能出现慢查询的情况. ...
- 谁能干掉了if else
很多人觉得自己写的是业务代码,按照逻辑写下去,再把公用的方法抽出来复用就可以了,设计模式根本就没必要用,更没必要学. 一开始的时候,我也是这么想,直到我遇到... 举个例子 我们先看一个普通的下单拦截 ...
- Vue指令实现原理
前言 自定义指令是vue中使用频率仅次于组件,其包含bind.inserted.update.componentUpdated.unbind五个生命周期钩子.本文将对vue指令的工作原理进行相应介绍, ...
- python用random模块模拟抽奖逻辑(print修改end参数使打印结果不分行)
import random #引入random模块,运用random函数list_one=["10081","10082","10083" ...