Loading problem...
You are managing a communication network consisting of n nodes, numbered from 1 to n. The network is defined by a list of directed connections, where each connection is represented as connections[i] = [origin, destination, latency]. Here, origin denotes the transmitting node, destination denotes the receiving node, and latency represents the time (in milliseconds) required for a signal to travel from the origin to the destination.
Your task is to broadcast a signal from a designated source node and determine the minimum time required for all nodes in the network to receive the signal. If it is impossible for all nodes to receive the signal (due to disconnected parts of the network), return -1.
The signal propagates optimally, meaning it always travels through the fastest possible paths from the source. Multiple signals can travel simultaneously through different connections, so the total propagation time is determined by the node that takes the longest to receive the signal.
connections = [[2,1,1],[2,3,1],[3,4,1]]
n = 4
source = 22Starting from node 2:
connections = [[1,2,1]]
n = 2
source = 11There are only 2 nodes. Node 1 is the source (time 0), and node 2 receives the signal in 1 ms via the direct connection.
connections = [[1,2,1]]
n = 2
source = 2-1The source is node 2, but the only connection goes from node 1 to node 2 (not the reverse). Since node 1 has no incoming edges from node 2, it can never receive the signal. Therefore, we return -1.
Constraints