Input:times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output:2
Explanation:The signal starts at node 2. It takes 1 unit of time to reach node 1, 1 unit of time to reach node 3, and then 1 additional unit of time to reach node 4 from node 3. The maximum time to reach all nodes is 2.
Example 2:
Input:times = [[1,2,1]], n = 2, k = 1
Output:1
Explanation:The signal starts at node 1. It takes 1 unit of time to reach node 2. Since there are only two nodes, the total time is 1.
Example 3:
Input:times = [[1,2,1]], n = 2, k = 2
Output:-1
Explanation:The signal starts at node 2. It can only reach itself, but node 1 is unreachable. Therefore, the result is -1.