#P5344. 【XR-1】逛森林

    ID: 4267 远端评测题 3000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>倍增洛谷原创O2优化最短路最近公共祖先,LCAst表,稀疏表

【XR-1】逛森林

Description

You are given a forest with nn nodes, with no edges at the beginning.

There are mm operations, of two types:

1 u1 v1 u2 v2 w1\ u_1\ v_1\ u_2\ v_2\ w: Build a directed portal. From all nodes on the simple path u1v1u_1 \rightarrow v_1, you can pay cost ww to reach all nodes on the simple path u2v2u_2 \rightarrow v_2. If u1u_1 and v1v_1 are not connected, or u2u_2 and v2v_2 are not connected (connectivity is determined only by edges added by type 22 operations), then ignore this operation.

2 u v w2\ u\ v\ w: Add an undirected edge of cost ww between nodes uu and vv. If uu and vv are already connected (connectivity is determined only by edges added by type 22 operations), then ignore this operation.

After these mm operations, find the minimum cost from node ss to every node.

Input Format

The first line contains three positive integers n,m,sn, m, s, representing the number of nodes in the tree, the number of operations, and the starting node.

The next mm lines each contain several positive integers, describing one operation.

Output Format

Output one line with nn integers. The ii-th integer is the minimum cost from node ss to node ii. In particular, if node ii 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 11, you can pay cost 22 to reach all nodes on the simple path 494 \rightarrow 9 (that is, nodes 44 and 99).
  • From all nodes on the simple path 858 \rightarrow 5 (that is, nodes 8,7,6,58, 7, 6, 5), you can pay cost 11 to reach all nodes on the simple path 161 \rightarrow 6 (that is, nodes 1,3,5,61, 3, 5, 6).

It is easy to see that starting from node 55, the minimum costs to reach the other nodes are: 1,1,1,1,0,1,7,9,11, 1, 1, 1, 0, 1, 7, 9, 1.

[Constraints]

For test points 1,21, 2, 1n1001 \le n \le 100, 1m3001 \le m \le 300.

For test points 3,43, 4, 1n10001 \le n \le 1000, 1m30001 \le m \le 3000.

For 100%100\% of the data, 1n500001 \le n \le 50000, 1m1061 \le m \le 10^6, 1u,vn1 \le u,v \le n, 1w1001 \le w \le 100.

For test points 11 to 1010, each is worth 55 points.

For test points 11,1211, 12, each is worth 2525 points.

Translated by ChatGPT 5