[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.length = N, and?j != i?is in the list?graph[i]?exactly once, if and only if nodes?i?and?j?are connected.

Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.

Example 1:

Input: [[1,2,3],[0],[0],[0]]
Output: 4
Explanation: One possible path is [1,0,2,0,3]

Example 2:

Input: [[1],[0,2,4],[1,3,4],[2],[1,2]]
Output: 4
Explanation: One possible path is [0,1,4,2,3]

Note:

  1. 1 <= graph.length <= 12
  2. 0 <= graph[i].length <?graph.length

Github 同步地址:

https://github.com/grandyang/leetcode/issues/847

类似题目:

参考资料:

https://leetcode.com/problems/shortest-path-visiting-all-nodes/

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/11410007.html

时间: 2024-11-08 21:41:44

[LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径的相关文章

LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination

原题链接在这里:https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/ 题目: Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can move up, down, left or right from and to an empty cell. Return

BFS 基础写法 —— 以 LeetCode #1091 Shortest Path in Binary Matrix 为例

Question In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that: Adjacent cells C_i and C_{i+1} are connected

[LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径

We are given a 2-dimensional?grid.?"."?is an empty cell,?"#"?is?a wall,?"@"?is the starting point, ("a",?"b", ...) are keys, and ("A",?"B", ...) are locks. We start at the starting poin

Dijkstra’s Shortest Path Algorithm / LeetCode 787. Cheapest Flights Within K Stops

Dijkstra’s Shortest Path Algorithm 实现详见:https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-using-priority_queue-stl/ 需要注意的是,priority_queue并无法更新内部的元素,因此我们更新dist的同时,直接把新的距离加入pq即可.pq里虽然有outdated的dist,但是由于距离过长,他们并不会更新dist. // If there is sho

Method for finding shortest path to destination in traffic network using Dijkstra algorithm or Floyd-warshall algorithm

A method is presented for finding a shortest path from a starting place to a destination place in a traffic network including one or more turn restrictions, one or more U-turns and one or more P-turns using a Dijkstra algorithm. The method as sets a

The Shortest Path in Nya Graph HDU - 4725

Problem Description This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.The Nya graph is an un

OSPF(Open Shortest Path First)

---恢复内容开始--- 1.概述 路由协议OSPF全称为Open Shortest Path First,也就开放的最短路径优先协议,因为OSPF是由IETF开发的. OSPF的流量使用IP协议号89. OSPF对网络没有跳数限制,支持 Classless Interdomain Routing (CIDR)和Variable-Length Subnet Masks (VLSMs),没有自动汇总功能. OSPF并不会周期性更新路由表,而采用增量更新,即只在路由有变化时,才会发送更新,并且只发送

HDU 4725 The Shortest Path in Nya Graph(构图)

The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13445    Accepted Submission(s): 2856 Problem Description This is a very easy problem, your task is just calculate

leetcode 934. Shortest Bridge

The algorithm for this problem is not so hard to figure out. This is a problem of finding the length of the shortest path in graph. As I mentioned in my previous article, BFS is a good way to slove this kind of problem. This problem is a little diffe