加载中...
882-细分图中的可到达结点(Reachable Nodes In Subdivided Graph)
发表于:2021-12-03 | 分类: 困难
字数统计: 1.6k | 阅读时长: 8分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/reachable-nodes-in-subdivided-graph

英文原文

You are given an undirected graph (the "original graph") with n nodes labeled from 0 to n - 1. You decide to subdivide each edge in the graph into a chain of nodes, with the number of new nodes varying between each edge.

The graph is given as a 2D array of edges where edges[i] = [ui, vi, cnti] indicates that there is an edge between nodes ui and vi in the original graph, and cnti is the total number of new nodes that you will subdivide the edge into. Note that cnti == 0 means you will not subdivide the edge.

To subdivide the edge [ui, vi], replace it with (cnti + 1) new edges and cnti new nodes. The new nodes are x1, x2, ..., xcnti, and the new edges are [ui, x1], [x1, x2], [x2, x3], ..., [xcnti-1, xcnti], [xcnti, vi].

In this new graph, you want to know how many nodes are reachable from the node 0, where a node is reachable if the distance is maxMoves or less.

Given the original graph and maxMoves, return the number of nodes that are reachable from node 0 in the new graph.

 

Example 1:

Input: edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3
Output: 13
Explanation: The edge subdivisions are shown in the image above.
The nodes that are reachable are highlighted in yellow.

Example 2:

Input: edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], maxMoves = 10, n = 4
Output: 23

Example 3:

Input: edges = [[1,2,4],[1,4,5],[1,3,1],[2,3,4],[3,4,5]], maxMoves = 17, n = 5
Output: 1
Explanation: Node 0 is disconnected from the rest of the graph, so only node 0 is reachable.

 

Constraints:

  • 0 <= edges.length <= min(n * (n - 1) / 2, 104)
  • edges[i].length == 3
  • 0 <= ui < vi < n
  • There are no multiple edges in the graph.
  • 0 <= cnti <= 104
  • 0 <= maxMoves <= 109
  • 1 <= n <= 3000

中文题目

给你一个无向图(原始图),图中有 n 个节点,编号从 0n - 1 。你决定将图中的每条边细分为一条节点链,每条边之间的新节点数各不相同。

图用由边组成的二维数组 edges 表示,其中 edges[i] = [ui, vi, cnti] 表示原始图中节点 ui 和 vi 之间存在一条边,cnti 是将边细分后的新节点总数。注意,cnti == 0 表示边不可细分。

要细分边 [ui, vi] ,需要将其替换为 (cnti + 1) 条新边,和 cnti 个新节点。新节点为 x1, x2, ..., xcnti ,新边为 [ui, x1], [x1, x2], [x2, x3], ..., [xcnti+1, xcnti], [xcnti, vi]

现在得到一个新的 细分图 ,请你计算从节点 0 出发,可以到达多少个节点?节点 是否可以到达的判断条件 为:如果节点间距离是 maxMoves 或更少,则视为可以到达;否则,不可到达。

给你原始图和 maxMoves ,返回新的细分图中从节点 0 出发 可到达的节点数

 

示例 1:

输入:edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3
输出:13
解释:边的细分情况如上图所示。
可以到达的节点已经用黄色标注出来。

示例 2:

输入:edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], maxMoves = 10, n = 4
输出:23

示例 3:

输入:edges = [[1,2,4],[1,4,5],[1,3,1],[2,3,4],[3,4,5]], maxMoves = 17, n = 5
输出:1
解释:节点 0 与图的其余部分没有连通,所以只有节点 0 可以到达。

 

提示:

  • 0 <= edges.length <= min(n * (n - 1) / 2, 104)
  • edges[i].length == 3
  • 0 <= ui < vi < n
  • 图中 不存在平行边
  • 0 <= cnti <= 104
  • 0 <= maxMoves <= 109
  • 1 <= n <= 3000

通过代码

官方题解

方法:Dijkstra 算法

思路

将原始图作为加权无向图处理,我们可以使用 Dijkstra 算法查找原始图中的所有可到达结点。 但是,这不足以解决仅部分使用细分边的示例。

当我们沿着边(沿任一方向)行进时,我们可以跟踪我们使用它的程度。最后,我们想知道我们在原始图中到达的每个结点,以及每条边的利用率之和。

算法

我们使用 Dijkstra 算法 来找出从源到所有目标的最短距离。 这是一个教科书算法, 请参阅此链接了解详细信息。

另外,对于每条(有向)边 (node,nei),我们将跟踪有多少结点(从原始边细分而来的新结点)被使用。 最后,我们将总结每条边的利用率。

有关更多详细信息,请参阅内联注释。

