#P9706. 「TFOI R1」Ride the Wind and Waves
「TFOI R1」Ride the Wind and Waves
Background
Professor Z is the teacher of Class C.
Professor Z recently discovered a magical phenomenon: all of his students secretly have a crush, but none of them dares to confess.
As someone who has been through it, Professor Z of course understands the most real and pure thoughts in each student's heart, as well as what they believe to be love. Professor Z recalled his first love, Jiao Tailang (pinyin), and he did not want his students to lose color in their youth. So, Professor Z took the risk of being fired and主动 helped the students express their feelings.
Then Professor Z was fired.
Problem Description
There is an inward directed unicyclic graph with nodes (weakly connected is guaranteed). Each edge has a weight. There is also a fixed parameter .
Since the graph is inward, a node may have nodes that it cannot reach directly. However, we may reverse some directed edges in the graph so that can reach every node. If a node needs to reverse at least edges in order to reach , then is called a "Ride the Wind and Waves" node of . After reversing the minimum number of edges so that can reach , on the shortest path from to , define as the sum of weights of the non-reversed edges, and as the sum of weights of the reversed edges.
If is a "Ride the Wind and Waves" node of , then there is a value representing the wave value from to , defined as .
For each node , output the value of , where ranges over all "Ride the Wind and Waves" nodes of .
Input Format
The first line contains two positive integers , representing the size of the unicyclic graph and the comparison parameter.
The next lines each contain three positive integers , indicating that there is an edge in the graph, where the starting node is , the ending node is , and the weight is .
Output Format
Output lines, each containing one positive integer, representing for each node the sum of wave values to all of its "Ride the Wind and Waves" nodes.
7 1
1 4 3
2 1 2
3 1 6
4 3 4
5 2 4
6 4 1
7 5 2
3
5
105
160
9
176
11
7 1
1 2 3
2 3 2
3 1 2
4 1 3
5 4 2
6 2 1
7 6 4
18
32
46
36
48
40
72
Hint
Sample Explanation #1
Take the answer for node as an example. The unicyclic graph looks like the figure below:

We can see that are "Ride the Wind and Waves" nodes of . Compute the answer:
-
.
-
.
-
.
-
.
So , and the answer is .
Constraints
This problem uses bundled testdata.
- Subtask 1 (5 points): , includes special property.
- Subtask 2 (10 points): , includes special property.
- Subtask 3 (25 points): , includes special property.
- Subtask 4 (60 points): , no special restrictions.
Special property: the number of nodes on the cycle is guaranteed to be within .
For all testdata, , , and the answer is guaranteed not to exceed .
Translated by ChatGPT 5