Skip to content

Latest commit

 

History

History
135 lines (52 loc) · 2.99 KB

File metadata and controls

135 lines (52 loc) · 2.99 KB

中文文档

Description

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 M moves.

 

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:

    <li><code>0 &lt;= edges.length &lt;= 10000</code></li>
    
    <li><code>0 &lt;= edges[i][0] &lt;&nbsp;edges[i][1] &lt; N</code></li>
    
    <li>There does not exist any&nbsp;<code>i != j</code> for which <code>edges[i][0] == edges[j][0]</code> and <code>edges[i][1] == edges[j][1]</code>.</li>
    
    <li>The original graph&nbsp;has no parallel edges.</li>
    
    <li><code>0 &lt;= edges[i][2] &lt;= 10000</code></li>
    
    <li><code>0 &lt;= M &lt;= 10^9</code></li>
    
    <li><code><font face="monospace">1 &lt;= N &lt;= 3000</font></code></li>
    
    <li>A reachable node is a node that can be travelled to&nbsp;using at most&nbsp;M moves starting from&nbsp;node 0.</li>
    
 

Solutions

Python3

Java

...