[qUNcLvX7-C++]
#define pii pair<int, int> class Solution { public: int reachableNodes(vector<vector<int>>& edges, int M, int N) { vector<vector<pii>> graph(N); for (vector<int> edge: edges) { int u = edge[0], v = edge[1], w = edge[2]; graph[u].push_back({v, w}); graph[v].push_back({u, w}); } map<int, int> dist; dist[0] = 0; for (int i = 1; i < N; ++i) dist[i] = M+1; map<pii, int> used; int ans = 0; priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push({0, 0}); while (!pq.empty()) { pii top = pq.top(); pq.pop(); int d = top.first, node = top.second; if (d > dist[node]) continue; dist[node] = d; // Each node is only visited once. We've reached // a node in our original graph. ans++; for (auto pair: graph[node]) { // M - d is how much further we can walk from this node; // weight is how many new nodes there are on this edge. // v is the maximum utilization of this edge. int nei = pair.first; int weight = pair.second; used[{node, nei}] = min(weight, M - d); // d2 is the total distance to reach 'nei' (nei***or) node // in the original graph. int d2 = d + weight + 1; if (d2 < min(dist[nei], M+1)) { pq.push({d2, nei}); dist[nei] = d2; } } } // At the end, each edge (u, v, w) can be used with a maximum // of w new nodes: a max of used[u, v] nodes from one side, // and used[v, u] nodes from the other. for (vector<int> edge: edges) { int u = edge[0], v = edge[1], w = edge[2]; ans += min(w, used[{u, v}] + used[{v, u}]); } return ans; } };
[qUNcLvX7-Java]
class Solution { public int reachableNodes(int[][] edges, int M, int N) { Map<Integer, Map<Integer, Integer>> graph = new HashMap(); for (int[] edge: edges) { int u = edge[0], v = edge[1], w = edge[2]; graph.computeIfAbsent(u, x->new HashMap()).put(v, w); graph.computeIfAbsent(v, x->new HashMap()).put(u, w); } PriorityQueue<ANode> pq = new PriorityQueue<ANode>( (a, b) -> Integer.compare(a.dist, b.dist)); pq.offer(new ANode(0, 0)); Map<Integer, Integer> dist = new HashMap(); dist.put(0, 0); Map<Integer, Integer> used = new HashMap(); int ans = 0; while (!pq.isEmpty()) { ANode anode = pq.poll(); int node = anode.node; int d = anode.dist; if (d > dist.getOrDefault(node, 0)) continue; // Each node is only visited once. We've reached // a node in our original graph. ans++; if (!graph.containsKey(node)) continue; for (int nei: graph.get(node).keySet()) { // M - d is how much further we can walk from this node; // weight is how many new nodes there are on this edge. // v is the maximum utilization of this edge. int weight = graph.get(node).get(nei); int v = Math.min(weight, M - d); used.put(N * node + nei, v); // d2 is the total distance to reach 'nei' (nei***or) node // in the original graph. int d2 = d + weight + 1; if (d2 < dist.getOrDefault(nei, M+1)) { pq.offer(new ANode(nei, d2)); dist.put(nei, d2); } } } // At the end, each edge (u, v, w) can be used with a maximum // of w new nodes: a max of used[u, v] nodes from one side, // and used[v, u] nodes from the other. // [We use the encoding (u, v) = u * N + v.] for (int[] edge: edges) { ans += Math.min(edge[2], used.getOrDefault(edge[0] * N + edge[1], 0) + used.getOrDefault(edge[1] * N + edge[0], 0) ); } return ans; } } class ANode { int node, dist; ANode(int n, int d) { node = n; dist = d; } }
[qUNcLvX7-Python]
class Solution(object): def reachableNodes(self, edges, M, N): graph = collections.defaultdict(dict) for u, v, w in edges: graph[u][v] = graph[v][u] = w pq = [(0, 0)] dist = {0: 0} used = {} ans = 0 while pq: d, node = heapq.heappop(pq) if d > dist[node]: continue # Each node is only visited once. We've reached # a node in our original graph. ans += 1 for nei, weight in graph[node].iteritems(): # M - d is how much further we can walk from this node; # weight is how many new nodes there are on this edge. # v is the maximum utilization of this edge. v = min(weight, M - d) used[node, nei] = v # d2 is the total distance to reach 'nei' (nei***or) node # in the original graph. d2 = d + weight + 1 if d2 < dist.get(nei, M+1): heapq.heappush(pq, (d2, nei)) dist[nei] = d2 # At the end, each edge (u, v, w) can be used with a maximum # of w new nodes: a max of used[u, v] nodes from one side, # and used[v, u] nodes from the other. for u, v, w in edges: ans += min(w, used.get((u, v), 0) + used.get((v, u), 0)) return ans

复杂度分析

  • 时间复杂度:$O(E \log N)$,其中 $E$ 是 edges 的长度。

  • 空间复杂度:$O(N)$。

统计信息

通过次数 提交次数 AC比率
2005 4080 49.1%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
880-索引处的解码字符串(Decoded String at Index)
下一篇:
881-救生艇(Boats to Save People)
本文目录
本文目录