[LeetCode] 882. Reachable Nodes In Subdivided Graph 细分图中的可到达结点

Starting with an?undirected?graph (the "original graph") with nodes from?0?to?N-1, subdivisions are made to some of the edges.

The graph is given as follows:?edges[k]?is a list of integer pairs?(i, j, n)?such that?(i, j)?is an edge of the original graph,

and?n?is the total number of?new?nodes on that edge.?

Then, the edge?(i, j)?is deleted from the original graph,?n?new nodes?(x_1, x_2, ..., x_n)?are added to the original graph,

and?n+1?new?edges?(i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j)?are added to the original?graph.

Now, you start at node?0?from the original graph, and in each move, you travel along one?edge.?

Return how many nodes you can reach in at most?Mmoves.

Example 1:

Input: `edges` = [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3
Output: 13
Explanation:
The nodes that are reachable in the final graph after M = 6 moves are indicated below.

Example 2:

Input: `edges` = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4
Output: 23

Note:

  1. 0 <= edges.length <= 10000
  2. 0 <= edges[i][0] <?edges[i][1] < N
  3. There does not exist any?i != j?for which?edges[i][0] == edges[j][0]?and?edges[i][1] == edges[j][1].
  4. The original graph?has no parallel edges.
  5. 0 <= edges[i][2] <= 10000
  6. 0 <= M <= 10^9
  7. 1 <= N <= 3000
  8. A reachable node is a node that can be travelled to?using at most?M moves starting from?node 0.

Github 同步地址:

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

参考资料:

https://leetcode.com/problems/reachable-nodes-in-subdivided-graph/

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

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

时间: 2024-10-08 00:03:18

[LeetCode] 882. Reachable Nodes In Subdivided Graph 细分图中的可到达结点的相关文章

882. Reachable Nodes In Subdivided Graph

题目链接 https://leetcode.com/contest/weekly-contest-96/problems/reachable-nodes-in-subdivided-graph/ 解题思路 1)题目要求,经过m步后,可以到达的点,等价于求有多少点距离起点的最短距离小于等于m,即这是一个单源最短路径问题,使用djstra算法 复杂度 时间 o(eloge) 空间复杂度o(e). e为边数 本解决方案的注意点 1)计数时,节点和边上的点要分开计数,防止重复计算节点 2)使用优先队列保

[LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点

Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 and v2. It's guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing. Notice You sh

[LeetCode 题解]: Reverse Nodes in K-Groups

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

LeetCode:Reverse Nodes in k-Group

1.题目名称 Reverse Nodes in k-Group(分组翻转链表) 2.题目地址 https://leetcode.com/problems/reverse-nodes-in-k-group 3.题目内容 英文: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multipl

【leetcode】reverse Nodes in k-groups

问题: 给定一个链表的头指针,以及一个整数k,要求将链表按每k个为一组,组内进行链表逆置.少于k个的部分不做处理. 分析: 个人觉得问题的重点是熟悉链表的就地逆置操作,就是头插法.其他的考察点如果还有的话,就的细心程度. 实现: void reverseList(ListNode *&pre, ListNode *head) { ListNode *tail = NULL; while (head) { ListNode* next = head->next; head->next =

【LeetCode】Swap Nodes in Pairs

题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list,

[LeetCode] 025. Reverse Nodes in k-Group (Hard) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 025. Reverse Nodes in k-Group (Hard) 链接: 题目:https://oj.leetcode.com/problems/reverse-nodes-in-k-group/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个链表每

[LeetCode] 024. Swap Nodes in Pairs (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 024. Swap Nodes in Pairs (Medium) 链接: 题目:https://oj.leetcode.com/problems/swap-nodes-in-pairs/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个链表中的每一对节点对换

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in