#P5344. 【XR-1】逛森林
【XR-1】逛森林
Background
NaCly_Fish and PinkRabbit are good friends.
One day, she went to the forest for fun. After coming back, she said to PinkRabbit: “I found many trees that can move!”
PinkRabbit twitched one rabbit ear: “What is so strange about that? With one rabbit ear, I can maintain the shape of every tree.”
NaCly_Fish was not convinced: “Not only that. I also saw some portals. They can jump from one branch to another!”
PinkRabbit twitched the other rabbit ear: “What is so strange about that? With two rabbit ears, I can count the information of every portal.”

So NaCly_Fish felt very depressed, and she asked you for help. Please help her.
What? You are not willing to help?
Then she will not give you the points for this problem.
Problem Description
You are given a forest with nodes, with no edges at the beginning.
There are operations, of two types:
: Build a directed portal. From all nodes on the simple path , you can pay cost to reach all nodes on the simple path . If and are not connected, or and are not connected (connectivity is determined only by edges added by type operations), then ignore this operation.
: Add an undirected edge of cost between nodes and . If and are already connected (connectivity is determined only by edges added by type operations), then ignore this operation.
After these operations, find the minimum cost from node to every node.
Input Format
The first line contains three positive integers , representing the number of nodes in the tree, the number of operations, and the starting node.
The next lines each contain several positive integers, describing one operation.
Output Format
Output one line with integers. The -th integer is the minimum cost from node to node . In particular, if node cannot be reached, output -1.
9 11 5
2 2 1 2
2 3 1 5
2 4 2 10
2 5 3 9
2 6 5 3
2 7 6 6
2 8 7 2
2 9 4 2
1 1 1 4 9 2
1 8 5 1 6 1
1 3 6 9 6 1
1 1 1 1 0 1 7 9 1
Hint
[Sample Explanation]
This is the tree given in the sample (strictly speaking, this tree is also a chain):

There are three portals, and two of them are like this:
- From node , you can pay cost to reach all nodes on the simple path (that is, nodes and ).
- From all nodes on the simple path (that is, nodes ), you can pay cost to reach all nodes on the simple path (that is, nodes ).
It is easy to see that starting from node , the minimum costs to reach the other nodes are: .
[Constraints]
For test points , , .
For test points , , .
For of the data, , , , .
For test points to , each is worth points.
For test points , each is worth points.
Translated by ChatGPT 5