P18144 [NWRRC 2026] Museum
Description
Mira manages a museum with $n$ rooms, numbered from $1$ to $n$, and $m$ two-way passages. Each passage connects two different rooms and has its own maintenance cost. No two passages connect the same pair of rooms, and every room is reachable from every other room.
Mira wants to keep exactly $n - 1$ passages open so that every room remains reachable from every other room. She will place a valuable exhibit in one of the rooms. To make the exhibit easier to guard, that room must have exactly one open passage leading to it.
For each room, find the minimum total maintenance cost of the open passages if the exhibit is placed there, or determine that this is impossible. The choice of open passages may be different for each room.
Input Format
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$, denoting the number of rooms and the number of passages ($2 \le n \le 10^5$; $n-1 \le m \le 2 \cdot 10^5$).
Each of the following $m$ lines contains three integers $u$, $v$, and $c$, describing a two-way passage between rooms $u$ and $v$ with maintenance cost $c$ ($1 \le u, v \le n$; $u \ne v$; $1 \le c \le 10^9$). No two passages connect the same pair of rooms. Every room is reachable from every other room.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $m$ over all test cases does not exceed $2 \cdot 10^5$.
Output Format
For each test case, print $n$ integers $w_1, w_2, \ldots, w_n$, where $w_i$ is the minimum total maintenance cost of the open passages when the exhibit is placed in room $i$, or $-1$ if no suitable choice of open passages exists.
Explanation/Hint
The figure shows the museum in the first test case. Numbers inside circles denote room numbers, and labels next to passages indicate their maintenance costs. If the exhibit is placed in room $2$, keeping the bold passages open gives the minimum total maintenance cost of $w_2 = 3 + 1 + 7 + 1 = 12$.
