#K35002. Minimum Latency to All Servers

    ID: 25434 Type: Default 1000ms 256MiB

Minimum Latency to All Servers

Minimum Latency to All Servers

You are given a set of servers and a list of directed links between them. Each link has an associated latency. Your task is to compute the minimum latency required to send data from a given source server to every other server in the network.

If a server cannot be reached from the source, output -1 for that server.

The network is described via three parameters: an integer N (the number of servers), an integer M (the number of links), and an integer source (the index of the source server). Following this, M lines are provided, each containing three integers u, v, and w, where there is a directed edge from server u to v with latency w. The output should be a single line of N space-separated integers, where the i-th integer represents the minimum latency from the source server to server i (1-indexed). If server i is unreachable, output -1.

Formula:

The latency from the source to any server \(v\) can be defined recursively by:

\[ D(v) = \min_{(u,v) \in E} \{ D(u) + w(u,v) \}, \quad D(source)=0 \]

inputFormat

The first line of input contains three integers: N (number of servers), M (number of links), and source (the source server index).

This is followed by M lines, each containing three space-separated integers u v w, indicating a directed link from server u to server v with a latency of w.

outputFormat

Output a single line containing N space-separated integers. The i-th integer should represent the minimum latency from the source server to server i. If a server is unreachable, output -1 for that server.

## sample
4 4 1
1 2 1
2 3 1
3 4 1
4 2 1
0 1 2 3

</p>