#K85542. Shortest Paths in a Network

    ID: 36664 Type: Default 1000ms 256MiB

Shortest Paths in a Network

Shortest Paths in a Network

You are given an undirected weighted graph representing a computer network. The nodes in the network represent computers, and the edges represent the connections between them with associated weights. Your task is to determine the shortest path lengths from the central server (node 0) to every other computer.

If a computer is unreachable from node 0, output -1 for that computer.

Note: The graph is given as an input in the following format. You are required to read the input from stdin and output the result to stdout. Use Dijkstra's algorithm (or an equivalent method) to compute the shortest paths.

The problem can be formally stated as follows:

Given a graph with \(n\) nodes and \(m\) connections, where each connection is described by three integers \(u, v, w\) indicating an undirected edge between node \(u\) and node \(v\) with weight \(w\), compute the shortest distance from node 0 to every node. If a node is unreachable, its distance is \(-1\).

For example:

Input:
5 5
0 1 4
0 2 1
1 3 1
2 3 5
3 4 3

Output: 0 4 1 5 8

</p>

inputFormat

The input is given via standard input (stdin) in the following format:

 n m
 u1 v1 w1
 u2 v2 w2
 ...
 um vm wm

Here, n is the number of nodes (computers) in the network, and m is the number of connections. Each of the following m lines contains three space-separated integers u, v, and w, representing an undirected edge between nodes u and v with a weight w.

outputFormat

Print a single line containing n space-separated integers. The \(i\)-th integer represents the shortest distance from node 0 to node \(i\). If a node is unreachable, output -1 for that node.

## sample
1 0
0