#P5344. 【XR-1】逛森林
【XR-1】逛森林
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
京公网安备 11011102002149号